kopia lustrzana https://github.com/micropython/micropython-lib
hmac: Port to MicroPython (missing bytes.translate(), warnings).
rodzic
b90d145a1d
commit
0d938a20a3
11
hmac/hmac.py
11
hmac/hmac.py
|
@ -4,12 +4,17 @@ Implements the HMAC algorithm as described by RFC 2104.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import warnings as _warnings
|
import warnings as _warnings
|
||||||
from _operator import _compare_digest as compare_digest
|
#from _operator import _compare_digest as compare_digest
|
||||||
import hashlib as _hashlib
|
import hashlib as _hashlib
|
||||||
|
PendingDeprecationWarning = None
|
||||||
|
RuntimeWarning = None
|
||||||
|
|
||||||
trans_5C = bytes((x ^ 0x5C) for x in range(256))
|
trans_5C = bytes((x ^ 0x5C) for x in range(256))
|
||||||
trans_36 = bytes((x ^ 0x36) 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
|
# The size of the digests returned by HMAC depends on the underlying
|
||||||
# hashing module used. Use digest_size from the instance of HMAC instead.
|
# hashing module used. Use digest_size from the instance of HMAC instead.
|
||||||
digest_size = None
|
digest_size = None
|
||||||
|
@ -78,8 +83,8 @@ class HMAC:
|
||||||
key = self.digest_cons(key).digest()
|
key = self.digest_cons(key).digest()
|
||||||
|
|
||||||
key = key + bytes(blocksize - len(key))
|
key = key + bytes(blocksize - len(key))
|
||||||
self.outer.update(key.translate(trans_5C))
|
self.outer.update(translate(key, trans_5C))
|
||||||
self.inner.update(key.translate(trans_36))
|
self.inner.update(translate(key, trans_36))
|
||||||
if msg is not None:
|
if msg is not None:
|
||||||
self.update(msg)
|
self.update(msg)
|
||||||
|
|
||||||
|
|
|
@ -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'])
|
|
@ -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")
|
||||||
|
|
Ładowanie…
Reference in New Issue