New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

cwi-crypto

Package Overview
Dependencies
Maintainers
3
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cwi-crypto

Common implementations for standard cryptographic utilities

latest
Source
npmnpm
Version
0.1.8
Version published
Weekly downloads
8
33.33%
Maintainers
3
Weekly downloads
 
Created
Source

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.

FAQs

Package last updated on 10 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts