Encrypt and decrypt in Node and Bash
The package main exports two functions, encrypt
and decrypt
import {
encrypt,
decrypt
} from '@sequencemedia/crypto'
function encrypt(
buffer: Buffer,
secret: string,
bytes?: number,
algorithm?: string
): Buffer
Only buffer
and secret
are required
buffer
is the data to encryptsecret
is the secret key to use for encryption
Both bytes
and algorithm
are optional
bytes
is the number of random bytes to use for the encryption initialisation vector. The default is16
algorithm
is the algorithm to use for encryption. The default isaes-256-ctr
function decrypt(
buffer: Buffer,
secret: string,
bytes?: number,
algorithm?: string
): Buffer
Only buffer
and secret
are required
buffer
is the data to decryptsecret
is the secret key to use for decryption
Both bytes
and algorithm
are optional
bytes
is the number of bytes to slice from the buffer for the decryption initialisation vector. The default is16
algorithm
is the algorithm to use for decryption. The default isaes-256-ctr
The package contains three scripts
crypto.sh
encrypt.sh
decrypt.sh
Script crypto.sh
exports four functions to consume in your own Bash scripts
source ./crypto.sh
encrypt
decrypt
encrypt_directory
decrypt_directory
Scripts encrypt.sh
and decrypt.sh
can be executed at the command line
Requires CRYPTO_KEY
as a variable in the Bash environment and a file path to encrypt
CRYPTO_KEY='secret'
encrypted_file_data=$(encrypt "./file.txt")
CRYPTO_KEY='secret'
encrypt "./file.txt" > "./encrypted.txt"
Requires CRYPTO_KEY
as a variable in the Bash environment and a file path to decrypt
CRYPTO_KEY='secret'
file_data=$(decrypt "./encrypted.txt")
CRYPTO_KEY='secret'
decrypt "./encrypted.txt" > "./file.txt"
Requires CRYPTO_KEY
as a variable in the Bash environment and a directory path to encrypt
- The first argument is the origin directory of files to encrypt
- The second argument is the destination directory for the encrypted files
CRYPTO_KEY='secret'
encrypt_directory "./directory" "./encrypted"
Requires CRYPTO_KEY
as a variable in the Bash environment and a directory path to decrypt
- The first argument is the origin directory of files to decrypt
- The second argument is the destination directory for the decrypted files
CRYPTO_KEY='secret'
decrypt_directory "./encrypted" "./directory"
Requires CRYPTO_KEY
as a variable in the Bash environment
- You can provide either file or directory paths
- You can provide
--verbose
or-v
- A file origin path to encrypt
- A file destination path for the encrypted file
CRYPTO_KEY='secret' ./encrypt.sh \
--origin "./file.txt" \
--destination "./encrypted.txt"
- A directory origin path to encrypt
- A directory destination path for encrypted files
CRYPTO_KEY='secret' ./encrypt.sh \
--origin "./directory" \
--destination "./encrypted"
Requires CRYPTO_KEY
as a variable in the Bash environment
- You can provide either file or directory paths
- You can provide
--verbose
or-v
- A file origin path to decrypt
- A file destination path for the decrypted file
CRYPTO_KEY='secret' ./decrypt.sh \
--origin "./encrypted.txt" \
--destination "./file.txt"
- A directory origin path to decrypt
- A directory destination path for decrypted files
CRYPTO_KEY='secret' ./decrypt.sh \
--origin "./encrypted" \
--destination "./directory"