Update password hashing to use evp functions
This commit is contained in:
@@ -3,33 +3,47 @@
|
||||
#include <stdlib.h>
|
||||
#include <sqlite3.h>
|
||||
#include <ctype.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/evp.h>
|
||||
|
||||
char *hash_sha256(char *pass, char *salt) {
|
||||
cchar *hash_sha256(char *pass, char *salt) {
|
||||
char *buffer = (char *)malloc(strlen(pass) + strlen(salt) + 1);
|
||||
char *shash = (char *)malloc(66);
|
||||
unsigned char hash[SHA256_DIGEST_LENGTH];
|
||||
char *shash = NULL;
|
||||
unsigned char hash[EVP_MAX_MD_SIZE];
|
||||
unsigned int length_of_hash = 0;
|
||||
int i;
|
||||
|
||||
if (!buffer) {
|
||||
printf("Out of memory!");
|
||||
fprintf(stderr, "Out of memory!\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
sprintf(buffer, "%s%s", pass, salt);
|
||||
|
||||
|
||||
SHA256_CTX sha256;
|
||||
SHA256_Init(&sha256);
|
||||
SHA256_Update(&sha256, buffer, strlen(buffer));
|
||||
SHA256_Final(hash, &sha256);
|
||||
int i = 0;
|
||||
for(i = 0; i < SHA256_DIGEST_LENGTH; i++) {
|
||||
sprintf(shash + (i * 2), "%02x", hash[i]);
|
||||
EVP_MD_CTX *context = EVP_MD_CTX_new();
|
||||
|
||||
if (context != NULL) {
|
||||
if(EVP_DigestInit_ex(context, EVP_sha256(), NULL)) {
|
||||
if(EVP_DigestUpdate(context, buffer, strlen(buffer))) {
|
||||
if(EVP_DigestFinal_ex(context, hash, &length_of_hash)) {
|
||||
|
||||
shash = (char *)malloc(length_of_hash * 2 + 1);
|
||||
for(i = 0; i < length_of_hash; i++) {
|
||||
sprintf(shash + (i * 2), "%02x", (int)hash[i]);
|
||||
|
||||
}
|
||||
EVP_MD_CTX_free(context);
|
||||
free(buffer);
|
||||
return shash;
|
||||
}
|
||||
}
|
||||
}
|
||||
EVP_MD_CTX_free(context);
|
||||
}
|
||||
shash[64] = 0;
|
||||
|
||||
free(buffer);
|
||||
return shash;
|
||||
fprintf(stderr, "Error creating hash!\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
void gen_salt(char **s) {
|
||||
|
Reference in New Issue
Block a user