digest_generator.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include<stdio.h>
  2. #include<errno.h>
  3. #include<openssl/ssl.h>
  4. #include<string.h>
  5. /* Macro to turn on compile with POSIX standard IEEE 1003.1-2008 */
  6. #define POSIX_C_SOURCE 200809L
  7. /* Definitions */
  8. #define SLURP_DIGEST_LENGTH 12
  9. #define MAP "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._"
  10. /**
  11. * Returns 12 byte digest like phabricator
  12. */
  13. void generate_digest(char *digest, char *hash) {
  14. for(int i=0;i<SLURP_DIGEST_LENGTH;i++) {
  15. unsigned int value = hash[i] & 0x3f;
  16. char c = MAP[value];
  17. digest[i] = c;
  18. }
  19. return;
  20. }
  21. int main(int argc, char **argv) {
  22. FILE *file = NULL;
  23. file = fopen("words.txt", "r");
  24. if(file == NULL) {
  25. printf("Could not open words.txt\n");
  26. return 1;
  27. }
  28. char *word;
  29. while(fscanf(file, "%s", word) != EOF) {
  30. if(word == NULL) {
  31. printf("ERROR: Word is a NULL pointer\n");
  32. return 1;
  33. }
  34. char hash[SHA256_DIGEST_LENGTH];
  35. size_t length = strlen(word);
  36. SHA256(word, length, hash);
  37. if(hash != NULL) {
  38. char *digest = (char*) malloc(SLURP_DIGEST_LENGTH);
  39. generate_digest(digest, hash);
  40. printf("%s→%s\n", word, digest);
  41. } else {
  42. printf("Hashing failed\n");
  43. }
  44. }
  45. return 0;
  46. }