1.0.7 • Published 5 years ago

zentinel-crypto-helper-test v1.0.7

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

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));
            
1.0.7

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago