//32位静态库编译
perl Configure no-shared VC-WIN32 --prefix=c:\openssl
//nmake -f makefile
//nmake test
nmake -f makefile install
//64位静态库编译
perl Configure no-shared VC-WIN64A --prefix=c:\openssl
nmake -f makefile install
//32位dll编译
perl Configure -shared VC-WIN32 --prefix=c:\openssl
nmake -f makefile install
void printHexstr(const unsigned char* md, int len)
{
int i = 0;
for (i = 0; i < len; i++)
printf("%02x", md[i]);
printf("\n");
}
//计算字符串hello world的md5方法
void testMD5(void)
{
const char* src_str = "hello world";
unsigned char md[MD5_DIGEST_LENGTH];
MD5((unsigned char*)src_str, strlen(src_str), md);
printHexstr(md, MD5_DIGEST_LENGTH);
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, (char*)src_str, strlen(src_str));
MD5_Final(md, &ctx);
printHexstr(md, MD5_DIGEST_LENGTH);
}
void GenerateMd5(char* fileName)
{
MD5_CTX ctx;
MD5_Init(&ctx);
int ret(1);
unsigned char data[READ_DATA_SIZE];
unsigned char out[MD5_DIGEST_LENGTH];
FILE* f = fopen(fileName, "rb");
if (NULL == f)
return ;
while (true)
{
ret = fread(data, 1, READ_DATA_SIZE, f);
if (-1 == ret)
{
fclose(f);
return ;
}
MD5_Update(&ctx, (char*)data, ret);
if (0 == ret || ret < READ_DATA_SIZE)
break;
}
fclose(f);
MD5_Final(out, &ctx);
printHexstr(out, MD5_DIGEST_LENGTH);
}
void GenerateSHA512(char* fileName)
{
SHA512_CTX ctx;
SHA512_Init(&ctx);
int ret(1);
unsigned char data[READ_DATA_SIZE];
unsigned char out[SHA512_DIGEST_LENGTH];
FILE* f = fopen(fileName, "rb");
if (NULL == f)
return;
while (true)
{
ret = fread(data, 1, READ_DATA_SIZE, f);
if (-1 == ret)
{
fclose(f);
return;
}
SHA512_Update(&ctx, (char*)data, ret);
if (0 == ret || ret < READ_DATA_SIZE)
break;
}
fclose(f);
SHA512_Final(out, &ctx);
OPENSSL_cleanse(&ctx, sizeof(ctx));
printHexstr(out, SHA512_DIGEST_LENGTH);
}
#include
#include
#include
#include
#include
#pragma comment(lib,"libcrypto.lib")
#pragma comment(lib,"libssl.lib")
using namespace std;
#define READ_DATA_SIZE 1024
const char* src_str = "hello world";
void printHexstr(const unsigned char* md, int len)
{
int i = 0;
for (i = 0; i < len; i++)
printf("%02x", md[i]);
printf("\n");
}
void testMD5()
{
printf("---------MD5 start-----\n");
unsigned char md[MD5_DIGEST_LENGTH];
MD5((unsigned char*)src_str, strlen(src_str), md);
printHexstr(md, MD5_DIGEST_LENGTH);
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, (char*)src_str, strlen(src_str));
MD5_Final(md, &ctx);
printHexstr(md, MD5_DIGEST_LENGTH);
printf("---------MD5 end-----\n\n");
}
void testSHA1()
{
printf("---------SHA1 start-----\n");
SHA_CTX c;
unsigned char md[SHA_DIGEST_LENGTH];
SHA1((unsigned char*)src_str, strlen(src_str), md);
printHexstr(md, SHA_DIGEST_LENGTH);
SHA1_Init(&c);
SHA1_Update(&c, src_str, strlen(src_str));
SHA1_Final(md, &c);
OPENSSL_cleanse(&c, sizeof(c));
printHexstr(md, SHA_DIGEST_LENGTH);
printf("---------SHA1 end-----\n\n");
}
void testSHA256()
{
printf("---------SHA256 start-----\n");
SHA256_CTX c;
unsigned char md[SHA256_DIGEST_LENGTH];
SHA256((unsigned char*)src_str, strlen(src_str), md);
printHexstr(md, SHA256_DIGEST_LENGTH);
SHA256_Init(&c);
SHA256_Update(&c, src_str, strlen(src_str));
SHA256_Final(md, &c);
OPENSSL_cleanse(&c, sizeof(c));
printHexstr(md, SHA256_DIGEST_LENGTH);
printf("---------SHA256 end-----\n\n");
}
void testSHA384()
{
printf("---------SHA384 start-----\n");
SHA512_CTX c;
unsigned char md[SHA384_DIGEST_LENGTH];
SHA384((unsigned char*)src_str, strlen(src_str), md);
printHexstr(md, SHA384_DIGEST_LENGTH);
SHA384_Init(&c);
SHA384_Update(&c, src_str, strlen(src_str));
SHA384_Final(md, &c);
OPENSSL_cleanse(&c, sizeof(c));
printHexstr(md, SHA384_DIGEST_LENGTH);
printf("---------SHA384 end-----\n\n");
}
void testSHA512()
{
printf("---------SHA512 start-----\n");
SHA512_CTX c;
unsigned char md[SHA512_DIGEST_LENGTH];
SHA512((unsigned char*)src_str, strlen(src_str), md);
printHexstr(md, SHA512_DIGEST_LENGTH);
SHA512_Init(&c);
SHA512_Update(&c, src_str, strlen(src_str));
SHA512_Final(md, &c);
OPENSSL_cleanse(&c, sizeof(c));
printHexstr(md, SHA512_DIGEST_LENGTH);
printf("---------SHA512 end-----\n\n");
}
void GenerateMd5(char* fileName)
{
printf("---------file MD5 start-----\n");
MD5_CTX ctx;
MD5_Init(&ctx);
int ret(1);
unsigned char data[READ_DATA_SIZE];
unsigned char out[MD5_DIGEST_LENGTH];
FILE* f = fopen(fileName, "rb");
if (NULL == f)
return ;
while (true)
{
ret = fread(data, 1, READ_DATA_SIZE, f);
if (-1 == ret)
{
fclose(f);
return ;
}
MD5_Update(&ctx, (char*)data, ret);
if (0 == ret || ret < READ_DATA_SIZE)
break;
}
fclose(f);
MD5_Final(out, &ctx);
printHexstr(out, MD5_DIGEST_LENGTH);
printf("---------MD5 end-----\n\n");
}
void GenerateSHA512(char* fileName)
{
printf("---------file SHA512 start-----\n");
SHA512_CTX ctx;
SHA512_Init(&ctx);
int ret(1);
unsigned char data[READ_DATA_SIZE];
unsigned char out[SHA512_DIGEST_LENGTH];
FILE* f = fopen(fileName, "rb");
if (NULL == f)
return;
while (true)
{
ret = fread(data, 1, READ_DATA_SIZE, f);
if (-1 == ret)
{
fclose(f);
return;
}
SHA512_Update(&ctx, (char*)data, ret);
if (0 == ret || ret < READ_DATA_SIZE)
break;
}
fclose(f);
SHA512_Final(out, &ctx);
OPENSSL_cleanse(&ctx, sizeof(ctx));
printHexstr(out, SHA512_DIGEST_LENGTH);
printf("---------SHA512 end-----\n\n");
}
int main(int argc, char** argv)
{
testMD5();
testSHA1();
testSHA256();
testSHA384();
testSHA512();
//GenerateMd5(argv[1]);
GenerateSHA512(argv[1]);
getchar();
return 0;
}