如何获得MD5使用Python的string的总和?

在Flickr API文档中 ,您需要查找string的MD5和以生成[api_sig]值。

如何从string生成一个MD5总和?

Flickr的例子:

string: 000005fab4534d05api_key9a0554259914a86fb9e7eb014e4e5d52permswrite

MD5总和: a02506b31c1cd46c2e0b6380fb94eb3d

对于Python 2.x,请使用python的hashlib

 import hashlib m = hashlib.md5() m.update("000005fab4534d05api_key9a0554259914a86fb9e7eb014e4e5d52permswrite") print m.hexdigest() 

输出: a02506b31c1cd46c2e0b6380fb94eb3d

您可以执行以下操作:

Python 2.x

 import hashlib print hashlib.md5("whatever your string is").hexdigest() 

Python 3.x

 import hashlib print(hashlib.md5("whatever your string is".encode('utf-8')).hexdigest()) 

但是在这种情况下,使用这个有用的Python模块与Flickr API进行交互可能会更好:

…将为您处理身份validation。

hashlib官方文档

你有没有尝试在hashlib中使用MD5实现? 请注意,哈希algorithm通常作用于二进制数据而不是文本数据,因此您可能需要注意在哈希之前使用哪种字符编码将文本转换为二进制数据。

散列的结果也是二进制数据 – 它看起来像Flickr的例子已被转换成使用hex编码的文本。 使用hexdigest中的十六hexdigest函数来获取它。

python:

 from Crypto.Hash import MD5 h = MD5.new() h.update(bytearray("any of your string")) print h.hexdigest()` 

这给出了inputstring的md5总和。

的NodeJS:

 var crypto = require('crypto'); var s = "any of your string"; var md5 = crypto.createHash('md5').update(s).digest('hex'); console.log("hash means",md5); 

这是nodejs用例中的md5 sum的代码。

感谢pycrypto提供了单向(哈希)和双向encryption的简单方法

 from Crypto.Hash import MD5 from Crypto.Cipher import AES 

在python访问中更完整简单的函数http://developer.e-power.com.kh/one-way-vs-twoway-encryption

Interesting Posts