← index #8017Issue #369
Off-topic · high · value 0.382
QUERY · ISSUE

SHA512 and hashlib port

openby willianpaixaoopened 2021-11-20updated 2024-10-01
enhancement

At the moment, Micropython supports SHA1 and SHA256.

I would like to suggest addition of more algorithms, in specific SHA512.
It's already implemented in micropython-lib and of course at cpython itself.

CANDIDATE · ISSUE

hmac code not compatible with hashlib/uhashlib implementations

closedby tmmorinopened 2020-02-03updated 2022-08-08

I noticed that micropython's hmac implementation relies on the existence of .copy() on instances of shaXX, which works fine with the implementation provided in hashlib._sha224/256/384/512, but does not work with the implementations that hashlib auto-loads from uhashlib when uhashlib is present (since this change).

I tested recent versions micropython-lib master and micropython 1.12).

$ cat test2.py 
import hmac
from hashlib import sha256
print(hmac.new(b'test', msg=b'test', digestmod=sha256).hexdigest())
$ micropython test2.py 
Warning: No block_size attribute on given digest object; Assuming 64.
Traceback (most recent call last):
  File "test2.py", line 4, in <module>
  File "/home/treizh/.micropython/lib/hmac.py", line 137, in hexdigest
  File "/home/treizh/.micropython/lib/hmac.py", line 120, in _current
AttributeError: 'sha256' object has no attribute 'copy'

Forcing the use of the implementation under hashlib._sha256 works though:

$ cat test.py 
import hmac
from hashlib._sha256 import sha256
print(hmac.new(b'test', msg=b'test', digestmod=sha256).hexdigest())
$ micropython test.py 
88cd2108b5347d973cf39cdf9053d7dd42704876d8c9a9bd8e2d168259d3ddf7

(this workaround unfortunately does not work for SHA1 since SHA1 has no implementation in hashlib)

I presume that whether hmac should be implemented without using relying on copy(), or at least should not autoload hashlib (this last solution would leave HMAC SHA1 unimplemented).

6 comments
tmmorin · 2020-02-03

@peter-conalgo: perhaps are you interested in this issue ?

tmmorin · 2020-02-03

Thanks @peter-conalgo. I'll try your code !

cositotito · 2020-04-24

hello the code error digest_size.

https://github.com/micropython/micropython-lib/issues/384

jimmo · 2022-08-08

See https://github.com/micropython/micropython-lib/pull/515 for a fix for this (sorry this has taken so long)

dpgeorge · 2022-08-08

Fixed by f95260d7e3c123a8b1907ded095e4c25e4251085

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied