@debitoor/cipher
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

cipher

Build Status Coverage Status npm (scoped)

Encrypt/decrypt objects using aes-256-cbc algorithm

Install

npm i -SE @debitoor/cipher

Usage

const cipher = require('@debitoor/cipher')('secret');

try {
    const encrypted = cipher.encrypt({userId: '123456'});
    console.log(encrypted); // { iv: 'eb0911c423161f0488337e5007887581', data: 'fd9612df14729ec373214f151b62fab74f8d7c5756082e4d057632dc5ea8d088' }
    
    const decrypted = cipher.decrypt(encrypted);
    console.log(decrypted); // { userId: '123456' }

} catch (e) {
	// Handle error during encrypting/decrypting
}

API

  • cipher(secret) - return cipher instance with given secret key
  • cipherInstance.encrypt(json) - encrypt object with secret and random initialization vector. Returns {iv, data} where iv - initialization vector, data - encrypted object. Throws error if json is invalid.
  • cipherInstance.decrypt({iv, data}) - decrypt previously encrypted object. Takes {iv, data} as argument where iv - initialization vector¹, data - encrypted object. Throws error if wrong secret key or incorrect data provided.

¹ (https://nodejs.org/api/crypto.html#crypto_crypto_createcipheriv_algorithm_key_iv_options): Initialization vectors should be unpredictable and unique; ideally, they will be cryptographically random. They do not have to be secret: IVs are typically just added to ciphertext messages unencrypted. It may sound contradictory that something has to be unpredictable and unique, but does not have to be secret; it is important to remember that an attacker must not be able to predict ahead of time what a given IV will be.

Readme

Keywords

Package Sidebar

Install

npm i @debitoor/cipher

Weekly Downloads

106

Version

1.1.2

License

MIT

Unpacked Size

5.81 kB

Total Files

5

Last publish

Collaborators

  • andrei-at-sumup
  • mtlarva
  • david-sumup
  • filipecorrea
  • moustafa-sumup
  • shadowmaru
  • silchenko-sumup
  • zaaom
  • dzmitry.h.sumup
  • andriimykhailovsumup
  • jonathanmh
  • talar.boyadjian
  • mmahrous
  • maciej-kusnierz
  • oalpatov
  • bgeraymovich
  • debitoor-bot
  • mamant
  • hilleer
  • s0ph1e
  • sumup.vascofg
  • igor_torba
  • igor.g
  • ituraj
  • hamoudaarfaoui
  • soulmonk
  • andriyonyshchenko
  • belrestro
  • connor_baer
  • balexandre
  • mickael-h
  • felixjung
  • oarostovtseva
  • mcntsh
  • ketansumup
  • zetxek