npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

adonis-encrypter

v1.0.0

Published

Encryption provider for adonis framework which uses a static initialization vector (IV)

Downloads

2

Readme

Coverage Status Build Status js-standard-style

Adonis Encrypter

Adonis Encryption is based on the official encryption provider that ships with Adonis, the only difference is that it uses a Static Initialization Vector for encryption instead of a dynamic one as the official encrypter.

Why Adonis Encrypter?

This fork was born because we had the need to encrypt our database but still be able to query thru the encrypted data, picture the following scenario:

Use case

You have the following table in your database

| field | type | length | index | |------------|:---------:|:------:|:--------| | id | integer | 11 | primary | | username | string | 255 | index | | email | string | 255 | index | | password | string | 255 | | | crated_at | timestamp | | | | updated_at | timestamp | | | | deleted_at | timestamp | |

Fields username and email where encrypted by Adonis official encryption provider using a dynamic initialization vector, that is to say, it generates a different encrypted string everytime you encrypt the data... You see the problem? if not, please continue reading.

The problem

The problem originates when you want to do a search on your encrypted data, you would normally encrypt the text and search by the encrypted string, but remember that same data won't result in the same string when encrypted again? since both strings, the one in the database and you recently encrypted text aren't the same, you won't get any matches.

The solution

The solution is very simple, actually, that is the only difference between this provider and the official one (all credit goes to Harminder Virk) is that this one uses a static IV, that's to say it does not generate an IV on each operation, you provide the IV you want to use to encrypt/decrypt your data.

By using a static IV, when you encrypt two "Hello", the resulting encrypted string will be the same and you will be able to store it in your database and search for your "Hello" value using its encrypted counterpart.

NOTE: You can use both encryption providers in the same project, deboting this one to scenarios like the explained above.

Authoring clarification

We don't like to take credit on something we didn't code ourselves, all credit goes for the original author since we just did an adaptation to our needs.

Getting Started

By installing adonis-framework you would have any dependencies covered, so you can just run

$ npm i --save adonis-encrypter

Configuration

Register the provider in your bootstrap/app.js file.

const providers = [
  'adonis-encrypter/providers/EncrypterProvider'
]

const aliases = {
  Encrypter: 'Pixeleur/Addons/Encrypter'
}

next generate your IV, a random 16 characters long alphanumeric string and add it to your ./env file

APP_IV=aRnd16CharString

next read your IV string into your Config provider, just below your App Key definition

  /*
  |--------------------------------------------------------------------------
  | App IV
  |--------------------------------------------------------------------------
  |
  | App IV is a 16 characters long Initialization Vector required
  | to encrypt/decrypt sensitive data.
  |
  | Specifying an IV will allow you to always generate the same string
  | while encrypting data, so you will be able to do encrypted database searchs.
  |
  | Do not specify if you want more security (although you won't have encrypted database searchs).
  |
  */
  appIV: Env.get('APP_IV', false),

How to use

Import Encrypter in your class

const Encrypter = use('Encrypter') 

next, use like you normally do with the official Encryption provider

Encrypt

let encrypted = Encrypter.encrypt(plainText)

Decrypt

let plainText = Encrypter.decrypt(decrypted)

Implement Model Getters and Setters

Manually encrypting and decrypting data coming from database may be tedious and is not scalable/maintainable, a better approach would be to implement getters and setters on your model:

'use strict'

const Lucid = use('Lucid')
const Encrypter = use('Encrypter')

class User extends Lucid {

  // Override table name
  static get table () {
      return 'user'
  }

  // Decrypt email after read
  getEmail (email) {
    email = Encrypter.decrypt(email)
    return email
  }

  // Encrypt email before write
  setEmail (email) {
    email = Encrypter.encrypt(email)
    return email
  }
}

module.exports = User

Contributing guidelines

In favor of active development we accept contributions for everyone. You can contribute by submitting a bug, creating pull requests or even improving documentation.

You can find a complete guide to be followed strictly before submitting your pull requests in the Adonis Official Documentation.