Page 1 of 1

md5 support

Posted: Wed May 31, 2017 3:29 am
by Rdelarosa
hello friends
hashlib.md5() and uhashlib.md5() not work,
show "object not have md5 atributte".
uhashlib.sha1() work fine,
I need a firmware whit md5 active,
thank you for advance
best regards

Re: md5 support

Posted: Wed May 31, 2017 4:28 am
by dhylands
Nobody has yet ported md5 support to MicroPython which is why its missing.

Re: md5 support

Posted: Wed May 31, 2017 4:57 am
by Rdelarosa
[quote="dhylands"]Nobody has yet ported md5 support to MicroPython which is why its missing.[/quote]


There are many devices that use md5 to commune with each other.
In any case, is there any solution for me?

Re: md5 support

Posted: Wed May 31, 2017 5:07 am
by mcauser
I came across this: https://github.com/drcoms/client-microp ... dev/md5.py

Almost works:

Code: Select all

import md5
md5.md5('foo')
TypeError: function missing 1 required positional arguments
Error in to_bytes() and from_bytes() in:

Code: Select all

message += orig_len_in_bits.to_bytes(8)
to_rotate = a + f + constants[i] + int.from_bytes(chunk[4*g:4*g+4])
https://micropython.org/resources/docs/ ... s.html#int

"In MicroPython, `byteorder` parameter must be positional (this is compatible with CPython)."

Tried changing it to to_bytes(8,'big'). Throws a NotImplementedError
Tried to_bytes(8,'little') and it works, but the hash does not match.

>>> md5('foo')
70470640994256801257106554029113479893

https://github.com/micropython/micropyt ... int.c#L435
It seems to_bytes() currently only supports little endian and assumes signed=False.

$ echo -n foo | md5
acbd18db4cc2f85cedef654fccc4a4d8

http://www.md5.cz/
source: 'foo'
hash: acbd18db4cc2f85cedef654fccc4a4d8

https://blueimp.github.io/JavaScript-MD5/
source: 'foo'
hash: acbd18db4cc2f85cedef654fccc4a4d8

Re: md5 support

Posted: Wed May 31, 2017 5:28 am
by mcauser
Docs seem to be incorrect:
http://docs.micropython.org/en/latest/e ... shlib.html
http://docs.micropython.org/en/latest/w ... e-uhashlib
http://docs.micropython.org/en/latest/p ... shlib.html

uhashlib.md5() does not exist.

"MD5 - A legacy algorithm, not considered cryptographically secure. Only selected boards, targeting interoperatibility with legacy applications, will offer this."

Not sure which selected boards offer it, if any.

Git history shows md5 was never added to uhashlib:
https://github.com/micropython/micropyt ... uhashlib.c

It was added to cc3200 port, but commented out:
https://github.com/micropython/micropyt ... 6578b363e0

Re: md5 support

Posted: Wed May 31, 2017 6:07 am
by Rdelarosa
thank you mcauser,

Find a script made in phyton2 unfortunately I do not know how to implement it in micropython, which is also based on python3, someone give me a hand?
https://github.com/bbc/UCMythTV/blob/ma ... r/md5py.py
best regards

Re: md5 support

Posted: Thu Jun 01, 2017 7:05 am
by pythoncoder
A starting point would be to get rid of the long integers: Python3 supports arbitrary precision integers. So replace (say) 0xC4AC5665L with 0xC4AC5665, and remove the long() functions.

You'll need to install copy from micropython-lib and use ustruct in place of struct, checking that the latter has the necessary functionality.

Those observations are from a quick glance at the code: there are bound to be other issues. Porting a substantial piece of code like that involves some work and considerable testing.

Re: md5 support

Posted: Mon Jun 05, 2017 3:25 am
by Rdelarosa
Thanks to everything for contribution and help, I will work on this and then tell you news

Re: md5 support

Posted: Fri Jul 20, 2018 4:27 pm
by rdagger
Any progress on MD5 support?

Re: md5 support

Posted: Sun Oct 21, 2018 9:42 am
by lemariva
It is an old post, but I didn't find a working version for md5. Then, I've modified the code published here (it is based on the https://rosettacode.org/wiki/MD5/Implementation#Python) and it works:

https://github.com/lemariva/ESP32MicroP ... ter/md5.py

The problem was the precision of:
constants = [int(abs(math.sin(i+1)) * 2**32) & 0xFFFFFFFF for i in range(64)]
The values differ between the matrix calculated using micropython on the esp32 and python on a x64.

Use as follows:

>>> import md5
>>> md5.digest('foo')
'acbd18db4cc2f85cedef654fccc4a4d8