md5 support

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Rdelarosa
Posts: 4
Joined: Sun May 28, 2017 5:12 pm

md5 support

Post by Rdelarosa » Wed May 31, 2017 3:29 am

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

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: md5 support

Post by dhylands » Wed May 31, 2017 4:28 am

Nobody has yet ported md5 support to MicroPython which is why its missing.

Rdelarosa
Posts: 4
Joined: Sun May 28, 2017 5:12 pm

Re: md5 support

Post by Rdelarosa » Wed May 31, 2017 4:57 am

[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?

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: md5 support

Post by mcauser » Wed May 31, 2017 5:07 am

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

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: md5 support

Post by mcauser » Wed May 31, 2017 5:28 am

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

Rdelarosa
Posts: 4
Joined: Sun May 28, 2017 5:12 pm

Re: md5 support

Post by Rdelarosa » Wed May 31, 2017 6:07 am

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: md5 support

Post by pythoncoder » Thu Jun 01, 2017 7:05 am

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.
Peter Hinch
Index to my micropython libraries.

Rdelarosa
Posts: 4
Joined: Sun May 28, 2017 5:12 pm

Re: md5 support

Post by Rdelarosa » Mon Jun 05, 2017 3:25 am

Thanks to everything for contribution and help, I will work on this and then tell you news

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: md5 support

Post by rdagger » Fri Jul 20, 2018 4:27 pm

Any progress on MD5 support?

User avatar
lemariva
Posts: 5
Joined: Sun Oct 21, 2018 9:33 am
Location: Hannover, Germany
Contact:

Re: md5 support

Post by lemariva » Sun Oct 21, 2018 9:42 am

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

Post Reply