如何检查加载的Javascript代码的完整性 [英] How to check the integrity of loaded Javascript code
问题描述
我运行比特币钱包,使用浏览器端Javascript来加密和解密比特币密钥。
I run a Bitcoin wallet that uses browser side Javascript to encrypt and decrypt Bitcoin keys.
我想让javascript可以在Github上进行审查,然后加载来自github存储库的javascript。
I want to make the javascript available for scrutiny on Github, and then load the javascript from the github repository.
我的问题是我需要检查加载的Javascript的完整性,以确保在Github上没有被篡改。
My problem is I need to check the integrity of the loaded Javascript to ensure at hasn't been tampered with at Github.
我怎样才能做到最好?它会是什么样的。
How can I best do this ? Would it be something like.
- 使用ajax调用加载远程Javascript。
- MD5哈希和比较。
- 如果处于良好状态,请执行它。
推荐答案
现在(或很快将会)有一种优雅的方式来实现这个目标(问题提出后2年)。
There is (or soon WILL be) an elegant way to achieve this now (2 years after the question was asked).
http://www.w3.org / TR / SRI /
您现在可以在脚本标记内指定integrity参数:
You can now specify the "integrity" parameter inside the script tag:
<script src="https://github.com/<path>/yourscript.js"
integrity="sha256-SDf......">
这对通过AJAX请求加载的脚本无效。但你可以引用脚本作为指向CDN的脚本标签(理想情况下不是直接在github上)
This won't work for the script loaded via an AJAX requests. But you can potentially reference scripts as script tags pointing at the CDN (ideally not at github directly)
这篇关于如何检查加载的Javascript代码的完整性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!