0.1.8 • Published 5 months ago

cwi-crypto v0.1.8

Weekly downloads
-
License
Open BSV License
Repository
github
Last release
5 months ago

cwi-crypto

This module provides a common implementation for standard cryptographic utilities, ciphers, IV management, low-level promitives and operations.

Exported Functions

NamesDescription
encrypt and decryptSymmetric AES-GCM encryption with a 32-byte prepended IV and support for data encoded as a Uint8Array or a string
XORBitwise Exclusive-OR between two Uint8Arrays of equal length
keyFromStringDerives a CryptoKey from a string with PBKDF2 using a sane iteration count and the given salt

API

encrypt(plaintext: Uint8Array|string, key: CryptoKey, returnType: string) => ciphertext: Uint8Array|string

Encrypts the given plaintext with the given key. Returns the ciphertext.

Parameters

NameDescription
plaintextThe data to encrypt. May be a String or a Uint8Array
keyThe CryptoKey object to use for the encryption operation
returnTypeA string indicating the desired return type. May be either string (default) or Uint8Array

Return Value

The function will either return a string or a Uint8Array representing the ciphertext, depending on the value of returnType.

decrypt(ciphertext: Uint8Array|string, key: CryptoKey, returnType: string) => plaintext: Uint8Array|string

Decrypts the given ciphertext with the given key. Returns the plaintext.

Parameters

NameDescription
ciphertextThe data to decrypt. May be a String or a Uint8Array
keyThe CryptoKey object to use for the decryption operation
returnTypeA string indicating the desired return type. May be either string (default) or Uint8Array

Return Value

The function will either return a string or a Uint8Array representing the plaintext, depending on the value of returnType.

keyFromString({ string: String, salt: Uint8Array }) => key: CryptoKey

Derives a suitable CryptoKey from the password string with PBKDF2 and the given salt.

Parameters

NameDescription
stringThe password string to use
salta Uint8Array representing the password salt to use

Return Value

The function will return a CryptoKey object representing the derived key.

XOR(k1: Uint8Array, k2: Uint8Array) => result: Uint8Array

Performs a bitwise exclusive OR operation with the given data. Returns the result. Inputs must be the same length.

Parameters

NameDescription
k1A Uint8Array representing the first input to XOR
k2A Uint8Array representing the second input to XOR

Return Value

The function will return a Uint8Array containing the output data.

Testing

While this is not a React application, the react-scripts package is used for testing. I tried to get Jest to work on its own with the Web Cryptography API, TextEncoder and TextDecoder, but was unable to find a suitable configuration. If anyone can get the tests to pass with only Jest, please feel free to create a pull request.

Adequate testing practices for use in a critical production application should be observed at all times for this library.

License

The license for the code in this repository is the Open BSV License.

0.1.8

5 months ago

0.1.7

5 months ago

0.1.6

5 months ago

0.1.5

5 months ago

0.1.3

1 year ago

0.1.1

2 years ago