26

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,797
  • 7
  • 28
  • 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

50

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
  • 19,036
  • 20
  • 88
  • 110
Zaffy
  • 16,801
  • 8
  • 50
  • 77
10

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
  • 64,866
  • 22
  • 157
  • 202
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,772
  • 2
  • 32
  • 48
2

libgcrypt

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
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
  • 35,167
  • 12
  • 80
  • 109
-4

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

Zelgada
  • 329
  • 2
  • 5