22

Possible Duplicate:
sha1 function in cpp (C++)Hi,

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

Community
  • 1
  • 1
Cory
  • 2,637
  • 7
  • 26
  • 28
  • 1
    C (and to a lesser extent, C++) are not known for having lots of built-in functionality. You'll probably be able to find a library that has one or more functions for this purpose. Which language are you using? – Chris Lutz Aug 03 '11 at 22:50
  • 3
    ... and here I am, direct from Googling! – Chap Nov 19 '13 at 17:15

6 Answers6

46

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.

Zitrax
  • 17,576
  • 17
  • 83
  • 103
Zaffy
  • 15,683
  • 8
  • 48
  • 76
9

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.

jterrace
  • 61,216
  • 21
  • 150
  • 193
2

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.

Community
  • 1
  • 1
Nadir Muzaffar
  • 4,602
  • 2
  • 30
  • 45
2

libgcrypt

Karoly Horvath
  • 91,854
  • 11
  • 113
  • 173
0

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.

Chris Thompson
  • 34,318
  • 11
  • 78
  • 107
-3

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

Zelgada
  • 192
  • 1
  • 5