14. SHA256 Routine Explanation

14. SHA256 Routine Explanation#

1. Overview#

The K230 comes with a built-in SHA256 acceleration unit, which supports efficient SHA256 hash computation through the uhashlib module. SHA256 is a widely used hashing algorithm, commonly employed in scenarios such as data integrity verification and digital signatures.

2. Example#

The following example demonstrates how to use the SHA256 functionality of the K230 for hash computation:

import uhashlib

print('###################### SHA256 Test ##############################')
print('********************** Test-1: Only Call update() Once ******************') 
# Initialize the sha256 object
obj = uhashlib.sha256() 
# Input message
msg = b'\x45\x11\x01\x25\x0e\xc6\xf2\x66\x52\x24\x9d\x59\xdc\x97\x4b\x73\x61\xd5\x71\xa8\x10\x1c\xdf\xd3\x6a\xba\x3b\x58\x54\xd3\xae\x08\x6b\x5f\xdd\x45\x97\x72\x1b\x66\xe3\xc0\xdc\x5d\x8c\x60\x6d\x96\x57\xd0\xe3\x23\x28\x3a\x52\x17\xd1\xf5\x3f\x2f\x28\x4f\x57\xb8' 
# Standard hash value
dgst0 = b'\x1a\xaa\xf9\x28\x5a\xf9\x45\xb8\xa9\x7c\xf1\x4f\x86\x9b\x18\x90\x14\xc3\x84\xf3\xc7\xc2\xb7\xd2\xdf\x8a\x97\x13\xbf\xfe\x0b\xf1' 
# Update the message to the hardware IP
obj.update(msg) 
# Compute the hash value
dgst = obj.digest()
print(dgst0 == dgst) 
# print(binascii.hexlify(dgst))
print('********************** Test-2: Call update() Twice ******************')
dgst0 = b'\x93\x6a\x18\x5c\xaa\xa2\x66\xbb\x9c\xbe\x98\x1e\x9e\x05\xcb\x78\xcd\x73\x2b\x0b\x32\x80\xeb\x94\x44\x12\xbb\x6f\x8f\x8f\x07\xaf'
obj = uhashlib.sha256() 
# Update the message to the hardware multiple times
obj.update(b'hello')
obj.update(b'world')
dgst = obj.digest()
print(dgst0 == dgst) 
# print('********************** Test-3: Call digest() Twice ******************') 
# dgst0 = b'\x93\x6a\x18\x5c\xaa\xa2\x66\xbb\x9c\xbe\x98\x1e\x9e\x05\xcb\x78\xcd\x73\x2b\x0b\x32\x80\xeb\x94\x44\x12\xbb\x6f\x8f\x8f\x07\xaf' 
# obj = uhashlib.sha256() 
# obj.update(b'hello') 
# obj.update(b'world') 
# dgst = obj.digest() 
# dgst1 = obj.digest() 
# print(dgst0 == dgst) 
# print(dgst == dgst1)

This routine demonstrates how to use the SHA256 acceleration unit of the K230 for hash computation. Whether updating the message once or multiple times, uhashlib provides a simple and clear interface, making it convenient for developers to efficiently implement data integrity verification and other functions.

Note

For detailed interface information on the Hashlib module, please refer to the API documentation.

Comments list

Comments list

Comments
Log in