Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. /*
  2. * Example: PBKDF2-SHA256 in libgcrypt
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <openssl/evp.h>
  7. #include <openssl/rand.h>
  8.  
  9. /* Print buffer in HEX with optional separator */
  10. static void hexprint(const char *d, int n, const char *sep)
  11. {
  12. int i;
  13.  
  14. for (i = 0; i < n; i++)
  15. printf("%02hhx%s", (const char)d[i], sep);
  16. printf("\n");
  17. }
  18.  
  19. static void print_out(const char *hash_type, const void *out, unsigned int out_len, const int iteration, const char *password, const char *salt, const double c)
  20. {
  21. printf("Derived key using %i vector. %s (P=%s, S=%s, c=%d, dkLen=%i)", iteration, hash_type, password, salt, c, out_len);
  22. hexprint((const char*)out, out_len, " ");
  23. }
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27. const char *passwords[] = {"password", "password", "password", "password", "passwordPASSWORDpassword", "pass\0word"};
  28. const char *salts[] = {"salt", "salt", "salt", "salt", "saltSALTsaltSALTsaltSALTsaltSALTsalt", "sa\0lt"};
  29. const double iterations[] = {1, 2, 4096, 16777216, 4096, 4096};
  30. const int out_lens[] = {20, 20, 20, 20, 25, 16};
  31.  
  32. /* Library initialization */
  33. OpenSSL_add_all_algorithms();
  34. printf("OpenSSL (%s):\n", SSLeay_version(SSLEAY_VERSION));
  35.  
  36. printf("PBKDF2-SHA1 using OpenSSL:\n");
  37.  
  38. int i;
  39. for (i = 0; i < 6; i++ ) {
  40. unsigned char key[iterations[i]];
  41. if (!PKCS5_PBKDF2_HMAC(passwords[i], strlen(passwords[i]), salts[i], sizeof(salts[i]),
  42. iterations[i], EVP_sha1(), sizeof(key), key));
  43. return 2;
  44.  
  45. print_out("PBKDF2-SHA1", key, sizeof(key), i, passwords[i], salts[i], iterations[i]);
  46. }
  47.  
  48. for (i = 0; i < 6; i++ ) {
  49. unsigned char key[32];
  50. if (!PKCS5_PBKDF2_HMAC(passwords[i], strlen(passwords[i]), salts[i], sizeof(salts[i]),
  51. iterations[i], EVP_sha256(), sizeof(key), key));
  52. return 2;
  53.  
  54. print_out("PBKDF2-SHA256", key, sizeof(key), i, passwords[i], salts[i], iterations[i]);
  55. }
  56.  
  57.  
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement