Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 642
Cory Avatar asked Aug 03 '11 22:08

Cory


People also ask

How is SHA-1 hash calculated?

SHA-1 works by feeding a message as a bit string of length less than 2 64 2^{64} 264 bits, and producing a 160-bit hash value known as a message digest. Note that the message below is represented in hexadecimal notation for compactness.

Can you decode SHA-1?

Since SHA-1 maps several byte sequences to one, you can't "decrypt" a hash, but in theory you can find collisions: strings that have the same hash. It seems that breaking a single hash would cost about 2.7 million dollars worth of computer time currently, so your efforts are probably better spent somewhere else.

Is SHA-1 () a secure method for hashing data?

SHA-1 is now considered insecure since 2005. Major tech giants browsers like Microsoft, Google, Apple and Mozilla have stopped accepting SHA-1 SSL certificates by 2017. To calculate cryptographic hashing value in Java, MessageDigest Class is used, under the package java.

What is SHA-1 hash generator?

What is a SHA-1 Hash? SHA-1 (Secure Hash Algorithm) is a 160 bit cryptographic hash function created by the NSA in 1995. It creates a 40 byte hash value for the input of the algorithm. SHA-1 is one-way, meaning that the original input cannot be be determined simply by knowing the hash value.


1 Answers

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.

like image 91
Zaffy Avatar answered Oct 29 '22 16:10

Zaffy