From 500a8bb751f05e8170f74bed3f1363c3ed90f39a Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Fri, 3 Feb 2023 21:48:33 +0100 Subject: [PATCH] llHMAC implementation (without importing hmac) --- lslopt/lslbasefuncs.py | 37 +++++++++++++++++++++++++++++++- strutil.py | 5 +++-- unit_tests/expr.suite/llhmac.lsl | 29 +++++++++++++++++++++++++ unit_tests/expr.suite/llhmac.out | 3 +++ unit_tests/expr.suite/llhmac.skp | 0 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 unit_tests/expr.suite/llhmac.lsl create mode 100644 unit_tests/expr.suite/llhmac.out create mode 100644 unit_tests/expr.suite/llhmac.skp diff --git a/lslopt/lslbasefuncs.py b/lslopt/lslbasefuncs.py index b42ddc6..b30b6aa 100644 --- a/lslopt/lslbasefuncs.py +++ b/lslopt/lslbasefuncs.py @@ -1307,6 +1307,41 @@ def llHash(s): hash = (hash * 65599 + ord(i)) & 0xFFFFFFFF return S32(hash) +def llHMAC(pwd, data, alg): + pwd = bytewrap(uniwrap(fs(pwd)).encode('utf8')) + data = bytewrap(uniwrap(fs(data)).encode('utf8')) + alg = fs(alg) + hash = None + if alg == u'md5': + hash = hashlib.md5() + elif alg == u'sha1': + hash = hashlib.sha1() + elif alg == u'sha224': + hash = hashlib.sha224() + elif alg == u'sha256': + hash = hashlib.sha256() + elif alg == u'sha384': + hash = hashlib.sha384() + elif alg == u'sha512': + hash = hashlib.sha512() + if hash is None: + raise ELSLCantCompute # we don't have info on how it behaves yet + # Calculate the HMAC here, to avoid requiring yet another module + if len(pwd) > hash.block_size: + tmp = hash.copy() + tmp.update(pwd) + pwd = bytewrap(tmp.digest()) + del tmp + if len(pwd) < hash.block_size: + pwd += bytewrap(b'\x00') * (hash.block_size - len(pwd)) + xorbytes = lambda a, b: bytewrap(x ^ y for (x, y) in zip(a, b)) + ipadded = xorbytes(pwd, bytewrap(b'\x36') * hash.block_size) + opadded = xorbytes(pwd, bytewrap(b'\x5C') * hash.block_size) + ohash = hash.copy() + hash.update(ipadded + data) + ohash.update(opadded + hash.digest()) + return b64encode(ohash.digest()).decode('utf8') + def llInsertString(s, pos, src): s = fs(s) pos = fi(pos) @@ -1316,7 +1351,7 @@ def llInsertString(s, pos, src): def llIntegerToBase64(x): x = fi(x) - return (b64encode(bytearray(((x>>24)&255, (x>>16)&255, (x>>8)&255, x&255))) + return (b64encode(bytewrap(((x>>24)&255, (x>>16)&255, (x>>8)&255, x&255))) .decode('utf8')) def llLinear2sRGB(v): diff --git a/strutil.py b/strutil.py index ba45189..7fcc8a3 100644 --- a/strutil.py +++ b/strutil.py @@ -23,14 +23,15 @@ codecs.register(lambda x: codecs.lookup('utf8') if x == 'cp65001' else None) import sys +python2Narrow = False if sys.version_info.major >= 3: unicode = str unichr = chr xrange = range python3 = True python2 = False - python2Narrow = False uniwrap = unicode + bytewrap = bytes def str2u(s, enc=None): """Convert a native Python3 str to Unicode. This is a NOP.""" @@ -60,8 +61,8 @@ else: xrange = xrange python2 = True python3 = False - python2Narrow = False uniwrap = unicode + bytewrap = bytearray def str2u(s, enc=None): """Convert a native Python2 str to Unicode.""" diff --git a/unit_tests/expr.suite/llhmac.lsl b/unit_tests/expr.suite/llhmac.lsl new file mode 100644 index 0000000..aa17e0b --- /dev/null +++ b/unit_tests/expr.suite/llhmac.lsl @@ -0,0 +1,29 @@ +[ llHMAC("a", "b", "sha1") +, llHMAC("-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEAqxXSIhFHzYO9UNEUvMMXwhB4vf32fPirCxxV/w4m88jKPmFH +QQe9DOwj7illmvg+81vzBNGt+uNYy/2zFegUtwvxKCEioeoanRpPcvn9r/d/kXad +WL/DyKJwHbF1EtTfPAZSl6ZIBIYis8HQ/RAln3olS705AmCKBRkbz3cZ+dTzqX1v +7ohqqPPoCaXQFgLTMYnqU8ZTsq1Sl8BwKK735HPmKLCEjZaMn97lvzGHufY/JdRs +dwdRHqKnpe2w2c0AzNpQtjoRCnPtj7cFg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Q/Ee +lxfr5tPinm+rmGWjOKIMCe53nA81RUlmB/iaxn9vA5ADrUS+53Vlj+SmPe7a/dVf +A5gHAoGAbS4sMlUkUd449PT33rqx26aNKkKLI9PLxgWE7YBfwzaUkG0MBryQqP5L +aIY2a+8ZvUeHxmY0oQfPQkH5KKbAaC0ozaXf+3qX0Gfkt8vxsh41ON5esr0tfcm2 +BFdQrdBOACefo2kOFfdMSP6KWKI3HZMJAr9SDcAiL23IQZ/wl/c= +-----END RSA PRIVATE KEY-----", "Hello, Avatar!", "sha1") +] diff --git a/unit_tests/expr.suite/llhmac.out b/unit_tests/expr.suite/llhmac.out new file mode 100644 index 0000000..e54502e --- /dev/null +++ b/unit_tests/expr.suite/llhmac.out @@ -0,0 +1,3 @@ +[ "ZleFVoaCOYbIdDYnMROXUgFMtgs=" +, "xK/3hcR1IVpVK9vftoztuVRGZFE=" +] \ No newline at end of file diff --git a/unit_tests/expr.suite/llhmac.skp b/unit_tests/expr.suite/llhmac.skp new file mode 100644 index 0000000..e69de29