简体   繁体   English

TypeError:必须是字符串或缓冲区,而不是int

[英]TypeError: must be string or buffer, not int

i am trying to solve the Rainbow Tables issue 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. 请帮忙。

hashlib.sha1 accepts a string as a parameter. hashlib.sha1接受字符串作为参数。

>>> import hashlib
>>> hashlib.sha1('asdf')
<sha1 HASH object @ 0000000002B97DF0>

But you're passing a int object. 但是,您正在传递一个int对象。 (The return value of the random.randint is int object as the name suggest) (顾名思义, 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.urandom to 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'

You can use python's built-in function type to 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>

hashlib.sha1 needs 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%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:必须是字符串或缓冲区,而不是int:在执行sqlAlchemy查询时 - TypeError: must be string or buffer, not int: when executing sqlAlchemy query TypeError:必须是字符串或缓冲区,而不是实例 - TypeError: must be string or buffer, not instance Python TypeError:必须是string,而不是int - Python TypeError: must be string, not int TypeError:int()参数必须是字符串或数字,而不是&#39;Binary&#39; - TypeError: int() argument must be a string or a number, not 'Binary' TypeError:int()参数必须是字符串或数字,而不是“列表” - TypeError: int() argument must be a string or a number, not 'list' Tensorflow-TypeError:int()参数必须是字符串 - Tensorflow - TypeError: int() argument must be a string TypeError:int()参数必须是字符串或数字,而不是&#39;tuple&#39; - TypeError: int() argument must be a string or a number, not 'tuple' 类型错误:INT()参数必须是字符串或数字,而不是“字典” - TypeError: int() argument must be a string or a number, not 'dict' TypeError:int()参数必须是字符串,而不是&#39;NonType&#39; - TypeError: int() argument must be a string… not 'NonType' Python 中的字符串切片中的 TypeError 必须是 str,而不是 int - TypeError must be str, not int in string slicing in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM