-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace custom PBKDF2 implementation with OpenSSL's PKCS5_P…
…BKDF2_HMAC for improved security and maintainability Signed-off-by: Dengfeng Liu <liudf0716@gmail.com>
- Loading branch information
Showing
3 changed files
with
29 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
#include <stdio.h> | ||
#include <openssl/evp.h> | ||
#include <openssl/sha.h> | ||
#include <string.h> | ||
#include <stdint.h> | ||
#include "fastpbkdf2.h" | ||
#include <stdio.h> | ||
|
||
void print_hex(const uint8_t *data, size_t len) { | ||
for (size_t i = 0; i < len; i++) { | ||
printf("%02x", data[i]); | ||
} | ||
printf("\n"); | ||
void pbkdf2_key(const uint8_t *password, size_t password_len, | ||
const uint8_t *salt, size_t salt_len, | ||
uint32_t iterations, uint8_t *out, size_t out_len) { | ||
PKCS5_PBKDF2_HMAC((const char *)password, password_len, | ||
salt, salt_len, iterations, | ||
EVP_sha1(), out_len, out); | ||
} | ||
|
||
void test_fastpbkdf2_hmac_sha1() { | ||
const uint8_t password[] = "password"; | ||
const uint8_t salt[] = "salt"; | ||
uint32_t iterations = 1000; | ||
uint8_t output[20]; // SHA-1 produces a 20-byte hash | ||
int main() { | ||
const char *password = "password"; | ||
const uint8_t salt[] = "saltsalt"; | ||
uint8_t key[16]; // size of the derived key | ||
|
||
fastpbkdf2_hmac_sha1(password, strlen((const char *)password), salt, strlen((const char *)salt), iterations, output, sizeof(output)); | ||
pbkdf2_key((const uint8_t *)password, strlen(password), | ||
salt, sizeof(salt) - 1, 1000, key, sizeof(key)); | ||
|
||
printf("PBKDF2-HMAC-SHA1: "); | ||
print_hex(output, sizeof(output)); | ||
} | ||
printf("Derived key: "); | ||
for (size_t i = 0; i < sizeof(key); i++) { | ||
printf("%02x", key[i]); | ||
} | ||
printf("\n"); | ||
|
||
int main() { | ||
test_fastpbkdf2_hmac_sha1(); | ||
return 0; | ||
} |