opensslsha1算法源碼c++版

opensslsha1算法源碼c++版

#include <iostream>  
#include <openssl/sha.h>   
#include <openssl/crypto.h>  // OPENSSL_cleanse  
#pragma comment(lib, "libeay32.lib")  


using namespace std;  

    const char *orgStr = "hello"; //待加密的字符串 

    void mysh1()  
    {  
        SHA_CTX c;//SHA_CTX結構體  
        unsigned char md[SHA_DIGEST_LENGTH];  
        SHA1((unsigned char *)orgStr, strlen(orgStr), md); //補位 


        SHA1_Init(&c);  //初始化
        SHA1_Update(&c, orgStr, strlen(orgStr)); //進行hash 
//SHA1Transform在SHA1_Update中調用,是內部函數,不需要用戶調用
        SHA1_Final(md, &c);//加密完成  
        OPENSSL_cleanse(&c, sizeof(c)); //清理結構體 
        printsha1(md, SHA_DIGEST_LENGTH);  //輸出密文
    }  

    // 字符串格式化輸出
    void printsha1(unsigned char *md, int len)  
    {  
        int i = 0;  
        for (i = 0; i < len; i++)  
           {  
            printf("%02x", md[i]);  
        }  

        printf("\n");  
    }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章