Python 尝试使用时间戳和base64编码的json字符串创建哈希,获取内存错误
这个简单的脚本由于内存错误而死亡,我不知道为什么Python 尝试使用时间戳和base64编码的json字符串创建哈希,获取内存错误,python,hmac,hashlib,Python,Hmac,Hashlib,这个简单的脚本由于内存错误而死亡,我不知道为什么 将simplejson导入为json 进口hmac 导入hashlib 从时间导入时间 导入base64 sso_user={'a':'foo','b':'bar'} ssoKey=b'XXXXXXXXXXXXXXXXXXXXXXXXX' 时间戳=四舍五入(时间()*1000) s=json.dumps(sso_用户) userDataJSONBase64=base64.b64encode(s.encode()) verificationHash
将simplejson导入为json
进口hmac
导入hashlib
从时间导入时间
导入base64
sso_user={'a':'foo','b':'bar'}
ssoKey=b'XXXXXXXXXXXXXXXXXXXXXXXXX'
时间戳=四舍五入(时间()*1000)
s=json.dumps(sso_用户)
userDataJSONBase64=base64.b64encode(s.encode())
verificationHash=hmac.new(
字节(时间戳)+userDataJSONBase64,
ssoKey,
hashlib.sha256
).hexdigest()
打印(验证哈希)
它被hmac.new()阻塞了。我做了这个更改,似乎已经解决了这个问题
verificationHash=hmac.new(
str(timestamp).encode()+userDataJSONBase64,
ssoKey,
hashlib.sha256
).hexdigest()
问题在于您使用的是,它的行为与python2中的不同。在python2.7中,bytes
是str
的别名。在python3中,接受整数的构造函数生成一个n0数组。由于您传入了类似于1617219736292
(2021年3月31日)的内容,因此您正在初始化一个大小为1.6万亿的数组,并且内存不足:MemoryError
$ python2
>>> print(bytes.__doc__)
str(object='') -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
^C
$ python3
>>> print(bytes.__doc__)
bytes(iterable_of_ints) -> bytes
bytes(string, encoding[, errors]) -> bytes
bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
bytes(int) -> bytes object of size given by the parameter initialized with null bytes
bytes() -> empty bytes object
Construct an immutable array of bytes from:
- an iterable yielding integers in range(256)
- a text string encoded using the specified encoding
- any object implementing the buffer API.
- an integer
您遇到了什么错误?(link_short)[sonnyparlin@fedoralink_short]$python test.py回溯(最后一次调用):文件“/home/sonnyparlin/github/link_short/test.py”,第12行,字节(时间戳)+userDataJSONBase64,MemoryError