basic-crypter
v1.0.2
Published
Data Encryption and Decryption wrapper around node js crpto module, Uses scrypt for key deriviation and handles the data encryption algorithm independent via <code>crypto.createCipheriv</code>. supports all algorithms that are able to use auth tags
Downloads
204
Maintainers
Readme
Data Encrypter and Decrypter
Encrypts and decrypts data using scrypt for key deriviation and all availiable algorithms in crypto.createCipherIv, this is just a wrapper for the node crypto module which does all the heavy lifting
Usage
Make sure the key is an object that can be interpreted as a buffer If the program fails to generate a buffer from the key it will throw The Default algorithm is aes-256-gcm
import { DataCrypter } from "basic-crypter"
const crypter = new DataCrypter();
const key = "test";
const data = "Very Secret Data";
const encrypted_data = await crypter.EncryptData(data,key);
const decrypted_data = await crypter.DecryptData(encrypted_data,key);
if (decrypted_data === data){
console.log("Success!");
}
Use a Different Algorithm
Currently only algorithms that are able to use auth tags are supported. If there is demand for it i will make it so others work as well create a github issue. If you attempt to use an algorithm that does not support auth tags it will throw. If you put in the wrong values for key size and iv size it will throw.
import { DataCrypter } from "basic-crypter"
// algorithm, key size (bytes), iv size (bytes)
const crypter = new DataCrypter("chacha20-poly1305", 32, 12);
const key = "test";
const data = "Very Secret Data";
const encrypted_data = await crypter.EncryptData(data,key);
const decrypted_data = await crypter.DecryptData(encrypted_data,key);
if (decrypted_data === data){
console.log("Success!");
}
Encoding of input and output
By default the ouput of encryptData is a buffer and the output of decryptData is in utf8. Here is how you can change it.
import { DataCrypter } from "basic-crypter"
// leave rest undefined to use default options
// ascii is the output encoding and hex the data encoding to use on input to decrypt and output on encrypt
const crypter = new DataCrypter(undefined,undefined,undefined,"ascii","hex");
const key = "test";
const data = "Very Secret Data";
const encrypted_data = await crypter.EncryptData(data,key);
console.log(encrypted_data); // in hex
const decrypted_data = await crypter.DecryptData(encrypted_data,key);
console.log(decrypted_data); // in ascii
if (decrypted_data === data){
console.log("Success!");
}
Supported Algorithms
Depends on node crypto.createCipherIv() The Algorithm must support node cipher.getAuthTag() If you are unsure if the algorithm is supported just try it out or use the default which should be fine for most use cases Run openssl list -cipher-algorithms in your terminal to see availiabe algorithms (this does not mean that they are supported)
Source Code and License
See github