Skip to content

Instantly share code, notes, and snippets.

@jbdrvl
Created August 5, 2018 05:25
Show Gist options
  • Save jbdrvl/b0a51589587ad447215ede4fa653cb79 to your computer and use it in GitHub Desktop.
Save jbdrvl/b0a51589587ad447215ede4fa653cb79 to your computer and use it in GitHub Desktop.
just returns the hash of argv[1]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
/*
* SHA1 Function
* Description: just returns the hash of argv[1]
* To compile: gcc -Wall sha1.c -o sha1 -lcrypto
*/
int main(int argc, char *argv[]) {
char hash_string[SHA_DIGEST_LENGTH*2+1];
unsigned char hash[SHA_DIGEST_LENGTH];
if(argc!=2) {
printf("Usage:\n./sha ARG\n");
exit(1);
}
SHA_CTX ctx;
SHA1_Init(&ctx);
SHA1_Update(&ctx, argv[1], strlen(argv[1]));
SHA1_Final(hash, &ctx);
for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
sprintf(&hash_string[i*2], "%02x", (unsigned int)hash[i]);
}
printf("Hash: %s\n", hash_string);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment