1.0.7 • Published 5 years ago
zentinel-crypto-helper-test v1.0.7
Zentinel-crypto-helper
This is a wrapper around the Web Crypto API available in modern browsers for integration with Zentinel.
Usage
import { Converter, Crypto } from 'zentinel-crypto-helper';
const crypto = new Crypto();
const testData = 'the message we want to encrypt';
(async () => {
// generate keys
const aesKey = await crypto.generateAESKey();
const exportedAesKey = await crypto.exportAES(aesKey);
const rsaKeys = await crypto.generateRSAKeys();
const ecdsaKeys = await crypto.generateECDSAKeys();
// ***************************
// sign and encrypt data
const signature = await crypto.signByECDSA(testData, ecdsaKeys.privateKey);
const { encrypted, iv } = await crypto.encryptByAES(testData, aesKey);
const encryptedAesKey = await crypto.encryptByRSA(
exportedAesKey,
rsaKeys.publicKey,
);
// check sign and decrypt data
const decryptedAesKey = await crypto.decryptByRSA(
encryptedAesKey,
rsaKeys.privateKey,
);
const importedAesKey = await crypto.importAES(decryptedAesKey);
const decryptedData = await crypto.decryptByAES(
{ iv, encrypted },
importedAesKey,
);
const verifySignData = await crypto.verifyByECDSA(
Converter.arrayBufferToString(decryptedData),
signature,
ecdsaKeys.publicKey,
);
return {
iv: Converter.arrayBufferToBase64String(iv),
signature: Converter.arrayBufferToBase64String(signature),
encryptedData: Converter.arrayBufferToBase64String(encrypted),
exportedAesKey: Converter.arrayBufferToBase64String(exportedAesKey),
};
})().then((locker) => console.log('EXAMPLE LOCKER :: ', locker));