P-H-C / phc-winner-argon2

The password hash Argon2, winner of PHC
Other
4.78k stars 406 forks source link

Updating README.md with provided example for encoded hash for the ease of storing in database #370

Open th3r0b0t opened 8 months ago

th3r0b0t commented 8 months ago

Hello! First of all, I wanna show my appreciation and respects for the nice work you folks have done; Secondly, to cut things short, I had to go through the run() func in run.c file to see how the command line utility used argon2i_hash_encoded() function, since the encodedlen parameter was a little tricky and hasn't been documented in the comments above argon2i_hash_encoded()! I think it would be nice to provide an example in readme file for encoded function too! Here is an example of creating Argon2_i encoded hash:

#include "argon2.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define HASHLEN 128
#define SALTLEN 32
#define PWD "Good strong password"

int main(void)
{
    uint8_t salt[SALTLEN];
    memset( salt, 0x00, SALTLEN );

    uint8_t *pwd = (uint8_t *)strdup(PWD);
    uint32_t pwdlen = strlen((char *)pwd);

    uint32_t t_cost = 16;            // 16-pass computation (1 second or so)
    uint32_t m_cost = (1<<17);      // 128 mebibytes memory usage
    uint32_t parallelism = 4;       // number of threads and lanes

    char * encoded = NULL;
    size_t encodedlen = argon2_encodedlen(t_cost, m_cost, parallelism, SALTLEN, HASHLEN, Argon2_i);    // Argon2_i == 1
    encoded = malloc(encodedlen + 1);

    // high-level API
    argon2i_hash_encoded(t_cost, m_cost, parallelism, pwd, pwdlen, salt, SALTLEN, HASHLEN, encoded, encodedlen);
    free(pwd);

    printf( "%.*s\n", (int)encodedlen, encoded );

    return 0;
}