是否有一个简单的 C 或 C++ 函数来计算字符串的 sha1 哈希?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6934232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a simple C or C++ function that calculates the sha1 hash of a string?
提问by Cory
Possible Duplicate:
sha1 function in cpp (C++)Hi,
可能的重复:
cpp (C++) 中的 sha1 函数嗨,
I was just looking for a function that calculates the sha1 hash of string and returns the result.
我只是在寻找一个函数来计算字符串的 sha1 哈希并返回结果。
回答by Zaffy
Not built-in. Try openssl's crypto library.
不是内置的。试试 openssl 的加密库。
(https://www.openssl.org/source/)
( https://www.openssl.org/source/)
(https://github.com/openssl/openssl/blob/master/include/openssl/sha.h)
( https://github.com/openssl/openssl/blob/master/include/openssl/sha.h)
(https://www.openssl.org/docs/man1.1.0/crypto/SHA1.html)
( https://www.openssl.org/docs/man1.1.0/crypto/SHA1.html)
#include <openssl/sha.h>
int main()
{
const unsigned char str[] = "Original String";
unsigned char hash[SHA_DIGEST_LENGTH]; // == 20
SHA1(str, sizeof(str) - 1, hash);
// do some stuff with the hash
return 0;
}
Link with -lssl
, which will imply -lcrypto
. If you are linking statically you might need to link both.
与 链接-lssl
,这将暗示-lcrypto
。如果您是静态链接,则可能需要同时链接两者。
回答by jterrace
回答by Nadir Muzaffar
Here's an example: http://www.codeproject.com/KB/recipes/csha1.aspx#csha1is
这是一个例子:http: //www.codeproject.com/KB/recipes/csha1.aspx#csha1is
Also, this question was already addressed on thisthread. They have a link for some further help. Check it out.
另外,这个问题已经在这个线程上解决了。他们有一个链接以获得进一步的帮助。一探究竟。
回答by Chris Thompson
Check out this post on Ubuntu Forums. They suggest looking in libcrypt
.
在 Ubuntu 论坛上查看这篇文章。他们建议查看libcrypt
。
Also there's an implementation herebut I'm not sure what the license is.