1 (edited by rahmanikivi724 2021-10-13 06:35:16)

Topic: Diffrent digest in compare of Online hash fucntion

I used MD5 as a test function I fill buffer like this and used printf() and %x operator to show the content of md5sum and shaa256sum as a resualt.

buffer[1024]= "Hello";
MD5 is 
e358b60a19d79e6ec8ccc18c834425 
SHA is 
7040f8eeeddf088d9d42198f8cf33be1471b2c225af4c1ab496eab9b9a89f8 

but it is a mistake because the Hash of Hello is this

Md5
8b1a9953c4611296a827abf8c47804d7
SHA256
185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969

What thing would be my mistake?

Share

Re: Diffrent digest in compare of Online hash fucntion

Hello

I would be curious to see your application code, perhaps you are hashing the whole buffer, not just the string "Hello"

wolfSSL has a command line utility project, aptly named wolfCLU:
https://github.com/wolfSSL/wolfCLU

I used it below to demonstrate that the hashes match the values from other tools (openssl and sha256sum):

$ echo "Hello" > test.txt
$ wolfssl -hash md5 -in test.txt 
09f7e02f1290be211da707a266f153b3
$ openssl md5 test.txt 
MD5(test.txt)= 09f7e02f1290be211da707a266f153b3
$ wolfssl -hash sha256 -in test.txt 
66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18
$ sha256sum test.txt 
66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18  test.txt

Re: Diffrent digest in compare of Online hash fucntion

this is my code

byte md5sum[MD5_DIGEST_SIZE]; 
byte sha256sum[SHA256_DIGEST_SIZE]; 
byte buffer[1024]= "Hello"; 
Md5 md5;
Sha256 sha256;
//MD5
wc_InitMd5(&md5);
wc_Md5Update(&md5, buffer, sizeof(buffer)); 
wc_Md5Final(&md5, md5sum);
printf("MD5 is \n");
for(int i=0 ; i < MD5_DIGEST_SIZE ;i++)  printf("%x",md5sum[i]);
printf(" \n");
//SHA256
wc_InitSha256(&sha256);
wc_Sha256Update(&sha256, buffer, sizeof(buffer)); 
wc_Sha256Final(&sha256, sha256sum);
printf("SHA is \n");
for(int i=0 ; i < SHA256_DIGEST_SIZE  ;i++) printf("%x",sha256sum[i]);
printf(" \n");

Share

Re: Diffrent digest in compare of Online hash fucntion

Yes, the sizeof operator returns the actual size of the buffer, not the string. You want to use

 strlen(buffer)
#ifndef WOLFSSL_USER_SETTINGS
#include <wolfssl/options.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/ssl.h>
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/wolfcrypt/error-crypt.h>

void main() {
    byte md5sum[MD5_DIGEST_SIZE]; 
    byte sha256sum[SHA256_DIGEST_SIZE]; 
    byte buffer[1024] = "Hello";
    Md5 md5;
    Sha256 sha256;
    
    //MD5
    wc_InitMd5(&md5);
    wc_Md5Update(&md5, buffer, strlen(buffer));
    wc_Md5Final(&md5, md5sum);
    printf("MD5 is \n");
    for(int i=0 ; i < MD5_DIGEST_SIZE ;i++)  printf("%x",md5sum[i]);
        printf(" \n");
    wc_Md5Free(&md5);


    //SHA256
    wc_InitSha256(&sha256);
    wc_Sha256Update(&sha256, buffer, strlen(buffer));
    wc_Sha256Final(&sha256, sha256sum);
    printf("SHA is \n");
    for(int i=0 ; i < SHA256_DIGEST_SIZE  ;i++) printf("%x",sha256sum[i]);
        printf(" \n");
    wc_Sha256Free(&sha256);
}

MD5 is
8b1a9953c4611296a827abf8c4784d7
SHA is
185f8db32271fe25f561a6fc938b2e26436ec304eda51807d1764826381969

Re: Diffrent digest in compare of Online hash fucntion

Thanks for your help.It solved

I used this instructor because I found them in the manual CHAPTER 10: WOLFCRYPT USAGE REFERENCE
https://www.wolfssl.com/docs/wolfssl-manual/ch10/

Share

Re: Diffrent digest in compare of Online hash fucntion

Excellent. Right, if you had filled the buffer with data (used all 1024 bytes), then the sizeof operator would be correct.