【发布时间】:2021-11-27 16:01:57
【问题描述】:
我想记录脚本哈希以实施内容安全策略。我已经能够使用以下代码在 python 中生成哈希:
import hashlib
import base64
string='''
//<![CDATA[
var theForm = document.forms['ctl00'];
if (!theForm) {
theForm = document.ctl00;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
'''
# encode as UTF-8
string_UTF8 = string.encode('utf-8')
# hash the message
hash_string = hashlib.sha256(string_UTF8).digest()
# base64 encode
result = base64.b64encode(hash_string)
print('sha256-' + result.decode('utf-8'))
如何使用 Javascript 做到这一点?
【问题讨论】:
-
看起来它可以工作,但我特别想在网络浏览器中为加载页面上的脚本即时执行此操作。
标签: javascript python base64 content-security-policy sha256