Python 中的 SHA-256 实现
我正在寻找 SHA-256 哈希函数的 Python 实现。我想用它来更好地理解 SHA-256 函数的工作原理,并且我认为 Python 是实现此目的的理想语言。伪代码有一个限制,我无法运行/测试它,以查看我对代码的修改对输出的影响。
I'm looking for a Python implementation of the SHA-256 hash function. I want to use it to get a better understanding of how the SHA-256 function works, and I think Python is the ideal language for this. Pseudo-code has the limitation that I can't run/test it, to see what my modifications of the code do to the output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
PyPy 的源代码包含 SHA-256 的纯 python 实现 这里。在该目录中浏览,您可能还会发现其他标准哈希的纯 Python 实现。
PyPy's source contains a pure-python implementation of SHA-256 here. Poking around in that directory, you'll probably also find pure-python implementations of other standard hashes.
前段时间我也在研究 SHA-256 并创建了实现这个哈希的纯 python 类。如果我没记错的话,我主要是从维基百科获取算法 SHA-256 Pseudocode 部分来自一些开源项目。
算法不导入任何(甚至标准)模块。当然,它比 hashlib 的变体慢得多,并且仅用于研究。
如果您只是运行该脚本,它会执行 1000 个测试来比较 hashlib 和我的变体。仅测试函数导入一些模块,算法类本身不需要任何模块。接口与 hashlib 的 sha256 类中的相同。有关使用示例,请参阅
test()
函数。<一href="https://tio.run/##lVZNc9s2EL37V6CHVmRtKwAIgKQnyfTk6aW9pDdV1QAgYDEWJY9IT2VPp3/dXSwoiZRlN@FF0AL7dvftB/Dw1C036@zlxa5025IvS82lurkg8N 235BOZ4TJ8dCd4obkviytY5yzLhShZWBtpqTfWh7UrjayMllcDvayUynJpwr4sPWPMo17JM19wLcJaG2alq0Z6VUFzraM9xotMGop6XGSskMYhnqQ2r2w21Mu5cbLKEbeglTPM 49nSVJYqnYe1ZaXxDM4M9JwojSot2nDeOJEXKqyph9OVVdE2tZpZO9TjlSu5VRi/0LkohNbomzVUg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iczs7UFt9xSke9F9oBaJHFQQXgugfTeS3IHEjyRhvrqRxIU0VQDYjd MxPFPBGTuS2DCnRxIDEj2S6IDchVA6fj7TmMArEgdOSKRbPzZuG6bvc/2QxHxfkdn5ipin6Znpvewrbwd2n16bDW3fD/j@hhqA7C@HcDEQ8C00a5OST3BRjS3F6TIO2bZ12w4v lf3NAzPgV90uiV5tna6eCIj0qn52FdHrili9xtPG9d5UP0wuzlyRl596J463zL7OwmV5SZo326EfNuD/hw9nxglW137@NTMlYNSFCYaLJMwxls7Ti3MXdDPrga9J0q9@RAvzI8 dVfefaDjkeEzzk6O3LF9fhjj1QkaavD0cj8ckw/bqp18nueK2K0RTeBWL6Arkp5mfA4mvhj@2jO71EBraOAS7d7nyMnQ6tMKGMZ0KqviB3lAWFySnq3mU4PzNhhorQ1vFFeD/c ztFxc3C8NwdEXAQHOvwT7dbNw2YbeLUA3kJvQOWtaugZKIVq01wcyiMoHUtEUjpwHIjXWFwIAkTeu3XPZYSZhp@oyWU@SEjozzC6otVpi0@BJOCl0yNPcOt9eWPntJF06DlwH2 dt@m3un3fiaDQdNcrXI8yr4OAyO@mW76GG8fKkVvW@qjHu8dQ8v9XHosfsme@k7GELb5RkEuPHeoHp0z5aCy3vH1erpym8OS/q8Ghf68YtFgFislg0ul4vFpNIQSyzi5eX/wa" rel="nofollow noreferrer" title="Python 3 – 在线试用">在线试用!
Some time ago I was also studying SHA-256 and created pure-python class that implements this hash. If I remember correctly, mostly I've taken algorithm from Wikipedia SHA-256 Pseudocode and partially from some open-source projects.
Algorithm doesn't import any (even standard) modules. Of cause it is much slower than
hashlib
's variant and only meant for studying.If you just run the script it executes 1000 tests comparing
hashlib
's and my variants. Only testing function imports some modules, algorithm's class itself doesn't need any modules. Interface is same as in hashlib's sha256 class. Seetest()
function for examples of usage.Try it online!
如果你只想要哈希值:
Python的hashlib还有SHA-1、SHA-384、SHA-512和MD5哈希函数。
If you only want the hash value:
Python's hashlib also has SHA-1, SHA-384, SHA-512, and MD5 hash functions.
这是我对 redis 的建议:
Here is my proposition with redis:
翻译 http://en.wikipedia.org/wiki/ SHA-2#SHA-256_.28a_SHA-2_variant.29_pseudocode 到 Python 应该是直接的。
Translating http://en.wikipedia.org/wiki/SHA-2#SHA-256_.28a_SHA-2_variant.29_pseudocode to Python should be straight forward.