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))
相关推荐
程序猿k哥2 分钟前
Golang channel底层是如何实现的?(深度好文)
golang
龙泉寺天下行走11 分钟前
编写Linux下第一个Go程序(2024版)
linux·运维·golang
子龙烜14 分钟前
数据分析三剑客-Matplotlib
python·数据挖掘·数据分析·matplotlib
图灵追慕者15 分钟前
python绘制领域矩形
开发语言·python·领域
PPPPPaPeR.28 分钟前
TopK问题与堆排序
c语言·开发语言·c++·算法
Recitative30 分钟前
python单元测试入门
人工智能·python·深度学习·机器学习·单元测试
小安同学iter34 分钟前
计算机大方向的选择
python·django·pygame
wxin_VXbishe1 小时前
servlet职称评审系统-计算机毕业设计源码00122
java·spring boot·python·servlet·django·flask·php
捉鸭子1 小时前
蜜雪冰城小程序逆向
javascript·爬虫·python·网络爬虫·逆向
满心欢喜love2 小时前
Python爬虫康复训练——笔趣阁《神魂至尊》
开发语言·爬虫·python