相关疑难解决方法(0)

在Python中散列(隐藏)字符串

我需要的是散列一个字符串.它不必是安全的,因为它只是文本文件中的隐藏短语(它不一定是人眼可识别的).

它不应该只是一个随机字符串,因为当用户键入字符串时我想将其哈希并将其与已经散列的字符串(来自文本文件)进行比较.

为此目的最好的是什么?可以使用内置类完成吗?

python string encryption hash

12
推荐指数
3
解决办法
9693
查看次数

如何在Python 3.2中实现__hash__?

我想使自定义对象可以哈希(通过酸洗).我可以找到__hash__Python 2.x的算法(参见下面的代码),但它明显不同于 Python 3.2的哈希(我想知道为什么?).有谁知道如何__hash__在Python 3.2中实现?

#Version: Python 3.2

def c_mul(a, b):
    #C type multiplication
    return eval(hex((int(a) * b) & 0xFFFFFFFF)[:-1])

class hs:
    #Python 2.x algorithm for hash from http://effbot.org/zone/python-hash.htm
    def __hash__(self):
        if not self:
            return 0 # empty
        value = ord(self[0]) << 7
        for char in self:
            value = c_mul(1000003, value) ^ ord(char)
        value = value ^ len(self)
        if value == -1:
            value = -2
        return value


def main():
    s = ["PROBLEM", "PROBLEN", "PROBLEO", "PROBLEP"]#, …
Run Code Online (Sandbox Code Playgroud)

python algorithm hash

4
推荐指数
1
解决办法
1710
查看次数

标签 统计

hash ×2

python ×2

algorithm ×1

encryption ×1

string ×1