是否有一个简单的 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:56:58  来源:igfitidea点击:

Is there a simple C or C++ function that calculates the sha1 hash of a string?

c++encryptionhash

提问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

CryptoPPis a great C++ library for cryptographic functions. It has a method for calculating a SHA1 digest. See examples of the hashing functions here.

CryptoPP是用于加密函数的出​​色 C++ 库。它有一种计算 SHA1 摘要的方法。请参阅此处的散列函数示例。

回答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 Karoly Horvath

回答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.

这里也有一个实现但我不确定许可证是什么。

回答by Zelgada

You need to use a library. Boosthas this functionality.

你需要使用一个库。 Boost有这个功能。