From 0d938a20a33a9ee97278195c0dd232576c2f9227 Mon Sep 17 00:00:00 2001 From: slush0 Date: Tue, 17 Feb 2015 23:07:19 +0100 Subject: [PATCH] hmac: Port to MicroPython (missing bytes.translate(), warnings). --- hmac/hmac.py | 11 ++++++++--- hmac/setup.py | 19 +++++++++++++++++++ hmac/test_hmac.py | 22 ++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 hmac/setup.py create mode 100644 hmac/test_hmac.py diff --git a/hmac/hmac.py b/hmac/hmac.py index 77785a2d..c2ce23b1 100644 --- a/hmac/hmac.py +++ b/hmac/hmac.py @@ -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) diff --git a/hmac/setup.py b/hmac/setup.py new file mode 100644 index 00000000..47366723 --- /dev/null +++ b/hmac/setup.py @@ -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']) diff --git a/hmac/test_hmac.py b/hmac/test_hmac.py new file mode 100644 index 00000000..a2c1349c --- /dev/null +++ b/hmac/test_hmac.py @@ -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") +