Python 类型错误:必须是字符串或缓冲区,而不是整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21129799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: must be string or buffer, not int
提问by ajknzhol
i am trying to solve the Rainbow Tablesissue with password encryption and have come only this far.
我正在尝试Rainbow Tables使用密码加密来解决这个问题,并且仅到此为止。
import sys
import random
import hashlib
def mt_rand (low = 0, high = sys.maxint):
"""Generate a better random value
"""
return random.randint (low, high)
def substr (s, start, length = None):
"""Returns the portion of string specified by the start and length
parameters.
"""
if len(s) >= start:
return False
if not length:
return s[start:]
elif length > 0:
return s[start:start + length]
else:
return s[start:length]
def unique_salt():
return substr(hashlib.sha1(mt_rand()),0,22)
password = "12345"
salt = unique_salt()
hash = hashlib.sha1(salt + password).hexdigest()
print(hash)
I am getting this error:
我收到此错误:
Traceback (most recent call last):
File "C:/Users/Ajay/PycharmProjects/itertools/test.py", line 27, in <module>
salt = unique_salt()
File "C:/Users/Ajay/PycharmProjects/itertools/test.py", line 24, in unique_salt
return substr(hashlib.sha1(mt_rand()),0,22)
TypeError: must be string or buffer, not int
I know i am missing something very trivial but cant get where i am missing. Please Help.
我知道我错过了一些非常微不足道的东西,但无法找到我错过的地方。请帮忙。
采纳答案by falsetru
hashlib.sha1accepts a string as a parameter.
hashlib.sha1接受一个字符串作为参数。
>>> import hashlib
>>> hashlib.sha1('asdf')
<sha1 HASH object @ 0000000002B97DF0>
But you're passing a int object. (The return value of the random.randintis intobject as the name suggest)
但是您正在传递一个 int 对象。(顾名思义random.randint就是int对象的返回值)
>>> hashlib.sha1(1234)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be string or buffer, not int
You can use os.urandomto generate random string:
您可以使用os.urandom生成随机字符串:
>>> import os
>>> hashlib.sha1(os.urandom(10)) # `os.urandom(10)` generate 10-bytes random string.
<sha1 HASH object @ 0000000002B97F30>
>>> hashlib.sha1(os.urandom(10)).digest()
'\x0c.y\x08\x13\xf0\x16.\xea\x05\x03\x07{6H\xa0U\xfe\xdfT'
>>> hashlib.sha1(os.urandom(10)).hexdigest()
'6e33d9cfdbd7ffcf062ee502eaa25893f618fcff'
回答by Madison May
You can use python's built-in function typeto inspect objects.
您可以使用 python 的内置函数type来检查对象。
>>>type(mt_rand())
int
>>>hashlib.sha1(mt_rand())
TypeError: must be string or buffer, not int
This is to be expected. Pass hashlib.sha1 a string instead.
这是可以预料的。将 hashlib.sha1 传递给一个字符串。
>>>hashlib.sha1("password")
<sha1 HASH object @ 0x1c89cb0>
回答by aIKid
hashlib.sha1needs a string to be hashed, but you put an integer.
hashlib.sha1需要一个字符串进行散列,但你输入了一个整数。
Convert it to string first:
首先将其转换为字符串:
def unique_salt():
return substr(hashlib.sha1(str(mt_rand())),0,22)
Here's a bit of demo:
这是一个演示:
>>> import hashlib
>>> import random
>>> s = random.randint(1, 1000)
>>> hashlib.sha1(str(s)).digest()
'\xd1\x84\x01\xb1\xbb7\xc5\xd9)|\xf1o\xc48X\xb4\xfd\xb3x%'

