Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSA-OAEP wrapKey/unwrapKey #32

Open
microshine opened this issue Aug 27, 2016 · 1 comment
Open

RSA-OAEP wrapKey/unwrapKey #32

microshine opened this issue Aug 27, 2016 · 1 comment

Comments

@microshine
Copy link

Check please those functions.
It seems that algorithm must be the same as for encrypt/decrypt - {name: string, label?: ArrayBufferView}

@microshine
Copy link
Author

It works for me in Chrome

window.crypto.subtle.generateKey(
    {
        name: "RSA-OAEP",
        modulusLength: 2048, //can be 1024, 2048, or 4096
        publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
        hash: { name: "SHA-1" }, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
    },
    false, //whether the key is extractable (i.e. can be used in exportKey)
    ["wrapKey", "unwrapKey"] //can be any combination of "sign" and "verify"
)
    .then(function (key) {
        //returns a keypair object
        console.log(key);
        console.log(window.pubkey = key.publicKey);
        console.log(window.prvkey = key.privateKey);
    })
    .then(function () {
        return window.crypto.subtle.generateKey(
            {
                name: "AES-CBC",
                length: 256, //can be  128, 192, or 256
            },
            true, //whether the key is extractable (i.e. can be used in exportKey)
            ["encrypt", "decrypt"] //can be "encrypt", "decrypt", "wrapKey", or "unwrapKey"
        )
    })
    .then(function (key) {
        //returns a key object
        console.log(window.aes = key);
        return window.crypto.subtle.wrapKey(
            "raw", //the export format, must be "raw" (only available sometimes)
            key, //the key you want to wrap, must be able to fit in RSA-OAEP padding
            window.pubkey, //the public key with "wrapKey" usage flag
            {   //these are the wrapping key's algorithm options
                name: "RSA-OAEP",
            }
        );
    })
    .then(function (wrapped) {
        //returns an ArrayBuffer containing the encrypted data
        console.log(new Uint8Array(wrapped));
        return window.crypto.subtle.unwrapKey(
            "raw", //the import format, must be "raw" (only available sometimes)
            wrapped, //the key you want to unwrap
            prvkey, //the private key with "unwrapKey" usage flag
            {   //these are the wrapping key's algorithm options
                name: "RSA-OAEP"
            },
            {   //this what you want the wrapped key to become (same as when wrapping)
                name: "AES-CBC",
                length: 256
            },
            false, //whether the key is extractable (i.e. can be used in exportKey)
            ["encrypt", "decrypt"] //the usages you want the unwrapped key to have
        )
    })
    .then(function (key) {
        //returns a key object
        console.log(key);
    })
    .catch(function (err) {
        console.error(err);
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant