summaryrefslogtreecommitdiffstats
path: root/common/passwd.c
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2009-01-02 17:41:48 +0100
committerDavid Sommerseth <dazo@users.sourceforge.net>2009-01-02 17:41:48 +0100
commit241b14d771d247127508cf7b20f833b9dbe0abda (patch)
treee43906492727d02827a21147f380ffb670cfc62a /common/passwd.c
parenta2ca4fdf6c6eee2d229c8cb7770f37accd467d93 (diff)
downloadeurephia-241b14d771d247127508cf7b20f833b9dbe0abda.tar.gz
eurephia-241b14d771d247127508cf7b20f833b9dbe0abda.tar.xz
eurephia-241b14d771d247127508cf7b20f833b9dbe0abda.zip
Prepared passwdhash function to allow salting and be prepared for other hashing algorithms
Diffstat (limited to 'common/passwd.c')
-rw-r--r--common/passwd.c59
1 files changed, 34 insertions, 25 deletions
diff --git a/common/passwd.c b/common/passwd.c
index c77fccb..101a1d6 100644
--- a/common/passwd.c
+++ b/common/passwd.c
@@ -24,12 +24,14 @@
#include <stdint.h>
#include "eurephia_nullsafe.h"
+#include "passwd.h"
#include "sha512.h"
-char *_passwdhash(const char *pwd, const char *file, const int line) {
+
+char *passwdhash(pwdHASH hashalgo, const char *salt, const char *pwd) {
SHA512Context sha;
uint8_t sha_res[SHA512_HASH_SIZE];
- char *ret = NULL, *ptr = NULL;
+ char *ret = NULL, *ptr = NULL, *tmp = NULL;
unsigned len = 0, i;
len = strlen_nullsafe(pwd);
@@ -37,31 +39,38 @@ char *_passwdhash(const char *pwd, const char *file, const int line) {
return NULL;
}
- // Generate SHA512 hash of password
- memset(&sha, 0, sizeof(SHA512Context));
- memset(&sha_res, 0, sizeof(sha_res));
- SHA512Init(&sha);
- SHA512Update(&sha, pwd, len);
- SHA512Final(&sha, sha_res);
-
- // Allocate memory for the return buffer
-#ifdef MEMWATCH
- ret = (char *) mwMalloc((SHA512_HASH_SIZE*2)+3, file, line);
-#else
- ret = (char *) malloc((SHA512_HASH_SIZE*2)+3);
-#endif
- memset(ret, 0,(SHA512_HASH_SIZE*2)+3);
- ptr = ret;
+ switch( hashalgo ) {
+ case pwdSHA512:
+ if( salt != NULL ) {
+ tmp = (char *) malloc(strlen_nullsafe(salt) + len + 2);
+ memset(tmp, 0, strlen_nullsafe(salt) + len + 2);
+ sprintf(tmp, "%s%s", pwd, salt);
+ } else {
+ tmp = strdup_nullsafe(pwd);
+ }
+ // Generate SHA512 hash of password
+ memset(&sha, 0, sizeof(SHA512Context));
+ memset(&sha_res, 0, sizeof(sha_res));
+ SHA512Init(&sha);
+ SHA512Update(&sha, tmp, len);
+ SHA512Final(&sha, sha_res);
- // Generate a readable string of the hash
- for( i = 0; i < SHA512_HASH_SIZE; i++ ) {
- sprintf(ptr, "%02x", sha_res[i]);
- ptr += 2;
- }
+ // Allocate memory for the return buffer
+ ret = (char *) malloc((SHA512_HASH_SIZE*2)+3);
+ memset(ret, 0,(SHA512_HASH_SIZE*2)+3);
+ ptr = ret;
- // Cleanup - remove hash data from memory
- memset(&sha, 0, sizeof(SHA512Context));
- memset(&sha_res, 0, sizeof(sha_res));
+ // Generate a readable string of the hash
+ for( i = 0; i < SHA512_HASH_SIZE; i++ ) {
+ sprintf(ptr, "%02x", sha_res[i]);
+ ptr += 2;
+ }
+ // Cleanup - remove hash data from memory
+ memset(&sha, 0, sizeof(SHA512Context));
+ memset(&sha_res, 0, sizeof(sha_res));
+ free_nullsafe(tmp);
+ break;
+ }
return ret;
}