首页 > 编程语言 > 详细

c++ 哈希加密

时间:2021-05-11 17:21:24      阅读:19      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>  //sprintf() 需要用到该库
#include <string.h>
#include <iostream>
#include <string>

using namespace std;

#include <openssl/sha.h>
string sha256(const string str)
{
    char buf[2];
    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256_CTX sha256;
    SHA256_Init(&sha256);
    SHA256_Update(&sha256, str.c_str(), str.size());
    SHA256_Final(hash, &sha256);
    std::string NewString = "";
    for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
    {
        sprintf(buf,"%02x",hash[i]);
        NewString = NewString + buf;
    }
        return NewString;
}

int main() {
    string x;
    cout<<"Please enter:";
    getline(cin,x);
    cout << sha256(x) << endl;
    return 0;
}
g++ -lssl -lcrypto -o sha256 sha256.cpp

参考:

https://www.cnblogs.com/spmt/p/12522383.html

https://my.oschina.net/bobwei/blog/524147

c++ 哈希加密

原文:https://www.cnblogs.com/CloudComputing-binbin/p/14754486.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!