Simple program to get sha256 hash

Paolo Bolzoni paolo.bolzoni.brown at
Thu Mar 16 23:47:01 CET 2017

Dear list,

Today I wanted to play a bit with libgcrypt and I wanted
to make a small program to return the sha256 hash of a
string passed as argument.

The program compiles, links, and gives an output.
However, the output is different than what I get using
sha256sum from coreutils!

Here is what I see in my shell:
$ gcc -o ssha  ./hash.cpp -lgcrypt
$ ./ssha blablabla
$ sha256sum <<< 'blablabla'
a5edca3a5b8fb54ae61d236a5274626ba6a38781573e02202000158faa707191  -

hash.cpp contains the code I copied in the bottom of the email.

What am I missing? Isn't expected to have the same hash code?

Yours faithfully,

---------- >8
#include <stdio.h>
#include <gcrypt.h>

int main(int argc, char** argv) {
    if (argc < 2) return 1;

    gcry_md_hd_t context;
    gcry_md_open(&context, GCRY_MD_SHA256, 0);

    char* c = argv[1];
    while (*c != '\0') {
        gcry_md_putc(context, *c); }

    unsigned char* csha  = gcry_md_read(context, GCRY_MD_SHA256);
    for (int i = 0; i < 32; ++i) {
        printf("%02x", csha[i]); }

    gcry_md_close(context); }

More information about the Gnupg-users mailing list