hmac: Port to MicroPython (missing bytes.translate(), warnings).

pull/21/merge
slush0 2015-02-17 23:07:19 +01:00 zatwierdzone przez Paul Sokolovsky
rodzic b90d145a1d
commit 0d938a20a3
3 zmienionych plików z 49 dodań i 3 usunięć

Wyświetl plik

@ -4,12 +4,17 @@ Implements the HMAC algorithm as described by RFC 2104.
"""
import warnings as _warnings
from _operator import _compare_digest as compare_digest
#from _operator import _compare_digest as compare_digest
import hashlib as _hashlib
PendingDeprecationWarning = None
RuntimeWarning = None
trans_5C = bytes((x ^ 0x5C) for x in range(256))
trans_36 = bytes((x ^ 0x36) for x in range(256))
def translate(d, t):
return b''.join([ chr(t[x]).encode('ascii') for x in d ])
# The size of the digests returned by HMAC depends on the underlying
# hashing module used. Use digest_size from the instance of HMAC instead.
digest_size = None
@ -78,8 +83,8 @@ class HMAC:
key = self.digest_cons(key).digest()
key = key + bytes(blocksize - len(key))
self.outer.update(key.translate(trans_5C))
self.inner.update(key.translate(trans_36))
self.outer.update(translate(key, trans_5C))
self.inner.update(translate(key, trans_36))
if msg is not None:
self.update(msg)

19
hmac/setup.py 100644
Wyświetl plik

@ -0,0 +1,19 @@
import sys
# Remove current dir from sys.path, otherwise setuptools will peek up our
# module instead of system.
sys.path.pop(0)
from setuptools import setup
setup(name='micropython-hmac',
version='0.1',
description='CPython hmac module ported to MicroPython',
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.',
url='https://github.com/micropython/micropython/issues/405',
author='CPython Developers',
author_email='python-dev@python.org',
maintainer='MicroPython Developers',
maintainer_email='micro-python@googlegroups.com',
license='Python',
install_requires=['micropython-hashlib'],
py_modules=['hmac'])

22
hmac/test_hmac.py 100644
Wyświetl plik

@ -0,0 +1,22 @@
import hmac
from hashlib.sha256 import sha256
from hashlib.sha512 import sha512
msg = b'zlutoucky kun upel dabelske ody'
dig = hmac.new(b'1234567890', msg=msg, digestmod=sha256).hexdigest()
print('c735e751e36b08fb01e25794bdb15e7289b82aecdb652c8f4f72f307b39dad39')
print(dig)
if dig != 'c735e751e36b08fb01e25794bdb15e7289b82aecdb652c8f4f72f307b39dad39':
raise Exception("Error")
dig = hmac.new(b'1234567890', msg=msg, digestmod=sha512).hexdigest()
print('59942f31b6f5473fb4eb630fabf5358a49bc11d24ebc83b114b4af30d6ef47ea14b673f478586f520a0b9c53b27c8f8dd618c165ef586195bd4e98293d34df1a')
print(dig)
if dig != '59942f31b6f5473fb4eb630fabf5358a49bc11d24ebc83b114b4af30d6ef47ea14b673f478586f520a0b9c53b27c8f8dd618c165ef586195bd4e98293d34df1a':
raise Exception("Error")