Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

Wrong SHA1 in c++ OPENSSL

Can someone tell me what I'm doing wrong ? I'm trying to print SHA1 hash for "12345" but it displays the wrong one, I can't figure out what is the problem in the code

int main() {

string smth = "12345";

unsigned char hash[SHA_DIGEST_LENGTH];
SHA_CTX sha1;
SHA_Init(&sha1);
SHA_Update(&sha1, smth.c_str(), smth.length());
SHA_Final(hash, &sha1);
stringstream ss;

for (int i = 0; i < SHA_DIGEST_LENGTH; i++)
{
    ss << hex << setw(2) << setfill('0') << (int)hash[i];
}
cout << ss.str() << endl; }

The hash I've got:

f7e507be49c187214406fd2556302ba47d535780

Correct hash:

8cb2237d0679ca88db6464eac60da96345513964
question from:https://stackoverflow.com/questions/66057263/wrong-sha1-in-c-openssl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SHA_* functions calculate SHA-0 sum. Replace all functions with SHA1_* alternatives to calculate SHA-1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...