问题
Being a newbie, I was reading papers on the recent crop of hash functions and BLAKE2 intrigued me. Then I wanted to play around with the "blake2s" code in the code package.
If I were to implement a simple string hasher, I could understand that there are built-in variables for a key and salt.
But I couldn't figure out how to provide a string or file as input and collect the hash as input. Which variable(s) are responsible for the message and the digest?
Maybe I'm doing things wrong but the following doesn't print any output.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "blake2s.h"
#include "blake2s.c"
#define SIZE 1024
int main(){
unsigned char *str, *hash;
str = malloc(SIZE * sizeof *str);
hash = malloc(32 * sizeof *str);
printf("> ");
scanf("%s", str);
int a = blake2s(hash, str, NULL, 32, 1024, 0);
if(a)
printf("%s", hash);
return 0;
}
回答1:
Your program has two issues that directly relate to printing the hash:
Success is indicated by returning
0
. So yourif
needs to be replaced by something likeif(a==0) printf("%s", hashStr); else printf("error %d", a);
The hash is raw binary and can even contain
\0
bytes. You should apply hex or Base64 encoding before printing it.
And a few other bugs and stylistic issues:
You probably want to replace the 1024 passed to the hasher by
strlen(str)
, so it only hashes the actual string.Your program suffers from a buffer overflow if the user enters more than 1024 bytes. No big issue in a test program, but something you should fix in a proper implementation since it'd be a security hole.
sizeof(char) == 1
by definition
来源:https://stackoverflow.com/questions/28813502/blake2-input-parameters