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))
相关推荐
拓端研究室TRL5 分钟前
Python贝叶斯回归、强化学习分析医疗健康数据拟合截断删失数据与参数估计3实例
开发语言·人工智能·python·数据挖掘·回归
returnShitBoy7 分钟前
Go语言中的垃圾回收是如何工作的?
java·jvm·golang
wolf犭良1 小时前
27、Python 数据库操作入门(SQLite)从基础到实战精讲
数据库·python·sqlite
普通网友1 小时前
如何在CentOS部署青龙面板并实现无公网IP远程访问本地面板
开发语言·后端·golang
sa100271 小时前
基于Python的网络爬虫技术研究
开发语言·爬虫·python
画扇落汗1 小时前
Python 几种将数据插入到数据库的方法(单行插入、批量插入,SQL Server、MySQL,insert into)
数据库·python·sql·mysql
Java致死1 小时前
费马小定理
算法·费马小定理
不吃元西2 小时前
leetcode 74. 搜索二维矩阵
算法·leetcode·矩阵
小开不是小可爱2 小时前
leetcode_454. 四数相加 II_java
java·数据结构·算法·leetcode