Created
July 27, 2016 21:04
-
-
Save gfortaine/27d9c7c882bb143ed89d456baefbd0ea to your computer and use it in GitHub Desktop.
Web Crypto API example
This file contains hidden or 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
var string = new Uint8Array([0x01, 0x02]); | |
var data = new Uint8Array([0x03, 0x04, 0x05]); | |
var handle_error = function (error) { | |
cnosole.log("Error:"); | |
console.log(err); | |
}; | |
window.crypto.subtle.digest({name: "SHA-256"}, string).then(function (hash) { | |
console.log("SHA-256:"); | |
console.log(new Uint8Array(hash)); | |
window.crypto.subtle.importKey("raw", hash, {name: "AES-GCM"}, true, ["encrypt", "decrypt"]).then(function (key) { | |
var algorithm = key.algorithm; | |
algorithm.iv = window.crypto.getRandomValues(new Uint8Array(16)); | |
window.crypto.subtle.encrypt(algorithm, key, data).then(function (ct) { | |
console.log("AES-GCM encrypt:"); | |
console.log(new Uint8Array(ct)); | |
window.crypto.subtle.decrypt(algorithm, key, ct).then(function (pt) { | |
console.log("AES-GCM decrypt:"); | |
console.log(new Uint8Array(pt)); | |
}, handle_error); | |
}, handle_error); | |
}, handle_error); | |
}, handle_error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment