Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Example: PBKDF2-SHA256 in libgcrypt
- */
- #include <stdio.h>
- #include <string.h>
- #include <openssl/evp.h>
- #include <openssl/rand.h>
- /* Print buffer in HEX with optional separator */
- static void hexprint(const char *d, int n, const char *sep)
- {
- int i;
- for (i = 0; i < n; i++)
- printf("%02hhx%s", (const char)d[i], sep);
- printf("\n");
- }
- 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)
- {
- printf("Derived key using %i vector. %s (P=%s, S=%s, c=%d, dkLen=%i)", iteration, hash_type, password, salt, c, out_len);
- hexprint((const char*)out, out_len, " ");
- }
- int main(int argc, char *argv[])
- {
- const char *passwords[] = {"password", "password", "password", "password", "passwordPASSWORDpassword", "pass\0word"};
- const char *salts[] = {"salt", "salt", "salt", "salt", "saltSALTsaltSALTsaltSALTsaltSALTsalt", "sa\0lt"};
- const double iterations[] = {1, 2, 4096, 16777216, 4096, 4096};
- const int out_lens[] = {20, 20, 20, 20, 25, 16};
- /* Library initialization */
- OpenSSL_add_all_algorithms();
- printf("OpenSSL (%s):\n", SSLeay_version(SSLEAY_VERSION));
- printf("PBKDF2-SHA1 using OpenSSL:\n");
- int i;
- for (i = 0; i < 6; i++ ) {
- unsigned char key[iterations[i]];
- if (!PKCS5_PBKDF2_HMAC(passwords[i], strlen(passwords[i]), salts[i], sizeof(salts[i]),
- iterations[i], EVP_sha1(), sizeof(key), key));
- return 2;
- print_out("PBKDF2-SHA1", key, sizeof(key), i, passwords[i], salts[i], iterations[i]);
- }
- for (i = 0; i < 6; i++ ) {
- unsigned char key[32];
- if (!PKCS5_PBKDF2_HMAC(passwords[i], strlen(passwords[i]), salts[i], sizeof(salts[i]),
- iterations[i], EVP_sha256(), sizeof(key), key));
- return 2;
- print_out("PBKDF2-SHA256", key, sizeof(key), i, passwords[i], salts[i], iterations[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement