la-crypta
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

La Crypta

A zero-dependency Node.js library that let's you encrypt/decrypt specific fields on any object. It's purpose is to help with client-side encryption.

  • Small: aproximately only 8 kB.

  • Simple: can only encrypt/decrypt string and string[] fields.

  • Flexible: it allows you to encrypt/decrypt specific fields in any object regardless of how nested they are.

  • Fully-typed: it shows you which fields can be encrypted/decrypted for a specific type in a dotted path manner.

Usage

Instantiation

import crypto from "crypto";
import { LaCrypta } from "la-crypta";

const key: Buffer = crypto.randomBytes(32);
const ivSeed: string = new Date().toISOString(); /* should be random to generate the iv */

const laCrypta = LaCrypta(key, ivSeed);

Object

Let use the type ExampleType.

type ExampleType = {
  prop1: string;
  nested: {
    prop2: number;
    prop3: string[];
    prop6: string;
  };
  nestedArray: {
    prop4: number[];
    prop5: string[];
  }[];
};

Now we instantiate the type. Notice that the encryptable/decryptable fields could be: prop1, nested.prop3, nested.prop6 and nesteArray.prop5. That's because they are of type string and string[].

const obj: ExampleType = {
  prop1: "a",
  nested: {
    prop2: 1,
    prop3: ["a", "b", "c"],
    prop6: "b",
  },
  nestedArray: [
    {
      prop4: [1, 2, 3],
      prop5: ["q", "w", "e"],
    },
    {
      prop4: [4, 5, 6],
      prop5: ["r", "t", "y"],
    },
  ],
};

Now we can encrypt specific fields of obj. Notice that obj is modified inplace.

laCrypta.cipherObject(["prop1", "nested.prop6", "nestedArray.prop5"], obj);

console.log(obj);
/*
{
  prop1: '<<encrypted text>>',
  nested: {
    prop2: 1,
    prop3: ['a', 'b', 'c'],
    prop6: '<<encrypted text>>',
  },
  nestedArray: [
    {
      prop4: [1, 2, 3],
      prop5: [
        '<<encrypted text>>',
        '<<encrypted text>>',
        '<<encrypted text>>'
      ],
    },
    {
      prop4: [4, 5, 6],
      prop5: [
        '<<encrypted text>>',
        '<<encrypted text>>',
        '<<encrypted text>>'
      ],
    },
  ],
}
*/

Finally, we can decrypt the properties we previously encrypted.

laCrypta.decipherObject(["prop1", "nested.prop6", "nestedArray.prop5"], obj);

console.log(obj);
/*
{
  prop1: 'a',
  nested: {
    prop2: 1,
    prop3: ['a', 'b', 'c'],
    prop6: 'b',
  },
  nestedArray: [
    {
      prop4: [1, 2, 3],
      prop5: ['q', 'w', 'e'],
    },
    {
      prop4: [4, 5, 6],
      prop5: ['r', 't', 'y'],
    },
  ],
}
*/

String array

We can also encrypt and decrypt a string[] by itself.

const arr = ["one", "two", "three"];
const encrypted = laCrypta.cipherArray(arr);
// encrypted now is ['<<encrypted text>>', '<<encrypted text>>', '<<encrypted text>>']
const decrypted = laCrypta.decipherArray(encrypted);
// decrypted is equal to arr

String

And encrypt and decrypt a string as well.

const str = "hola";
const encrypted = laCrypta.cipherArray(arr);
// encrypted now is '<<encrypted text>>'
const decrypted = laCrypta.decipherArray(encrypted);
// decrypted is equal to str

API

Object

cipherObject(fieldsToEncrypt: PropPath<T>[], document: T): T;

Receives:

  • fieldsToEncrypt: an array of strings in which each string is a dotted path to an encryptable property in document.
  • document: an object whose fieldsToEncrypt fields will be encrypted.

Returns:

  • Nothing, because document is modified inplace.
decipherObject(fieldsToDecrypt: PropPath<T>[], document: T): T;

Receives:

  • fieldsToDecrypt: an array of strings in which each string is a dotted path to an decryptable property in document.
  • document: an object whose fieldsToDecrypt fields will be decrypted.

Returns:

  • Nothing, because document is modified inplace.

String array

cipherArray(values: string[]): string[];

Receives:

  • values: an array of strings to encrypt.

Returns:

  • string[]: values but with its contents encrypted.
decipherArray(values: string[]): string[];

Receives:

  • values: an array of strings to decrypt.

Returns:

  • string[]: values but with its contests decrypted.

String

cipherValue(value: string): string;

Receives:

  • value: a string to encrypt.

Returns:

  • value but encrypted.
decipherValue(value: string): string;

Receives:

  • value: a string to decrypt.

Returns:

  • value but decrypted.

Package Sidebar

Install

npm i la-crypta

Weekly Downloads

0

Version

0.0.2

License

MIT

Unpacked Size

12.8 kB

Total Files

11

Last publish

Collaborators

  • bloopblip
  • fresh-avocado
  • macarenaoyague