This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to use SHA256 and MD5?

Hi. I'm using nRF52832, SDK13.0.0.

I want to use SHA256 as below.

output32Byte = SHA256[input32Byte];

Which function or example can I refer to?

And also I want to use MD5 as below.

output16Byte = MD5[input32Byte];

Which function or example can I refer to?

I made my code as below.

NRF_CRYPTO_HASH_CREATE(init_packet_hash, SHA256);
NRF_CRYPTO_HASH_CREATE(init_packet_md5, MD5);

const nrf_crypto_hash_info_t hash_info_sha256 =
{
    .hash_type = NRF_CRYPTO_HASH_TYPE_SHA256,
    .endian_type = NRF_CRYPTO_ENDIAN_LE
};

const nrf_crypto_hash_info_t hash_info_MD5 =
{
    .hash_type = NRF_CRYPTO_HASH_TYPE_MD5,
    .endian_type = NRF_CRYPTO_ENDIAN_LE
};

......

......

 uint8_t p_init_cmd[32];
 uint32_t init_cmd_len = 32;

 for(int i=0;i<32;i++) p_init_cmd[i]=(i%10);

 SEGGER_RTT_printf_log_Hex(0, "p_init_cmd     : ", p_init_cmd, 32);

 err_code = nrf_crypto_hash_compute(hash_info_sha256, p_init_cmd, init_cmd_len, &init_packet_hash);

 SEGGER_RTT_printf_log(0, "hash_info_sha256, err_code=0x%x\n", err_code);
 SEGGER_RTT_printf_log_Hex(0, "init_packet_hash     : ", init_packet_hash.p_value, 32);
 
 err_code = nrf_crypto_hash_compute(hash_info_MD5, p_init_cmd, init_cmd_len, &init_packet_md5);

 SEGGER_RTT_printf_log(0, "hash_info_MD5, err_code=0x%x\n", err_code);
 SEGGER_RTT_printf_log_Hex(0, "init_packet_hash     : ", init_packet_md5.p_value, 16);

And my log is

p_init_cmd     : 0001020304050607080900010203040506070809000102030405060708090001
hash_info_sha256, err_code=0x0
init_packet_hash     : E8352A8521130C3C6564D205FA78C236F9E2AE133FC191D0CA70E35D4AACB1DC
hash_info_MD5, err_code=0x6

I think MD5 didn't work because it only supports SHA256. Is there any way to use MD5?

I should use MD5.

And I think SHA256 has a problem.

input data is "0001020304050607080900010203040506070809000102030405060708090001"

and output data is "E8352A8521130C3C6564D205FA78C236F9E2AE133FC191D0CA70E35D4AACB1DC"

But SHA-256 Calculator says output data is "94d785502c800d13c5ee22d0f4e5e6d754c29f7ef4a8ff540aa56bfd5da468b5"

I refered to http://www.xorbin.com/tools/sha256-hash-calculator

I don't know why the result is not good.

I need your help.

Related