将32个字符的md5字符串转换为整数 [英] Convert 32-char md5 string to integer

查看:3602
本文介绍了将32个字符的md5字符串转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将md5哈希转换为唯一整数以执行模运算的最有效方法是什么?

What's the most efficient way to convert an md5 hash to a unique integer to perform a modulus operation?

推荐答案

解决方案语言未指定,Python用于此示例。

Since the solution language was not specified, Python is used for this example.

import os
import hashlib

array = os.urandom(1 << 20)
md5 = hashlib.md5()
md5.update(array)
digest = md5.hexdigest()
number = int(digest, 16)

print(number % YOUR_NUMBER)

这篇关于将32个字符的md5字符串转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆