Advertisement
spikeysnack

evp_digest.c

Jul 1st, 2023
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | Cybersecurity | 0 0
  1. #include <stdlib.h>
  2.  
  3. #include <stdio.h>
  4.  
  5. #include <string.h>
  6.  
  7. #include <openssl/evp.h>
  8.  
  9.  
  10. // crash message & cleanup
  11. void crash( EVP_MD_CTX* ctx, const char* msg)
  12. {
  13.   EVP_MD_CTX_free(ctx);
  14.  
  15.   fprintf(stderr, "%s\n", msg);
  16.  
  17.   exit(1);
  18. }
  19.  
  20.  
  21.  
  22.  
  23. int main(int argc, char *argv[])
  24. {  
  25.  
  26.   // evp structures
  27.  
  28.   EVP_MD_CTX *mdctx;                         // EVP  Message Digest context
  29.  
  30.   const EVP_MD *md;                         // Message Digest
  31.  
  32.   unsigned char md_value[EVP_MAX_MD_SIZE];  // output byte array  (hash accumulates here)
  33.  
  34.   unsigned int md_len;                  // length of digest
  35.  
  36.  
  37.   // local vars
  38.  
  39.   const char* prog_name;            // this program name
  40.   const char* digest_str;           // name of digest  "md5"  "sha1"  "sha224"
  41.   const char* fname;                // file to hash
  42.  
  43.   FILE* inFile = NULL;              // input file
  44.  
  45.  
  46.   if (argc == 3)  // parse args
  47.       {
  48.     prog_name  = argv[0];    
  49.     digest_str = argv[1];  
  50.     fname      = argv[2];  
  51.    
  52.       }
  53.   else   // bad args
  54.       {
  55.     fprintf(stderr, "no file: specified.\n usage %s  <digest>  <filename>\n", argv[0]);
  56.     exit(2);  // return bad arguments
  57.       }
  58.  
  59.  
  60.   // try open file
  61.   inFile = fopen(fname, "rb");    
  62.  
  63.   if ( inFile == NULL )            // fail to open
  64.     {
  65.       fprintf(stderr, "could not open file: %s\n", fname);
  66.      
  67.       fprintf(stderr, "usage:\t %s  <digest>  <filename>\n", prog_name );
  68.      
  69.       exit(1); // fail
  70.     }
  71.  
  72.  
  73.   // get digest type
  74.   md = EVP_get_digestbyname(digest_str);
  75.  
  76.   if (md == NULL)  // bad digest name
  77.       {
  78.         fprintf( stderr, "Unknown message digest %s\n", digest_str);
  79.         exit(2);
  80.       }
  81.  
  82.  
  83.   // new digest context
  84.     mdctx = EVP_MD_CTX_new();
  85.    
  86.     if (EVP_DigestInit_ex2(mdctx, md, NULL) == 0 )
  87.         crash(mdctx, "Message digest initialization failed.\n");
  88.  
  89.  
  90.    
  91.     // read data into a buffer and update the hash
  92.     // 1KB at a time
  93.    
  94.     unsigned char bytedata[1024];  // buffer to read into
  95.  
  96.     size_t bytes;
  97.  
  98.     // update digest context with new bytes
  99.      while ( (bytes = fread (bytedata, 1, 1024, inFile)) != 0 )
  100.        {
  101.  
  102.      if( EVP_DigestUpdate(mdctx, bytedata, 1024) == 0 )
  103.        crash(mdctx, "Message digest update failed.\n");    
  104.        }
  105.      
  106.      // finalize digest
  107.      if (EVP_DigestFinal_ex(mdctx, md_value, &md_len) == 0 )
  108.        crash(mdctx, "Message digest finalization failed.\n");  
  109.  
  110.      // final clean up
  111.      EVP_MD_CTX_free(mdctx);  // clean up dynamic memory
  112.  
  113.      
  114.  
  115.      // output
  116.      
  117.      fprintf(stdout, "%s Digest is: ", digest_str);
  118.  
  119.      for ( unsigned int i = 0; i < md_len; i++)
  120.        fprintf(stdout, "%02x", md_value[i]);  // hex digit
  121.  
  122.     fprintf(stdout,"\n");
  123.    
  124.     return 0;
  125.  
  126. }
  127.  
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement