BLAKE2 input parameters

◇◆丶佛笑我妖孽 提交于 2021-01-29 08:22:09

问题


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:

  1. Success is indicated by returning 0. So your if needs to be replaced by something like

    if(a==0)
         printf("%s", hashStr);
    else
         printf("error %d", a);
    
  2. 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:

  1. You probably want to replace the 1024 passed to the hasher by strlen(str), so it only hashes the actual string.

  2. 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.

  3. sizeof(char) == 1 by definition



来源:https://stackoverflow.com/questions/28813502/blake2-input-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!