編譯時用以下指令:
gcc -o test test.c -lcrypto
範例本文:
/* compile with OpenSSL */
/* adapted from SSLeay source */
#include
#include
#include
#include
#define BUFSIZE 1024*4
void do_all (FILE * f, const char *fname);
void pt_all (unsigned char *md5, unsigned char *sha, unsigned char *sha1,
const char *fname);
int read (int, void *, unsigned int);
int
main (int argc, char **argv)
{
int i, err = 0;
FILE * IN;
if (argc == 1)
{
do_all (stdin, "STDIN\0");
}
else
{
for (i = 1; i < argc; i++)
{
IN = fopen (argv[i], "rb");
if (IN == NULL)
{
perror (argv[i]);
err++;
continue;
}
do_all (IN, argv[i]);
fclose (IN);
}
}
return (err);
}
#define LENGTH 16
void
do_all (FILE * f, const char *fname)
{
MD5_CTX c1;
SHA_CTX c2;
SHA_CTX c3;
unsigned char md5[LENGTH];
unsigned char sha[LENGTH];
unsigned char sha1[LENGTH];
int fd;
int i;
static unsigned char buf[BUFSIZE];
fd = fileno (f);
MD5_Init (&c1);
SHA_Init (&c2);
SHA1_Init (&c3);
for (;;)
{
i = read (fd, buf, BUFSIZE);
if (i <= 0)
break;
MD5_Update (&c1, buf, (unsigned long) i);
SHA_Update (&c2, buf, (unsigned long) i);
SHA1_Update (&c3, buf, (unsigned long) i);
} MD5_Final (&(md5[0]), &c1);
SHA_Final (&(sha[0]), &c2);
SHA1_Final (&(sha1[0]), &c3);
pt_all (md5, sha, sha1, fname);
} void
pt_all (unsigned char *md5, unsigned char *sha, unsigned char *sha1,
const char *fname)
{
int i;
printf ("MD5(%s)= ", fname);
for (i = 0; i < LENGTH; i++)
printf ("%02x", md5[i]);
printf ("\n");
printf ("SHA(%s)= ", fname);
for (i = 0; i < LENGTH; i++)
printf ("%02x", sha[i]);
printf ("\n");
printf ("SHA1(%s)= ", fname);
for (i = 0; i < LENGTH; i++)
printf ("%02x", sha1[i]);
printf ("\n");
}
沒有留言:
張貼留言