开发者

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

开发者 https://www.devze.com 2023-03-24 21:42 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: sha1 func开发者_如何转开发tion in cpp (C++)Hi,
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

sha1 func开发者_如何转开发tion in cpp (C++)Hi,

I was just looking for a function that calculates the sha1 hash of string and returns the result.


Not built-in. Try openssl's crypto library.

(https://www.openssl.org/source/)

(https://github.com/openssl/openssl/blob/master/include/openssl/sha.h)

(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.


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


libgcrypt


Here's an example: http://www.codeproject.com/KB/recipes/csha1.aspx#csha1is

Also, this question was already addressed on this thread. They have a link for some further help. Check it out.


Check out this post on Ubuntu Forums. They suggest looking in libcrypt.

Also there's an implementation here but I'm not sure what the license is.


You need to use a library. Boost has this functionality.

0

精彩评论

暂无评论...
验证码 换一张
取 消