sha.c 609 B

123456789101112131415161718192021222324252627
  1. #include "sha.h"
  2. static unsigned char *generateHash(const char *string, size_t length) {
  3. unsigned char *hash = malloc(SHA_DIGEST_LENGTH);
  4. SHA1((unsigned char*)string, length, hash);
  5. return hash;
  6. }
  7. static char *hashToString(unsigned char *hash) {
  8. char *string = malloc(2 * SHA_DIGEST_LENGTH + 1);
  9. for(int i = 0; i < SHA_DIGEST_LENGTH; i++)
  10. sprintf(string+(i*2), "%02x", hash[i]);
  11. return string;
  12. }
  13. char *getHash(const char *string) {
  14. size_t length = strlen(string);
  15. unsigned char *hash = generateHash(string, length);
  16. char *hashString = hashToString(hash);
  17. free(hash);
  18. return hashString;
  19. }