From d12726b2d42ace2f31944e14769890f272d594c6 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 17 Mar 2019 21:12:11 +0100 Subject: [PATCH] adds example to compute password hash --- CMakeLists.txt | 24 +++++++++++ example/passwd/main.c | 98 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 example/passwd/main.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f19c3f3..d801f23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -181,6 +181,30 @@ target_link_libraries(wsfs-provider-app PUBLIC wsfs-provider ${EXTRA_LIBS}) target_include_directories(wsfs-provider-app PUBLIC ${EXTRA_INCLUDE_DIRS}) target_compile_options(wsfs-provider-app PUBLIC ${EXTRA_CFLAGS}) +# wsfs-passwd + +pkg_check_modules(OPENSSL REQUIRED openssl) + + +add_executable(wsfs-passwd + example/passwd/main.c +) + +target_link_libraries(wsfs-passwd PUBLIC + ${OPENSSL_LIBRARIES} +) + +target_include_directories(wsfs-passwd PUBLIC + example/passwd + ${OPENSSL_INCLUDE_DIRS} +) + +target_compile_options(wsfs-passwd PUBLIC + ${C_WARNINGS} + ${OPENSSL_CFLAGS_OTHER} +) + + endif(NOT WITHOUT_EXAMPLE) # tests diff --git a/example/passwd/main.c b/example/passwd/main.c new file mode 100644 index 0000000..0f8f971 --- /dev/null +++ b/example/passwd/main.c @@ -0,0 +1,98 @@ +#include +#include +#include + +#include +#include + +#define HASH_ALGORITHM "sha512" + +static char hex_char(unsigned char value) +{ + switch (value) + { + case 0x00: return '0'; + case 0x01: return '1'; + case 0x02: return '2'; + case 0x03: return '3'; + case 0x04: return '4'; + case 0x05: return '5'; + case 0x06: return '6'; + case 0x07: return '7'; + case 0x08: return '8'; + case 0x09: return '9'; + case 0x0a: return 'a'; + case 0x0b: return 'b'; + case 0x0c: return 'c'; + case 0x0d: return 'd'; + case 0x0e: return 'e'; + case 0x0f: return 'f'; + default: return '?'; + } +} + +static char * to_hex(unsigned char const * value, size_t length) +{ + char * result = malloc((2 * length) + 1); + if (NULL != result) + { + for (size_t i = 0, j = 0; i < length; i++, j+=2) + { + unsigned char high = (value[i] >> 4) & 0x0f; + unsigned char low = value[i] & 0x0f; + + result[j ] = hex_char(high); + result[j + 1] = hex_char(low); + } + + result[2 * length] = '\0'; + } + + return result; +} + +static char * get_password_hash( + char const * password, + char const * salt, + char * pepper) +{ + EVP_MD const * digest = EVP_get_digestbyname(HASH_ALGORITHM); + if (NULL == digest) + { + fprintf(stderr, "error: hash algorithm %s not supported\n", HASH_ALGORITHM); + exit(EXIT_FAILURE); + } + + char * result = NULL; + unsigned int hash_size = digest->md_size; + unsigned char * hash = malloc(hash_size); + + if (NULL != hash) + { + EVP_MD_CTX context; + EVP_MD_CTX_init(&context); + EVP_DigestInit_ex(&context, digest, NULL); + EVP_DigestUpdate(&context, password, strlen(password)); + EVP_DigestUpdate(&context, salt, strlen(salt)); + EVP_DigestUpdate(&context, pepper, strlen(pepper)); + EVP_DigestFinal_ex(&context, hash, &hash_size); + EVP_MD_CTX_cleanup(&context); + + result = to_hex(hash, hash_size); + free(hash); + } + + return result; +} + +int main(int argc, char * argv[]) +{ + OPENSSL_init(); + OPENSSL_add_all_algorithms_conf(); + + char * test = get_password_hash("secret", "123\n", ""); + puts(test); + free(test); + + return EXIT_SUCCESS; +} \ No newline at end of file