Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import forge from 'node-forge';
import sjcl from 'sjcl';
import { Base64 } from "js-base64";
export class PassmanCrypto {
static generateRSAKeypair = (keyLength = 2048) => {
return new Promise(function (resolve, reject) {
// generate an RSA key pair asynchronously (uses web workers if available)
// use workers: -1 to run a fast core estimator to optimize # of workers
forge.pki.rsa.generateKeyPair({
bits: keyLength,
workers: 2
}, (error, keypair) => {
resolve({ error, keypair });
});
});
};
static rsaKeyPairToPEM = (keypair) => {
return {
privateKey: forge.pki.privateKeyToPem(keypair.privateKey),
publicKey: forge.pki.publicKeyToPem(keypair.publicKey)
};
};
static sjcl_encryption_config = {
adata: "",
iter: 1000,
ks: 256,
mode: 'ccm',
ts: 64,
//salt: [],
//iv: []
};
static encryptString = (plainText, key) => {
// todo: think about replacing aes-ccm from sjcl with the more modern and faster aes-gcm from forge
// see https://crypto.stackexchange.com/questions/6842/how-to-choose-between-aes-ccm-and-aes-gcm-for-storage-volume-encryption
// see https://github.com/digitalbazaar/forge#cipher
// todo: try to use aes-ccm from jscrypto instead of the very outdated sjcl
// see https://github.com/Hinaser/jscrypto/blob/master/API.md#aes
let rp = {};
const ct = sjcl.encrypt(key, plainText, PassmanCrypto.sjcl_encryption_config, rp);
return Base64.btoa(ct);
};
/**
* @param b64EncCiphertext
* @param key
* @throws untyped sjcl exceptions
*/
static decryptString = (b64EncCiphertext, key) => {
const ciphertext = Base64.atob(b64EncCiphertext);
return sjcl.decrypt(key, ciphertext);
};
}
|