我刚从源代码安装了Python 2.6.6,得到了什么:
>>> import hashlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/hashlib.py", line 136, in <module>
md5 = __get_builtin_constructor('md5')
File "/usr/local/lib/python2.6/hashlib.py", line 63, in __get_builtin_constructor
import _md5
ImportError: No module named _md5
Run Code Online (Sandbox Code Playgroud) 我有超过1亿个唯一字符串(MySQL数据库中的VARCHAR(100)UNIQUE).现在我使用下面的代码从它们创建唯一的哈希值(VARCHAR(32)UNIQUE)以减少InnoDB表的索引大小(varchar(100)上的唯一索引大约是varchar(32)字段的3倍).
id = hashlib.md5(str).hexdigest()
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以从这些字符串创建更短的ID并做出合理的唯一性保证?
这个例子正常工作例如:
import hashlib
m = hashlib.md5()
m.update(b"Nobody inspects")
r= m.digest()
print(r)
Run Code Online (Sandbox Code Playgroud)
现在,我想用变量做同样的事情:var= "hash me this text, please".我怎么能按照例子的相同逻辑做到这一点?
好吧,今天我在python中检查hashlib模块,但后来我找到了一些我仍然无法弄清楚的东西.
在这个python模块中,有一个我无法遵循的导入.我是这样的:
def __get_builtin_constructor(name):
if name in ('SHA1', 'sha1'):
import _sha
return _sha.new
Run Code Online (Sandbox Code Playgroud)
我试图从python shell导入_sha模块,但似乎无法通过那种方式进行.我首先猜测它是一个C模块,但我不确定.
那么告诉我们,你知道那个模块在哪里吗?他们如何进口?
Python应该是强类型的.
例如:'abc'['1']不起作用,因为你需要在那里提供一个整数,而不是字符串.将引发错误,您可以继续纠正它.
但是hashlib的情况并非如此.确实,请尝试以下方法:
import hashlib
hashlib.md5('abc') #Works OK
hashlib.md5(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: md5() argument 1 must be string or read-only buffer, not int
hashlib.md5(u'abc') #Works, but shouldn't : this is unicode, not str.
haslib.md5(u'é')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
当然,它不会因为a而失败TypeError,而是因为UnicodeEncodeError.UnicodeEncodeError当您尝试将unicode编码为字符串时,应该会引发此问题.
当我猜测Hashlib默默地试图将unicode转换为字符串时,我想我离真相并不太远.
现在.我同意,hashlib表示该参数hashlib.md5() …
运行mkpasswd -m sha-512 -S salt1234 password结果如下:
$6$salt1234$Zr07alHmuONZlfKILiGKKULQZaBG6Qmf5smHCNH35KnciTapZ7dItwaCv5SKZ1xH9ydG59SCgkdtsTqVWGhk81
Run Code Online (Sandbox Code Playgroud)
我有这段Python代码,我认为会输出相同的,但不是:
import hashlib, base64
print(base64.b64encode(hashlib.sha512('password' + 'salt1234').digest()))
Run Code Online (Sandbox Code Playgroud)
它反而导致:
nOkBUt6l7zlKAfjtk1EfB0TmckXfDiA4FPLcpywOLORZ1PWQK4+PZVEiT4+9rFjqR3xnaruZBiRjDGcDpxxTig==
Run Code Online (Sandbox Code Playgroud)
不确定我做错了什么.
我的另一个问题是,如何告诉sha512函数进行自定义轮次.似乎只需要一个论点.
我生成 md5 内容哈希值用于上传验证,但最近我注意到,对于在启用 FIPS 的计算机上运行的任何用户来说,这都会失败。FIPS 禁用 openssl md5,导致ValueError我尝试初始化 hashlib 时出现错误。通常我会使用 SHA,但我依赖于需要 content-md5 标头的外部服务。
我的问题是:有没有办法强制Python使用非openssl哈希函数?这里有一些关于添加usedforsecurity标志的讨论,但似乎没有任何进展。
下面的代码hashlib.sha256()与我sha256_test()用原始 python 编写的函数在哈希率性能方面进行了比较。
from time import time_ns as time\nimport hashlib\n\ndef pad512(bytes_):\n L = len(bytes_)*8\n K = 512 - ((L + 1) % 512)\n padding = (1 << K) | L\n return bytes_ + padding.to_bytes((K + 1)//8, \'big\')\n\ndef mpars (M):\n chunks = []\n while M:\n chunks.append(M[:64])\n M = M[64:]\n return chunks\n\ndef sha256_transform(H, Kt, W):\n a, b, c, d, e, f, g, h = H\n # Step 1: Looping\n for t in range(0, 64):\n T1 = h …Run Code Online (Sandbox Code Playgroud) 我正在编写一个简单的工具,允许我快速检查下载的ISO文件的MD5哈希值.这是我的算法:
import sys
import hashlib
def main():
filename = sys.argv[1] # Takes the ISO 'file' as an argument in the command line
testFile = open(filename, "r") # Opens and reads the ISO 'file'
# Use hashlib here to find MD5 hash of the ISO 'file'. This is where I'm having problems
hashedMd5 = hashlib.md5(testFile).hexdigest()
realMd5 = input("Enter the valid MD5 hash: ") # Promt the user for the valid MD5 hash
if (realMd5 == hashedMd5): # Check if valid
print("GOOD!") …Run Code Online (Sandbox Code Playgroud) 我在 python 中使用 hashlib 库,在 ruby 中使用 Digest::SHA256.hexdigest 库
我试过用python,
import hashlib
hasher = hashlib.sha256()
hasher.update("xyz")
hasher.digest()
hash = hasher.hexdigest()
print hash
Run Code Online (Sandbox Code Playgroud)
输出:3608bca1e44ea6c4d268eb6db02260269892c0b42b86bbf1e77a6fa16c3c9282
我试过用红宝石,
require 'digest'
hasher = Digest::SHA256.digest "xyz"
hash = Digest::SHA256.hexdigest(hasher)
Run Code Online (Sandbox Code Playgroud)
输出:“18cefdae0f25ad7bb5f3934634513e54e5ac56d9891eb13ce456d3eb1f3e72e8”
谁能帮助我理解为什么会有区别?如何获得与 python 相同的值?
hashlib ×10
python ×10
hash ×4
md5 ×3
python-3.x ×2
sha256 ×2
iso ×1
python-2.7 ×1
ruby ×1
sha512 ×1