C++计算md5/sha1/sha256/sha384/sha512算法

最近需要分析文件的特征唯一值需求,这几个哈希算法就非常有用了.特别记录一下!

算法需要openssl库的支持,如果你勤快也能把库里面依赖的代码抠出来.我就懒得的弄了.直接编译openssl库 下载openssl1.1.1源码,下载perl运行环境

这里用的是vs2019 开始菜单找一下vs的命令行环境 注意x86和x64要选好对应不然会报错的(编译过boost的都会玩的)

C++计算md5/sha1/sha256/sha384/sha512算法_第1张图片

 vc2019命令行编译命令

//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

这里为了方便我就不nmake test测试了直接安装

C++计算md5/sha1/sha256/sha384/sha512算法_第2张图片

nmake编译生成需要的库 libcrypto.lib libssl.lib,这两个库就是需要链接用到的,现在开始撸代码吧.

计算字符串的md5方法:

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);
}

计算文件的MD5 sha512方法,弄个大文件试试非常好使吧.

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;
}

其他的怎么造就不用我说太多了.

 

你可能感兴趣的:(C++,c++,算法)