Skip to content

Instantly share code, notes, and snippets.

@ppeeou
Created March 25, 2017 01:40
Show Gist options
  • Save ppeeou/7ebf88381a49e5d39c396f7f303ff2a0 to your computer and use it in GitHub Desktop.
Save ppeeou/7ebf88381a49e5d39c396f7f303ff2a0 to your computer and use it in GitHub Desktop.
javascript Promise
function convertStringToArrayBufferView(str){
var bytes = new Uint8Array(str.length);
for(var iii = 0 ; iii<str.lenght;iii++){
bytes[iii] = str.charCodeAt(iii);
}
return bytes;
}
function convertArrayBufferToHexaDecimal(buffer){
var data_view = new DataView(buffer);
var iii,len,hex='',c;
for(iii =0 ,len = data_view.byteLength;iii<len;iii++){
c = data_view.getUint8(iii).toString(16);
if(c.length<2){
c= '0' +c;
}
hex += c;
}
return hex;
}
window.crypto.subtle.digest({name:"SHA-256"},convertStringToArrayBufferView("Hello World"))
.then(function(result){
var hash_value = convertArrayBufferToHexaDecimal(result);
console.log(hash_value);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment