问题:如何使用python获取字符串的MD5和?

Flickr API文档中,您需要找到字符串的MD5总和以生成[api_sig]值。

如何从字符串生成MD5和?

Flickr的示例:

串: 000005fab4534d05api_key9a0554259914a86fb9e7eb014e4e5d52permswrite

MD5总和: a02506b31c1cd46c2e0b6380fb94eb3d

In the Flickr API docs, you need to find the MD5 sum of a string to generate the [api_sig] value.

How does one go about generating an MD5 sum from a string?

Flickr’s example:

string: 000005fab4534d05api_key9a0554259914a86fb9e7eb014e4e5d52permswrite

MD5 sum: a02506b31c1cd46c2e0b6380fb94eb3d


回答 0

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

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

输出: a02506b31c1cd46c2e0b6380fb94eb3d

For Python 2.x, use python’s hashlib

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

Output: a02506b31c1cd46c2e0b6380fb94eb3d


回答 1

您可以执行以下操作:

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进行交互:

…将为您处理身份验证。

hashlib的官方文档

You can do the following:

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())

However in this case you’re probably better off using this helpful Python module for interacting with the Flickr API:

… which will deal with the authentication for you.

Official documentation of hashlib


回答 2

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

哈希的结果也是二进制数据-看起来Flickr的示例已使用十六进制编码转换为文本。使用hexdigesthashlib中的函数来获取此信息。

Have you tried using the MD5 implementation in hashlib? Note that hashing algorithms typically act on binary data rather than text data, so you may want to be careful about which character encoding is used to convert from text to binary data before hashing.

The result of a hash is also binary data – it looks like Flickr’s example has then been converted into text using hex encoding. Use the hexdigest function in hashlib to get this.


回答 3

Try This 
import hashlib
user = input("Enter text here ")
h = hashlib.md5(user.encode())
h2 = h.hexdigest()
print(h2)
Try This 
import hashlib
user = input("Enter text here ")
h = hashlib.md5(user.encode())
h2 = h.hexdigest()
print(h2)

回答 4

您可以b在字符串文字前面使用character

import hashlib
print(hashlib.md5(b"Hello MD5").hexdigest())
print(hashlib.md5("Hello MD5".encode('utf-8')).hexdigest())

出:

e5dadf6524624f79c3127e247f04b548
e5dadf6524624f79c3127e247f04b548

You can use b character in front of a string literal:

import hashlib
print(hashlib.md5(b"Hello MD5").hexdigest())
print(hashlib.md5("Hello MD5".encode('utf-8')).hexdigest())

Out:

e5dadf6524624f79c3127e247f04b548
e5dadf6524624f79c3127e247f04b548

回答 5

您可以尝试

#python3
import hashlib
rawdata = "put your data here"
sha = hashlib.sha256(str(rawdata).encode("utf-8")).hexdigest() #For Sha256 hash
print(sha)
mdpass = hashlib.md5(str(sha).encode("utf-8")).hexdigest() #For MD5 hash
print(mdpass)

You can Try with

#python3
import hashlib
rawdata = "put your data here"
sha = hashlib.sha256(str(rawdata).encode("utf-8")).hexdigest() #For Sha256 hash
print(sha)
mdpass = hashlib.md5(str(sha).encode("utf-8")).hexdigest() #For MD5 hash
print(mdpass)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。