Python hash编码(go hash编码)

id="中国人"

首先,go语言hash:

Go 复制代码
import (mmh3 "murmurhash3")
mmh3.Murmurhash3([]byte(id))

对应到Python hash编码,可以直接使用mmh3

python 复制代码
import mmh3
mmh3.hash(id,signed=False)

其源码可以表示为

python 复制代码
def sum32WithSeed(datas, seed=0):
    c1_32 = 0xcc9e2d51
    c2_32 = 0x1b873593
    h1 = seed
    datas_bytes = datas.encode('utf-8')
    datas_bytes_len = len(datas_bytes)
    if datas_bytes_len == 0:
        return 0
    nblocks = datas_bytes_len // 4
    for id in range(datas_bytes_len):
        if id % 4 != 0 or id + 4 > datas_bytes_len:
            continue
        k1 = int.from_bytes(datas_bytes[id:id + 4], byteorder='little', signed=False)
        k1 *= c1_32
        k1 &= 0xffffffff
        k1 = (k1 << 15) | (k1 >> 17)
        k1 *= c2_32
        k1 &= 0xffffffff
        h1 ^= k1
        h1 = (h1 << 13) | (h1 >> 19)
        h1 = h1 * 4 + h1 + 0xe6546b64
        h1 &= 0xffffffff
    tail = datas_bytes[nblocks * 4:]
    tail_len = len(tail)
    k1 = 0
    for id in [3, 2, 1]:
        if tail_len >= id and id == 3:
            k1 ^= int.from_bytes(tail[2:3], byteorder='little', signed=False) << 16
        if tail_len >= id and id == 2:
            k1 ^= int.from_bytes(tail[1:2], byteorder='little', signed=False) << 8
        if tail_len >= id and id == 1:
            k1 ^= int.from_bytes(tail[0:1], byteorder='little', signed=False)
            k1 *= c1_32
            k1 &= 0xffffffff
            k1 = (k1 << 15) | (k1 >> 17)
            k1 *= c2_32
            k1 &= 0xffffffff
            h1 ^= k1
            h1 &= 0xffffffff

    h1 ^= datas_bytes_len
    h1 ^= h1 >> 16
    h1 *= 0x85ebca6b
    h1 &= 0xffffffff
    h1 ^= h1 >> 13
    h1 *= 0xc2b2ae35
    h1 &= 0xffffffff
    h1 ^= h1 >> 16
    return h1


def sum32(datas):
    return sum32WithSeed(datas, 0)


print(sum32(id))
相关推荐
赖small强2 分钟前
【ZeroRange WebRTC】码学基础与实践:哈希、HMAC、AES、RSA/ECDSA、随机数、X.509
webrtc·哈希算法·aes·hmac·rsa/ecdsa·x.509
dragoooon3412 分钟前
[优选算法专题六.模拟 ——NO.40~41 外观数列、数青蛙]
数据结构·算法·leetcode
徐新帅34 分钟前
CCF-GESP 等级考试 2025年3月认证C++一级真题解析
算法
wu_jing_sheng036 分钟前
深度学习入门:揭开神经网络的神秘面纱(附PyTorch实战)
python
Ace_317508877641 分钟前
淘宝店铺全量商品接口实战:分类穿透采集与增量同步的技术方案
大数据·数据库·python
陌路201 小时前
S16 排序算法--堆排序
算法·排序算法
烛衔溟1 小时前
C语言算法:排序算法入门
c语言·算法·排序算法·插入排序·冒泡排序·选择排序·多关键字排序
一匹电信狗1 小时前
【C++】封装红黑树实现map和set容器(详解)
服务器·c++·算法·leetcode·小程序·stl·visual studio
Laity______1 小时前
指针(2)
c语言·开发语言·数据结构·算法
LeonDL1681 小时前
基于YOLO11深度学习的电动车头盔检测系统【Python源码+Pyqt5界面+数据集+安装使用教程+训练代码】【附下载链接】
人工智能·python·深度学习·pyqt5·yolo数据集·电动车头盔检测系统·yolo11深度学习