Skip to content

Commit 0d938a2

Browse files
admin-slushpfalcon
authored andcommitted
hmac: Port to MicroPython (missing bytes.translate(), warnings).
1 parent b90d145 commit 0d938a2

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

hmac/hmac.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
"""
55

66
import warnings as _warnings
7-
from _operator import _compare_digest as compare_digest
7+
#from _operator import _compare_digest as compare_digest
88
import hashlib as _hashlib
9+
PendingDeprecationWarning = None
10+
RuntimeWarning = None
911

1012
trans_5C = bytes((x ^ 0x5C) for x in range(256))
1113
trans_36 = bytes((x ^ 0x36) for x in range(256))
1214

15+
def translate(d, t):
16+
return b''.join([ chr(t[x]).encode('ascii') for x in d ])
17+
1318
# The size of the digests returned by HMAC depends on the underlying
1419
# hashing module used. Use digest_size from the instance of HMAC instead.
1520
digest_size = None
@@ -78,8 +83,8 @@ def __init__(self, key, msg = None, digestmod = None):
7883
key = self.digest_cons(key).digest()
7984

8085
key = key + bytes(blocksize - len(key))
81-
self.outer.update(key.translate(trans_5C))
82-
self.inner.update(key.translate(trans_36))
86+
self.outer.update(translate(key, trans_5C))
87+
self.inner.update(translate(key, trans_36))
8388
if msg is not None:
8489
self.update(msg)
8590

hmac/setup.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys
2+
# Remove current dir from sys.path, otherwise setuptools will peek up our
3+
# module instead of system.
4+
sys.path.pop(0)
5+
from setuptools import setup
6+
7+
8+
setup(name='micropython-hmac',
9+
version='0.1',
10+
description='CPython hmac module ported to MicroPython',
11+
long_description='This is a module ported from CPython standard library to be compatible with\nMicroPython interpreter. Usually, this means applying small patches for\nfeatures not supported (yet, or at all) in MicroPython. Sometimes, heavier\nchanges are required. Note that CPython modules are written with availability\nof vast resources in mind, and may not work for MicroPython ports with\nlimited heap. If you are affected by such a case, please help reimplement\nthe module from scratch.',
12+
url='https://github.com/micropython/micropython/issues/405',
13+
author='CPython Developers',
14+
author_email='[email protected]',
15+
maintainer='MicroPython Developers',
16+
maintainer_email='[email protected]',
17+
license='Python',
18+
install_requires=['micropython-hashlib'],
19+
py_modules=['hmac'])

hmac/test_hmac.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import hmac
2+
from hashlib.sha256 import sha256
3+
from hashlib.sha512 import sha512
4+
5+
msg = b'zlutoucky kun upel dabelske ody'
6+
7+
dig = hmac.new(b'1234567890', msg=msg, digestmod=sha256).hexdigest()
8+
9+
print('c735e751e36b08fb01e25794bdb15e7289b82aecdb652c8f4f72f307b39dad39')
10+
print(dig)
11+
12+
if dig != 'c735e751e36b08fb01e25794bdb15e7289b82aecdb652c8f4f72f307b39dad39':
13+
raise Exception("Error")
14+
15+
dig = hmac.new(b'1234567890', msg=msg, digestmod=sha512).hexdigest()
16+
17+
print('59942f31b6f5473fb4eb630fabf5358a49bc11d24ebc83b114b4af30d6ef47ea14b673f478586f520a0b9c53b27c8f8dd618c165ef586195bd4e98293d34df1a')
18+
print(dig)
19+
20+
if dig != '59942f31b6f5473fb4eb630fabf5358a49bc11d24ebc83b114b4af30d6ef47ea14b673f478586f520a0b9c53b27c8f8dd618c165ef586195bd4e98293d34df1a':
21+
raise Exception("Error")
22+

0 commit comments

Comments
 (0)