Created
December 8, 2016 07:52
-
-
Save berlam/4cb3ee73486ebae6759254220600be1b to your computer and use it in GitHub Desktop.
Browser AES
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
| <html xmlns="http://www.w3.org/1999/xhtml" style="overflow: hidden; height: 100%;"> | |
| <head> | |
| <script type="text/javascript"> | |
| function sha256(str) { | |
| var buffer = new TextEncoder("utf-8").encode(str); | |
| return crypto.subtle.digest("SHA-256", buffer).then(function (hash) { | |
| return hash; | |
| }); | |
| } | |
| function encryptAes(str, key, iv) { | |
| var toEncryptArr = new TextEncoder("utf-8").encode(str); | |
| var passwordArr = new TextEncoder("utf-8").encode(key); | |
| return crypto.subtle.digest("SHA-256", passwordArr).then(function (hash) { | |
| return crypto.subtle.importKey("raw", hash, {name: "AES-CBC", iv: iv}, true, ["encrypt"]).then(function (passwordKey) { | |
| return crypto.subtle.encrypt({name: "AES-CBC", iv: iv}, passwordKey, toEncryptArr).then(function (encrypted) { | |
| return encrypted; | |
| }); | |
| }); | |
| }); | |
| } | |
| sha256("foobar").then(function (digest) { | |
| console.log(new DataView(digest).byteLength); | |
| console.log(new Int8Array(digest)); | |
| }); // outputs "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2" | |
| encryptAes("foobar", "key", new Int8Array(16)).then(function (digest) { | |
| console.log(new DataView(digest).byteLength); | |
| console.log(new Int8Array(digest)); | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment