wub*_*bbe 3 javascript buffer window.crypto
我正在尝试哈希一个字符串.但IE11和Safari中的函数TextEncoder的替代方案是什么?
var string = "foobar";
window.crypto.subtle.digest(
{ "name": "SHA-256" },
new TextEncoder("utf-8").encode(string)).then(function (hash)
{
console.log(hex(hash)); // 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2'
}
);
var string = "foobar";
var buffer = new TextEncoder("utf-8").encode(string); // Uint8Array (ArrayBuffer)
var string = new TextDecoder("utf-8").decode(buffer); // string
console.log("buffer", buffer);
console.log("string '" + string + "'");
Run Code Online (Sandbox Code Playgroud)
hex是我从mozilla获得的一个函数
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
编辑:我刚刚使用了http://www.movable-type.co.uk/scripts/sha256.html中的SHA256例程,因为我不需要高性能,而且代码简短而且甜蜜且代码相同路径随处可见.crypto.subtle API被证明有太多的极端情况我失败了(我在某些移动设备上遇到问题,除了下面链接的Chrome问题导致我感到悲伤,我也需要支持旧浏览器).
我为IE11编写了这个函数,它对我有用(虽然很难测试):
function textEncode(str) {
if (window.TextEncoder) {
return new TextEncoder('utf-8').encode(str);
}
var utf8 = unescape(encodeURIComponent(str));
var result = new Uint8Array(utf8.length);
for (var i = 0; i < utf8.length; i++) {
result[i] = utf8.charCodeAt(i);
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
请注意还有其他问题,因为IE11没有承诺,并且msCrypto.subtle.digest()是同步的.以下内容可能对您有用,但您应该修复hacks(它需要工作以使其健壮并使用Promise polyfill):
function sha256(str) {
function hex(buffer) {
var hexCodes = [];
var view = new DataView(buffer);
for (var i = 0; i < view.byteLength; i += 4) {
var value = view.getUint32(i);
var stringValue = value.toString(16);
var padding = '00000000';
var paddedValue = (padding + stringValue).slice(-padding.length);
hexCodes.push(paddedValue);
}
return hexCodes.join('');
}
var buffer = textEncode(str);
var res = crypto.subtle.digest('SHA-256', buffer);
if (res.then) {
return res.then(function (hash) {
return hex(hash);
});
} else if (res.result) { // IE11
return {
then: function(resolver) {
resolver(hex(res.result));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
另请注意此Chrome问题,您可以使用regexp /^https:|^file:|^http:\/\/localhost|^http:\/\/127.0.0.1/.test(location.protocol)进行测试.