1.0.0 • Published 3 months ago

safecookies v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

safe-cookies

Wrap the cookie libraries with a safer encrypt - decrypt function - should work with most libraries.

The encrypt, decrypt function wraps a normal setter (encrypt) and getter (decrypt) functions with crypto functions that help create safer value storage options for procedures like localStorage, cookies, password storage in database etc.

Do take a look at the architectural concept here

You can find the demos here

USAGE ONE:

import pkg from "safecookie";
const { encrypt, decrypt, encryptRecursive, decryptRecursive, cryptoencrypt, cryptodecrypt, getKeyFromPassword } = pkg;

console.log("Testing new crypter");

let cryptedtext = encrypt((v) => { console.log(v.toString("base64")); return v; }, getKeyFromPassword("password", "testsalt"), 0, cryptoencrypt)("Testing new crypter");
console.log(cryptedtext.toString("base64"));

let decryptedtext = decrypt((v) => { console.log(v.toString("base64")); return v; }, getKeyFromPassword("password", "testsalt"), 0, cryptodecrypt)(cryptedtext);
console.log(decryptedtext.toString());

USAGE TWO:

import pkg from "safecookie";
const { encrypt, decrypt, encryptRecursive, decryptRecursive, cryptoencrypt, cryptodecrypt, getKeyFromPassword } = pkg;

console.log("Testing new crypter");

let cryptedtext = encrypt((v) => { console.log(v.toString("base64")); return v; }, getKeyFromPassword("password", "testsalt"), 0, cryptoencrypt)("Testing new crypter");
console.log(cryptedtext.toString("base64"));

let decryptedtext = decrypt((v) => { console.log(v.toString("base64")); return v; }, getKeyFromPassword("password", "testsalt"), 0, cryptodecrypt)(cryptedtext);
console.log(decryptedtext.toString());

NOTE:

Errors:

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object occurs because of the crypto cryptoencrypt and cryptodecrypt function.

  • It basically means the encrypted data passed in not of the right format sent.
  • It expects that what was send back from the set function of encrypt data function is passed back to it in the same format
  • The format of the encrypted data looks like this: { iv: 'uIF9gjowtvLDtUYMU0hQjg==', content: 'K129hrHxeBkg' }
  • You can also remove the implementation used and pass your own encrypting function you wish

TODO:

  • Create a browser implementation
  • Test with React useState functions
  • Test with tough cookies package, jsdom in nodejs, etc