jwk-encrypt
Version:
Encrypt and decrypt your data using JSON Web Key (JWK). Implemented using [Web Crypto Api](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API), [RSA-OAEP](https://datatracker.ietf.org/doc/html/rfc3447), [AES-CTR](https://csrc.nist.gov/publica
40 lines • 1.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.encrypt = exports.importPublicKey = exports.encryptText = void 0;
const utils_1 = require("../utils");
async function encryptText(jwk, message) {
const encoder = new TextEncoder();
return await encrypt(jwk, encoder.encode(message));
}
exports.encryptText = encryptText;
async function importPublicKey(jwk) {
const keyData = {
kty: "RSA",
e: "AQAB",
n: jwk.n,
alg: "RSA-OAEP-256",
ext: true,
};
const algo = { name: "RSA-OAEP", hash: { name: "SHA-256" } };
return await window.crypto.subtle.importKey("jwk", keyData, algo, false, [
"encrypt",
]);
}
exports.importPublicKey = importPublicKey;
async function encrypt(jwk, data) {
const iv = window.crypto.getRandomValues(new Uint8Array(16));
const key = window.crypto.getRandomValues(new Uint8Array(32));
const aes_key = await window.crypto.subtle.importKey("raw", key.buffer, "AES-CTR", false, ["encrypt"]);
const encrypted_content = await window.crypto.subtle.encrypt({
name: "AES-CTR",
counter: iv,
length: 128,
}, aes_key, data);
const rsa_public_key = await importPublicKey(jwk);
var encrypted_key = await window.crypto.subtle.encrypt({
name: "RSA-OAEP",
}, rsa_public_key, key);
return (0, utils_1.appendBuffers)((0, utils_1.appendBuffers)(iv, encrypted_key), encrypted_content);
}
exports.encrypt = encrypt;
//# sourceMappingURL=index.js.map