2020-01-06 15:15:04 +01:00
|
|
|
const testAPI = function(){
|
|
|
|
"use strict";
|
|
|
|
|
2020-03-14 12:54:23 +01:00
|
|
|
const digest = crypto.subtle? crypto.subtle.digest.bind(crypto.subtle, "SHA-256"): function(buffer){
|
|
|
|
return new Uint32Array(buffer.buffer);
|
|
|
|
};
|
|
|
|
|
2020-01-06 15:15:04 +01:00
|
|
|
function bufferToString(hash){
|
|
|
|
const chunks = [];
|
|
|
|
(new Uint32Array(hash)).forEach(function(num){
|
|
|
|
chunks.push(num.toString(16));
|
|
|
|
});
|
|
|
|
return chunks.map(function(chunk){
|
|
|
|
return "0".repeat(8 - chunk.length) + chunk;
|
|
|
|
}).join("");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
hash: async function(input){
|
|
|
|
const buffer = ((typeof input) === "string")?
|
|
|
|
new TextEncoder("utf-8").encode(input):
|
|
|
|
input;
|
2020-03-14 12:54:23 +01:00
|
|
|
const hash = await digest(buffer);
|
2020-01-06 15:15:04 +01:00
|
|
|
return bufferToString(hash);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}();
|