The crypto-cipher
library provides AES encryption and decryption functionality, supporting both in-memory buffers and streams. It adheres to the NIST SP 800-38D standard recommendations.
It is part of the crypto
utility libraries and can be installed by running the following command:
npm i @alessiofrittoli/crypto-cipher
or using pnpm
pnpm i @alessiofrittoli/crypto-cipher
- Supports multiple AES algorithms (
CCM
,GCM
,OCB
,CBC
) and evenchacha20-poly1305
. - In-memory buffer encryption and decrpytion.
- Robust support for encrypting and decrypting streams (in-memory and file based), with seamless handling of key/IV extraction.
- Hybrid encryption methods for combining symmetric and asymmetric cryptography.
- A solid options resolver mechanism ensures consistent handling of defaults and constraints.
- Random
salt
andIV
generation. - Authenticated encryption modes with proper
authTag
andAdditional Authenticated Data
handling.
- Separation of concerns with clear method responsibilities.
- Comprehensive JSDoc comments enhance maintainability and readability.
Defines the minimum, maximum, and default lengths for salt.
Properties
Property | Value |
---|---|
min |
16 |
max |
64 |
default |
32 |
Defines the minimum, maximum, and default lengths for initialization vectors (IV).
Properties
Property | Value |
---|---|
min |
8 |
max |
32 |
default |
16 |
Defines the minimum, maximum, and default lengths for authentication tags.
Properties
Property | Value |
---|---|
min |
4 |
max |
16 |
default |
16 |
Defines the minimum, maximum, and default lengths for additional authenticated data (AAD).
Properties
Property | Value |
---|---|
min |
16 |
max |
4096 |
default |
32 |
Specifies default AES algorithms for buffer and stream operations.
Properties
Operation | Algorithm | Description |
---|---|---|
buffer |
aes-256-gcm |
Default algorithm used for buffer data encryption/decryption |
stream |
aes-256-cbc |
Default algorithm used for stream encryption/decryption |
Supported AES algorithms:
Properties
aes-128-gcm
aes-192-gcm
aes-256-gcm
aes-128-ccm
aes-192-ccm
aes-256-ccm
aes-128-ocb
aes-192-ocb
aes-256-ocb
aes-128-cbc
aes-192-cbc
aes-256-cbc
chacha20-poly1305
Encrypts an in-memory data buffer.
Cipher.streamEncrypt()
or Cipher.hybridEncrypt()
methods for large data encryption.
Parameters
Name | Type | Description |
---|---|---|
data |
CoerceToUint8ArrayInput |
Data to encrypt. |
secret |
CoerceToUint8ArrayInput |
Secret key for encryption. |
options |
Cph.Options |
(Optional) Additional encryption options. |
Returns
Type: Buffer
- The encrypted result buffer.
- See
CoerceToUint8ArrayInput
for more informations about supported input data types. - See
Cph.Options
for more informations about additional encryption options. - See In-memory data buffer encryption/decryption examples.
Decrypts an in-memory data buffer.
Cipher.streamDecrypt()
or Cipher.hybridDecrypt()
methods for large data decryption.
Parameters
Name | Type | Description |
---|---|---|
data |
CoerceToUint8ArrayInput |
Data to decrypt. |
secret |
CoerceToUint8ArrayInput |
Secret key for decryption. |
options |
Cph.Options |
(Optional) Decryption options (must match encryption). |
Returns
Type: Buffer
- The decrypted result buffer.
- See
CoerceToUint8ArrayInput
for more informations about supported input data types. - See
Cph.Options
for more informations about additional decryption options. - See In-memory data buffer encryption/decryption examples.
Encrypts a Readable
stream to a Writable
stream.
Parameters
Name | Type | Description |
---|---|---|
secret |
CoerceToUint8ArrayInput |
Secret key for encryption. |
options |
Cph.Stream.Symmetric.EncryptOptions |
Stream encryption options. |
Returns
Type: Cph.Stream.Symmetric.EncryptReturnType
- An object containing:
- a new instance of
crypto.Cipher
allowing you to add listeners to thecipher
encryption process. - the actual
encrypt
callback that must be called and awaited in order to start the encryption process.
- a new instance of
- See
CoerceToUint8ArrayInput
for more informations about supported input data types. - See
Cph.Stream.Symmetric.EncryptOptions
for more informations about encryption options. - See In-memory data stream encryption/decryption examples.
- See File based data stream encryption/decryption examples.
Decrypts a Readable
stream to a Writable
stream.
Parameters
Name | Type | Description |
---|---|---|
secret |
CoerceToUint8ArrayInput |
Secret key for decryption. |
options |
Cph.Stream.Symmetric.DecryptOptions |
Stream decryption options. |
Returns
Type: Promise<Cph.Stream.Symmetric.DecryptReturnType>
- A new Promise that resolves when Key IV extraction completes returning an object containing:
- a new instance of
crypto.Decipher
allowing you to add listeners to thedecipher
decryption process. - the actual
decrypt
callback that must be called and awaited in order to start the decryption process.
- a new instance of
- See
CoerceToUint8ArrayInput
for more informations about supported input data types. - See
Cph.Stream.Symmetric.DecryptOptions
for more informations about decryption options. - See In-memory data stream encryption/decryption examples.
- See File based data stream encryption/decryption examples.
Encrypts a stream using hybrid encryption (symmetric + RSA).
Parameters
Name | Type | Description |
---|---|---|
secret |
CoerceToUint8ArrayInput |
Symmetric secret key. |
publicKey |
crypto.RsaPublicKey | crypto.KeyLike |
RSA public key used to encrypt the generated symmetric key. |
options |
Cph.Stream.Hybrid.EncryptOptions |
Stream encryption options. |
Returns
Type: Cph.Stream.Hybrid.EncryptReturnType
- An object containing:
- a new instance of
cipher
allowing you to add listeners to thecipher
encryption process. - the actual
encrypt
callback that must be called and awaited to start the encryption process.
- a new instance of
- See
CoerceToUint8ArrayInput
for more informations about supported input data types. - See
Cph.Stream.Hybrid.EncryptOptions
for more informations about encryption options. - See In-memory data stream with hybrid encryption/decryption examples.
- See File based data stream with hybrid encryption/decryption examples.
Decrypts a stream using hybrid decryption (symmetric + RSA).
Parameters
Name | Type | Description |
---|---|---|
privateKey |
crypto.RsaPrivateKey | crypto.KeyLike |
RSA private key for used to decrpyt the encrypted symmetric key. |
options |
Cph.Stream.Hybrid.DecryptOptions |
Stream decryption options. |
Returns
Type: Promise<Cph.Stream.Hybrid.DecryptReturnType>
- A new Promise that resolves when Key IV extraction completes returning an object containing:
- a new instance of
crypto.Decipher
allowing you to add listeners to thedecipher
decryption process. - the actual
decrypt
callback that must be called and awaited in order to start the decryption process.
- a new instance of
- See
Cph.Stream.Hybrid.DecryptOptions
for more informations about decryption options. - See In-memory data stream with hybrid encryption/decryption examples.
- See File based data stream with hybrid encryption/decryption examples.
This module supports different input data types and it uses the coerceToUint8Array
utility function from @alessiofrittoli/crypto-buffer
to convert it to a Uint8Array
.
- See
coerceToUint8Array
for more informations about the supported input types.
Cipher CBC algorithm types.
Supported AES algorithm types.
Common options in encryption/decryption processes.
Type parameters
Parameter | Default | Description |
---|---|---|
T |
Cph.AesAlgorithm |
Accepted algorithm in Cph.Options . This is usefull to constraint specifc algorithms. |
Properties
Property | Type | Default | Description |
---|---|---|---|
algorithm |
T |
aes-256-gcm | aes-256-cbc |
Accepted algorithms. |
salt |
number |
32 |
The salt length in bytes. Minimum: 16 , Maximum: 64 . |
iv |
number |
16 |
The Initialization Vector length in bytes. Minimum: 8 , Maximum: 32 . |
authTag |
number |
16 |
The authTag length in bytes. Minimum: 4 , Maximum: 16 . |
aad |
CoerceToUint8ArrayInput |
- | Custom Additional Authenticated Data. aadLength is then automatically resolved. If not provided, a random AAD is generated with a max length of aadLength . |
aadLength |
number |
32 |
The auto generated AAD length in bytes. Minimum: 16 , Maximum: 128 . |
Stream symmetric encryption options.
- Extends
Cph.Options<Cph.CBCTypes>
.
Properties
Property | Type | Description |
---|---|---|
input |
Readable |
The Readable Stream from where raw data to encrypt is read. |
output |
Writable |
The Writable Stream where encrypted data is written. |
Returnign object from Cipher.streamEncrypt()
method.
Properties
Property | Type | Description |
---|---|---|
cipher |
crypto.Cipher |
The crypto.Cipher instance. |
encrypt |
() => Promise<void> |
The actual encrypt callback that must be called and awaited in order to start the encryption process. |
Stream symmetric decryption options.
- Extends
Cph.Stream.Symmetric.EncryptOptions
.
Properties
Property | Type | Description |
---|---|---|
input |
Readable |
The Readable Stream from where encrypted data is read. |
output |
Writable |
The Writable Stream where decrypted data is written. |
Returnign object from awaited Cipher.streamDecrypt()
method.
Properties
Property | Type | Description |
---|---|---|
decipher |
crypto.Decipher |
The crypto.Decipher instance. |
decrypt |
() => Promise<void> |
The actual decrypt callback that must be called and awaited in order to start the decryption process. |
Stream hybrid encryption options.
- Alias for
Cph.Stream.Symmetric.EncryptOptions
Returnign object from Cipher.hybridEncrypt()
method.
- Alias for
Cph.Stream.Symmetric.EncryptReturnType
Stream hybrid decryption options.
- Extends
Cph.Stream.Symmetric.DecryptOptions
.
Properties
Property | Type | Description |
---|---|---|
rsaKeyLength |
number |
The RSA key length in bytes used while encrypting data. This is used to properly extract the encrypted Cipher Key and Initialization Vector from the encrypted data. |
Returnign object from awaited Cipher.hybridDecrypt()
method.
- Alias for
Cph.Stream.Symmetric.DecryptReturnType
import { Cipher } from '@alessiofrittoli/crypto-cipher'
import type { Cph as CipherTypes } from '@alessiofrittoli/crypto-cipher/types'
The simpliest way to encrypt/decrypt in-memory data buffers.
// encrypt
const data = 'my top-secret data'
const password = 'my-very-strong-password'
const encrypted = Cipher.encrypt( data, password )
// decrypt
const decrypted = Cipher.decrypt( encrypted, password )
console.log( decrypted ) // Outputs: my top-secret data
The in-memory data stream comes pretty handy when, for example, we need to stream encrypted data within a Server Response or to decrypt stream data from a Server Response.
// /api/stream-encrypt
import { Readable, Writable } from 'stream'
const routeHandler = () => {
const data = 'my top-secret data'
const password = 'my-very-strong-password'
const stream = new TransformStream()
const writer = stream.writable.getWriter()
// Create a `Readable` Stream with raw data.
const input = new Readable( {
read()
{
this.push( data )
this.push( null ) // Signal end of stream
},
} )
// `Writable` Stream where encrypted data is written
const output = new Writable( {
write( chunk, encoding, callback )
{
writer.write( chunk )
callback()
},
final( callback )
{
writer.close()
callback()
}
} )
Cipher.streamEncrypt( password, { input, output } )
.encrypt()
return (
// encrypted stream
new Response( stream.readable )
)
}
// /api/stream-decrypt
import { Transform, Writable } from 'stream'
import { StreamReader } from '@alessiofrittoli/stream-reader'
const password = 'my-very-strong-password'
const routeHandler = () => (
fetch( '/api/stream-encrypt' )
.then( response => {
if ( ! response.body ) {
return (
new Respone( null, { status: 400 } )
)
}
const stream = new TransformStream()
const writer = stream.writable.getWriter()
const reader = new StreamReader( response.body )
const input = new Transform()
reader.read()
reader.on( 'read', chunk => {
input.push( chunk )
} )
reader.on( 'close', () => {
input.push( null )
} )
const output = new Writable( {
write( chunk, encoding, callback )
{
writer.write( chunk )
callback()
},
final( callback ) {
writer.close()
callback()
},
} )
const { decrypt } = await Cipher.streamDecrypt( password, { input, output } )
decrypt()
return (
// decrypted stream
new Response( stream.readable )
)
} )
)
Hybrid encryption offers an higher level of security by encrypting the generated symmetric key with asymmetric RSA keys.
const password = 'my-very-strong-password'
/** RSA modulus length is required for proper key extraction during decryption process. */
const rsaKeyLength = 512 // bytes
const keyPair = crypto.generateKeyPairSync( 'rsa', {
modulusLength : rsaKeyLength * 8, // 4096 bits
publicKeyEncoding : { type: 'spki', format: 'pem' },
privateKeyEncoding : { type: 'pkcs1', format: 'pem' },
} )
// or you can optionally set a custom passphrase
const keyPair = crypto.generateKeyPairSync( 'rsa', {
modulusLength : rsaKeyLength * 8, // 4096 bits
publicKeyEncoding : { type: 'spki', format: 'pem' },
privateKeyEncoding : { type: 'pkcs1', format: 'pem', passphrase: password, cipher: 'aes-256-cbc' },
} )
const data = 'my top-secret data'
/** Store encrypted chunks for next example. */
const encryptedChunks: Buffer[] = []
// Create a `Readable` Stream with raw data.
const input = new Readable( {
read()
{
this.push( data ) // Push data to encrypt
this.push( null ) // Signal end of stream
},
} )
// Create a `Writable` Stream where encrypted data is written
const output = new Writable( {
write( chunk, encoding, callback )
{
// push written chunk to `encryptedChunks` for further usage.
encryptedChunks.push( chunk )
callback()
}
} )
const { encrypt } = Cipher.hybridEncrypt( password, {
key : keyPair.publicKey,
padding : crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash : 'SHA-256',
}, { input, output } )
await encrypt()
/** Store decrypted chunks. */
const chunks: Buffer[] = []
// Create a `Readable` Stream with encrypted data.
const input = new Readable( {
read()
{
this.push( Buffer.concat( encryptedChunks ) ) // Push data to decrypt
this.push( null ) // Signal end of stream
},
} )
// Create a `Writable` Stream where decrypted data is written
const output = new Writable( {
write( chunk, encoding, callback )
{
chunks.push( chunk )
callback()
},
} )
const { decrypt } = await Cipher.hybridDecrypt(
{
key : keyPair.privateKey,
passphrase: password, // optional passhrase (required if set while generating keypair).
padding : crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash : 'SHA-256',
}, { input, output, rsaKeyLength }
)
await decrypt()
console.log( Buffer.concat( chunks ).toString() ) // Outputs: 'my top-secret data'
Nothig differs from the In-memory data stream encryption/decryption example, except for input
and output
streams which now comes directly from files reading/writing.
import fs from 'fs'
const password = 'my-very-strong-password'
// input where raw data to encrypt is read
const input = fs.createReadStream( 'my-very-large-top-secret-file.pdf' )
// output where encrypted data is written
const output = fs.createWriteStream( 'my-very-large-top-secret-file.encrypted' )
// encrypt
await Cipher.streamEncrypt( password, { input, output } )
.encrypt()
import fs from 'fs'
const password = 'my-very-strong-password'
// input where encrypted data is read
const input = fs.createReadStream( 'my-very-large-top-secret-file.encrypted' )
// output where decrypted data is written
const output = fs.createWriteStream( 'my-very-large-top-secret-file-decrypted.pdf' )
// decrypt
const { decrypt } = await Cipher.streamDecrypt( password, { input, output } )
await decrypt()
Nothig differs from the In-memory data stream with hybrid encryption/decryption example, except for input
and output
streams which now comes directly from files reading/writing.
const password = 'my-very-strong-password'
/** RSA modulus length is required for proper key extraction during decryption process. */
const rsaKeyLength = 512 // bytes
const keyPair = crypto.generateKeyPairSync( 'rsa', {
modulusLength : rsaKeyLength * 8, // 4096 bits
publicKeyEncoding : { type: 'spki', format: 'pem' },
privateKeyEncoding : { type: 'pkcs1', format: 'pem' },
} )
// or you can optionally set a custom passphrase
const keyPair = crypto.generateKeyPairSync( 'rsa', {
modulusLength : rsaKeyLength * 8, // 4096 bits
publicKeyEncoding : { type: 'spki', format: 'pem' },
privateKeyEncoding : { type: 'pkcs1', format: 'pem', passphrase: password, cipher: 'aes-256-cbc' },
} )
import fs from 'fs'
const password = 'my-very-strong-password'
// input where raw data to encrypt is read
const input = fs.createReadStream( 'my-very-large-top-secret-file.pdf' )
// output where encrypted data is written
const output = fs.createWriteStream( 'my-very-large-top-secret-file.encrypted' )
// encrypt
const { encrypt } = Cipher.hybridEncrypt( password, {
key : keyPair.publicKey,
padding : crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash : 'SHA-256',
}, { input, output } )
await encrypt()
import fs from 'fs'
const password = 'my-very-strong-password'
// input where encrypted data is read
const input = fs.createReadStream( 'my-very-large-top-secret-file.encrypted' )
// output where decrypted data is written
const output = fs.createWriteStream( 'my-very-large-top-secret-file-decrypted.pdf' )
// decrypt
const { decrypt } = await Cipher.hybridDecrypt(
{
key : keyPair.privateKey,
passphrase: password, // optional passhrase (required if set while generating keypair).
padding : crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash : 'SHA-256',
}, { input, output, rsaKeyLength }
)
npm install
or using pnpm
pnpm i
Run the following command to test and build code for distribution.
pnpm build
warnings / errors check.
pnpm lint
Run all the defined test suites by running the following:
# Run tests and watch file changes.
pnpm test:watch
# Run tests in a CI environment.
pnpm test:ci
- See
package.json
file scripts for more info.
Run tests with coverage.
An HTTP server is then started to serve coverage files from ./coverage
folder.
test:coverage:serve
Contributions are truly welcome!
Please refer to the Contributing Doc for more information on how to start contributing to this project.
Help keep this project up to date with GitHub Sponsor.
If you believe you have found a security vulnerability, we encourage you to responsibly disclose this and NOT open a public issue. We will investigate all legitimate reports. Email security@alessiofrittoli.it
to disclose any security vulnerabilities.
|
|