mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2024-11-21 06:15:56 -07:00
llHMAC implementation (without importing hmac)
This commit is contained in:
parent
1bd9551c2f
commit
500a8bb751
5 changed files with 71 additions and 3 deletions
|
@ -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):
|
||||
|
|
|
@ -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."""
|
||||
|
|
29
unit_tests/expr.suite/llhmac.lsl
Normal file
29
unit_tests/expr.suite/llhmac.lsl
Normal file
|
@ -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/JdR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LoBoIBBYzyiVaWQKBgD3GvHmbmHVc/0f8c0drsedWIK0K+tct3ssqqGXu
|
||||
gw/rA+CPVDfTRQv6qntJwyTxh3xxDRNSMW7S2/0rZ0cUPgoIGz+kMn+TdvH8Q/Ee
|
||||
lxfr5tPinm+rmGWjOKIMCe53nA81RUlmB/iaxn9vA5ADrUS+53Vlj+SmPe7a/dVf
|
||||
A5gHAoGAbS4sMlUkUd449PT33rqx26aNKkKLI9PLxgWE7YBfwzaUkG0MBryQqP5L
|
||||
aIY2a+8ZvUeHxmY0oQfPQkH5KKbAaC0ozaXf+3qX0Gfkt8vxsh41ON5esr0tfcm2
|
||||
BFdQrdBOACefo2kOFfdMSP6KWKI3HZMJAr9SDcAiL23IQZ/wl/c=
|
||||
-----END RSA PRIVATE KEY-----", "Hello, Avatar!", "sha1")
|
||||
]
|
3
unit_tests/expr.suite/llhmac.out
Normal file
3
unit_tests/expr.suite/llhmac.out
Normal file
|
@ -0,0 +1,3 @@
|
|||
[ "ZleFVoaCOYbIdDYnMROXUgFMtgs="
|
||||
, "xK/3hcR1IVpVK9vftoztuVRGZFE="
|
||||
]
|
0
unit_tests/expr.suite/llhmac.skp
Normal file
0
unit_tests/expr.suite/llhmac.skp
Normal file
Loading…
Reference in a new issue