我们有两个不同的库,一个在Python中,另一个在Go中,它们需要以相同的方式计算Murmu3散列.不幸的是,无论我们多么努力,我们都不能让库产生相同的结果.从this SO question about Java and Python%来看,兼容性并不一定是直接的.

现在我们使用的是python mmh3Go github.com/spaolacci/murmur3库.

在围棋中:

hash := murmur3.New128()
hash.Write([]byte("chocolate-covered-espresso-beans")
fmt.Println(base64.RawURLEncoding.EncodeToString(hash.Sum(nil)))
// Output: cLHSo2nCBxyOezviLM5gwg

在Python中:

name = "chocolate-covered-espresso-beans"
hash = mmh3.hash128(name.encode('utf-8'), signed=False).to_bytes(16, byteorder='big', signed=False)
print(base64.urlsafe_b64encode(hash).decode('utf-8').strip("="))
# Output: jns74izOYMJwsdKjacIHHA (big byteorder)

hash = mmh3.hash128(name.encode('utf-8'), signed=False).to_bytes(16, byteorder='little', signed=False)
print(base64.urlsafe_b64encode(hash).decode('utf-8').strip("="))
# Output: HAfCaaPSsXDCYM4s4jt7jg (little byteorder)

hash = mmh3.hash_bytes(name.encode('utf-8'))
print(base64.urlsafe_b64encode(hash).decode('utf-8').strip("="))
# Output: HAfCaaPSsXDCYM4s4jt7jg

在GO中,murmur3返回uint64,因此我们假定在Python中为signed=False;然而,我们也try 了signed=True,但没有得到匹配的散列.

我们对不同的库持开放态度,但我们想知道从字符串计算Base64编码的哈希的Go或Python方法是否有问题.如果有任何帮助,我很感激.

推荐答案

第一个Python结果几乎是正确的.

>>> binascii.hexlify(base64.b64decode('jns74izOYMJwsdKjacIHHA=='))
b'8e7b3be22cce60c270b1d2a369c2071c'

在围棋中:

    x, y := murmur3.Sum128([]byte("chocolate-covered-espresso-beans"))
    fmt.Printf("%x %x\n", x, y)

结果是:

70b1d2a369c2071c 8e7b3be22cce60c2

因此,这两个词的顺序颠倒了.要在Python中获得相同的结果,您可以try 执行以下操作:

name = "chocolate-covered-espresso-beans"
hash = mmh3.hash128(name.encode('utf-8'), signed=False).to_bytes(16, byteorder='big', signed=False)
hash = hash[8:] + hash[:8]
print(base64.urlsafe_b64encode(hash).decode('utf-8').strip("="))
# cLHSo2nCBxyOezviLM5gwg

Python相关问答推荐

在Windows上启动新Python项目的正确步骤顺序

使用from_pandas将GeDataFrame转换为polars失败,ArrowType错误:未传递numpy. dype对象

通过仅导入pandas来在for循环中进行多情节

Pandas 填充条件是另一列

如何使用Jinja语法在HTML中重定向期间传递变量?

韦尔福德方差与Numpy方差不同

如何从具有不同len的列表字典中创建摘要表?

发生异常:TclMessage命令名称无效.!listbox"

将tdqm与cx.Oracle查询集成

CommandeError:模块numba没有属性generated_jit''''

可以bcrypts AES—256 GCM加密损坏ZIP文件吗?

在pandas数据框中计算相对体积比指标,并添加指标值作为新列

Python—转换日期:价目表到新行

如何获取Python synsets列表的第一个内容?

在Docker容器(Alpine)上运行的Python应用程序中读取. accdb数据库

如何在Gekko中使用分层条件约束

Pandas 数据帧中的枚举,不能在枚举列上执行GROUP BY吗?

提取数组每行的非零元素

仅使用预先计算的排序获取排序元素

Seaborn散点图使用多个不同的标记而不是点