Created
March 25, 2017 01:40
-
-
Save ppeeou/7ebf88381a49e5d39c396f7f303ff2a0 to your computer and use it in GitHub Desktop.
javascript Promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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