Skip to content

Instantly share code, notes, and snippets.

@marcus7777
Last active July 25, 2017 11:08

Revisions

  1. marcus7777 revised this gist Jul 25, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions web_crypto_api_example.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    // for uint8array = new TextEncoder("utf-8").encode("¢");
    // for string = new TextDecoder("utf-8").decode(uint8array);

    var string = new Uint8Array([0x01, 0x02])
    var data = new Uint8Array([0x03, 0x04, 0x05])

  2. marcus7777 revised this gist Jul 25, 2017. 1 changed file with 31 additions and 1 deletion.
    32 changes: 31 additions & 1 deletion web_crypto_api_example.js
    Original file line number Diff line number Diff line change
    @@ -20,4 +20,34 @@ window.crypto.subtle.digest({name: "SHA-256"}, string).then(function (hash) {
    }, handle_error)
    }, handle_error)
    }, handle_error)
    }, handle_error)
    }, handle_error)

    function sha256(str) {
    // We transform the string into an arraybuffer.
    var buffer = new TextEncoder("utf-8").encode(str);
    return crypto.subtle.digest("SHA-256", buffer).then(function (hash) {
    return hex(hash);
    });
    }

    function hex(buffer) {
    var hexCodes = [];
    var view = new DataView(buffer);
    for (var i = 0; i < view.byteLength; i += 4) {
    // Using getUint32 reduces the number of iterations needed (we process 4 bytes each time)
    var value = view.getUint32(i)
    // toString(16) will give the hex representation of the number without padding
    var stringValue = value.toString(16)
    // We use concatenation and slice for padding
    var padding = '00000000'
    var paddedValue = (padding + stringValue).slice(-padding.length)
    hexCodes.push(paddedValue);
    }

    // Join all the hex strings into one
    return hexCodes.join("");
    }

    sha256("foobar").then(function(digest) {
    console.log(digest);
    });
  3. marcus7777 revised this gist Jul 24, 2017. 1 changed file with 13 additions and 17 deletions.
    30 changes: 13 additions & 17 deletions web_crypto_api_example.js
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,23 @@
    var string = new Uint8Array([0x01, 0x02]);
    var data = new Uint8Array([0x03, 0x04, 0x05]);
    var string = new Uint8Array([0x01, 0x02])
    var data = new Uint8Array([0x03, 0x04, 0x05])

    var handle_error = function (error) {
    cnosole.log("Error:");
    console.log(err);
    };
    cnosole.log("Error:",error)
    }

    window.crypto.subtle.digest({name: "SHA-256"}, string).then(function (hash) {
    console.log("SHA-256:");
    console.log(new Uint8Array(hash));
    console.log("SHA-256:",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));
    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));
    console.log("AES-GCM encrypt:",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);
    console.log("AES-GCM decrypt:",new Uint8Array(pt))
    }, handle_error)
    }, handle_error)
    }, handle_error)
    }, handle_error)
  4. @er91 er91 revised this gist Sep 26, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions web_crypto_api_example.js
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,16 @@
    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"}, new Uint8Array([0x01, 0x02])).then(function (hash) {
    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 data = new Uint8Array([0x01, 0x02]);
    var algorithm = key.algorithm;
    algorithm.iv = window.crypto.getRandomValues(new Uint8Array(16));

  5. @er91 er91 created this gist Sep 26, 2014.
    25 changes: 25 additions & 0 deletions web_crypto_api_example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    var handle_error = function (error) {
    cnosole.log("Error:");
    console.log(err);
    };

    window.crypto.subtle.digest({name: "SHA-256"}, new Uint8Array([0x01, 0x02])).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 data = new Uint8Array([0x01, 0x02]);
    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);