v0mit

Untitled

Sep 8th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. int create_hash(char *plaintext, char *digest) {
  2.     char digest_buff[3];
  3.     DWORD buf_size = SHA1_HASH_LEN;
  4.     HCRYPTPROV prov;
  5.     HCRYPTHASH hash;
  6.     BYTE rgbHash[SHA1_HASH_LEN];
  7.     CHAR rgbDigits[] = "0123456789abcdef";
  8.     int i;
  9.  
  10.     if(!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
  11.         if(DEBUG) {
  12.             printf("CryptAcquireContext failed: %d\n", GetLastError());
  13.         }
  14.         return SHA1_FAILED;
  15.     }
  16.  
  17.     if(!CryptCreateHash(prov, CALG_SHA, NULL, NULL, &hash)) {
  18.         if(DEBUG) {
  19.             printf("CryptCreateHash failed: %d\n", GetLastError());
  20.         }
  21.         return SHA1_FAILED;
  22.     }
  23.  
  24.     if(!CryptHashData(hash, (BYTE *)plaintext, sizeof(hash), NULL)) {
  25.         if(DEBUG) {
  26.             printf("CryptHashData failed: %d\n", GetLastError());
  27.         }
  28.         return SHA1_FAILED;
  29.     }
  30.  
  31.     if(!CryptGetHashParam(hash, HP_HASHVAL, rgbHash, &buf_size, NULL)) {
  32.         if(DEBUG) {
  33.             printf("CryptGetHashParam failed: %d\n", GetLastError());
  34.         }
  35.         return SHA1_FAILED;
  36.     }
  37.  
  38.     for(i=0; i < buf_size; i++) {
  39.         sprintf(digest_buff, "%c%c", rgbDigits[rgbHash[i] >> 4], rgbDigits[rgbHash[i] & 0xf]);
  40.         strcat(digest, digest_buff);
  41.     }
  42.  
  43.     if(!CryptDestroyHash(hash)) {
  44.         if(DEBUG) {
  45.             printf("CryptDestroyHash failed: %d\n", GetLastError());
  46.         }
  47.         return SHA1_FAILED;
  48.     }
  49.  
  50.     if(!CryptReleaseContext(prov, NULL)) {
  51.         if(DEBUG) {
  52.             printf("CryptReleaseContext failed: %d\n", GetLastError());
  53.         }
  54.         return SHA1_FAILED;
  55.     }
  56.  
  57.     if(DEBUG) {
  58.         printf("digest: %s\n", digest);
  59.     }
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment