1.1.0 • Published 2 years ago

@runninghill/simple-encryption v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Runninghill Logo

Simple Encryption

Please note that this is the README for CONSUMERS, if you plan on contributing to this package, please take a look here.

A simple encryption package that allows you to encrypt and decrypt data.

Getting Started 🏁

If you plan on contributing to the package, please read the contributor readme.

  1. Run npm install @runninghill/simple-encryption.

  2. Import and use the exposed classes to encrypt/decrypt your data.

Usage 💡

To begin using Simple Encryption, create an instance of the Encryptor class, passing in the appropriate configuration (optional).

It is recommended that you configure the secret, this is your private key for the encryption.

To understand what you can configure, check out the API reference.

NB: In order to decrypt data, the configuration of the Encryptor instance must be the same as the one used to initially encrypt the data.

const { Encryptor } = require('@runninghill/simple-encryption')

const encryptor = new Encryptor({
    saltRounds: 15,
    secret: 'a9479590-299c-4812-9636-a333a97ca6cc'
})

To encrypt data, simply call the encrypt method:

const user = {
    name: 'John',
    surname: 'Doe',
    age: 31
}

const encryption = await encryptor.encrypt(user)

To decrypt data, simply call the decrypt method:

const decryptedData = await encryptor.decrypt(encryption)
// decryptedData = {
//     name: 'John',
//     surname: 'Doe',
//     age: 31
// }

API Reference 📖

Classes

Encryptor

A class that allows you to encrypt and decrypt data. It also provides optional configuration.

Configuration

NameTypeDescriptionDefault
secretstringthe private key for the encryption'default'
saltRoundsnumberthe amount of rounds to generate the salt, used in the creation of the encryption key. The greater the amount of rounds the more resource intensive the encryption will betrue
saltPlacementStrategySaltPlacementStrategythe strategy for placing the salt in the encryptionSaltPlacementStrategy.Before

Methods

Encrypt

Encrypts the provided data using the configuration of the Encryptor instance.

The provided data can be primitive or an object.

Returns: a promise with a string, the encrypted data with the salt placed inside it.

Parameters

NameTypeDescriptionDefault
dataanythe data to be encrypted

Decrypt

Decrypts the provided encryption using the configuration of the Encryptor instance.

Returns: a promise with the primitive data or object that was encrypted

Parameters

NameTypeDescriptionDefault
encryptionstringthe result from the encrypt method; the encrypted data with the salt placed inside it

Enums

SaltPlacementStrategy

Determines how the salt is placed in the encrypted data

Values

NameDescription
Beforethe salt is placed before the encrypted data
Afterthe salt is placed after the encrypted data