-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathossl_pbkdf2.c
61 lines (55 loc) · 1.99 KB
/
ossl_pbkdf2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_OPENSSL_EVP_H
#include <openssl/evp.h>
#include <openssl/sha.h>
#endif
#include <stdint.h>
#include <string.h>
#include <stdio.h>
void PBKDF2_HMAC_SHA512(const char* pass, const unsigned char* salt, int32_t iterations, uint32_t outputBytes, char* hexResult)
{
unsigned int i;
unsigned char digest[outputBytes];
PKCS5_PBKDF2_HMAC(pass, strlen(pass), salt, strlen((const char*)salt), iterations, EVP_sha512(), outputBytes, digest);
for (i = 0; i < sizeof(digest); i++)
sprintf(hexResult + (i * 2), "%02x", 255 & digest[i]);
}
void PBKDF2_HMAC_SHA1(const char* pass, const unsigned char* salt, int32_t iterations, uint32_t outputBytes, char* hexResult)
{
unsigned int i;
unsigned char digest[outputBytes];
PKCS5_PBKDF2_HMAC(pass, strlen(pass), salt, strlen((const char*)salt), iterations, EVP_sha1(), outputBytes, digest);
for (i = 0; i < sizeof(digest); i++)
sprintf(hexResult + (i * 2), "%02x", 255 & digest[i]);
}
void PBKDF2_HMAC_MD5(const char* pass, const unsigned char* salt, int32_t iterations, uint32_t outputBytes, char* hexResult)
{
unsigned int i;
unsigned char digest[outputBytes];
PKCS5_PBKDF2_HMAC(pass, strlen(pass), salt, strlen((const char*)salt), iterations, EVP_md5(), outputBytes, digest);
for (i = 0; i < sizeof(digest); i++)
sprintf(hexResult + (i * 2), "%02x", 255 & digest[i]);
}
void usage()
{
fprintf(stderr, "Usage: pbkdf2 pwd salt iter olen\n");
exit(1);
}
int main(int argc, char *argv[])
{
if (argc != 5)
usage();
int olen = atol(argv[4])/8;
char* obuf = (char*)malloc(1+2*olen);
OPENSSL_init();
PBKDF2_HMAC_SHA1(argv[1], (unsigned char*)argv[2], atol(argv[3]), olen, obuf);
printf("PBKDF2(SHA1 , ...) = %s\n", obuf);
PBKDF2_HMAC_SHA512(argv[1], (unsigned char*)argv[2], atol(argv[3]), olen, obuf);
printf("PBKDF2(SHA512, ...) = %s\n", obuf);
PBKDF2_HMAC_MD5(argv[1], (unsigned char*)argv[2], atol(argv[3]), olen, obuf);
printf("PBKDF2(MD5 , ...) = %s\n", obuf);
free(obuf);
return 0;
}