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

string-cipher

v1.0.8

Published

Simple string encryption and decryption functions

Downloads

1,810

Readme

String-Cipher

unit test eslint

Simple set of crypto function for encrypting and decrypting UTF-8/ACSII strings. The module uses AES-GCM (128, 192 and 256) bases on Node crypto module. Solution used is based on this gist. By using AES-GCM encryption chiper text is authenticated as well. Base of this module are the make functions that generate desired encrypt and decrypt functions.

Written in Typescript as an ES6 module, all functions are provided in Sync and Async versions. In order to imporve over all security scheme, user supplied Password and random Salt is used to drive a key using pbkdf2 (with default iterations of 1, for speed but this can be changed using options). The key length depends on the AES-GCM version (128/192/256 use 16/24/32 bit keys) other values are defaulted to values specified by RFC 5288

Contents

Installation and Usage

npm install --save string-cipher

Async Api usage

Using async api for string inputs

import { encryptString, decryptString } from 'string-cipher';

const fetchedPassword = 'password'; // fetched from secure location not to placed in code like this 
const plainText = 'test string'; // utf-8 strings
const cipherText = await encryptString(plainText, fetchedPassword);
const retrivedText = await decryptString(cipherText, fetchedPassword);

Using async api for JSON inputs

import { encryptJson, decryptJson } from 'string-cipher';

const fetchedPassword = 'password'; // fetched from secure location not to placed in code like this 
const plainJson = { "field": "value" }; // any object that can be stringifed by JSON.stringify
const cipherJson = await encryptJson(plainJson, fetchedPassword);
const retrivedJson = await decryptJson(cipherJson, fetchedPassword);

Sync Api usage

Using sync api for string inputs

import { encryptStringSync, decryptStringSync } from 'string-cipher';

const fetchedPassword = 'password'; // fetched from secure location not to placed in code like this 
const plainText = 'test string'; // utf-8 strings
const cipherText = encryptStringSync(plainText, fetchedPassword);
const retrivedText = decryptStringSync(cipherText, fetchedPassword);

Using sync api for JSON inputs

import { encryptJsonSync, decryptJsonSync } from 'string-cipher';

const fetchedPassword = 'password'; // fetched from secure location not to placed in code like this 
const plainJson = { "field": "value" }; // any object that can be stringifed by JSON.stringify
const cipherJson = encryptJsonSync(plainJson, fetchedPassword);
const retrivedJson = decryptJsonSync(cipherJson, fetchedPassword);

Customization

With the help of make functions provided by the module you can customize the encryption and decrption setting. Note that same configuration should be appiled to both makeStringEncrypter and makeStringDecrypter

import { makeStringEncrypter, makeStringDecrypter } from 'string-cipher';

const commonOptions = {
  algorithm: 'aes-128-gcm',
  stringEncoding = 'ascii',
  authTagLength = 8,
  ivLength = 8,
  saltLength = 8,
  iterations = 10,
  digest = 'sha256'
}

const customEncrypt = makeStringEncrypter({
  outputEncoding = 'hex',
  ...commonOptions
});
const customEncryptJson = (payload, password) => customEncrypt(JSON.stringify(payload), password);

const customDecrypt = makeStringDecrypter({
  inputEncoding = 'hex',
  ...commonOptions
});
const customDecryptJson = async (payload, password) => JSON.parse(await customDecrypt(payload, password));

const fetchedPassword = 'password'; // fetched from secure location not to placed in code like this 
const plainText = 'test string';
const cipherText = await customEncrypt(plainText, fetchedPassword);
const retrivedText = await customDecrypt(cipherText, fetchedPassword);

makeStringEncrypterSync and makeStringDecrypterSync functions can used to generate Sync versions of cutomized encrypt and decrypt functions.

Make Functions Option

|Option|Type|Required|Values|Default|Notes| |------|----|--------|------|-------|-----| |algorithm|CipherGCMTypes|Yes|aes-256-gcm,aes-128-gcm,aes-192-gcm|none|| |stringEncoding|string|No|utf8,ascii|utf8|encoding format of the input string| |outputEncoding|string|No|base64,hex|base64|only for encryption function output format| |inputEncoding|string|No|base64,hex|base64|only for decryption function input format| |ivLength|number|No|any number|12|Security of encryption depends on this 12 is recomemded|
|authTagLength|number|No|any number|16|Security of encryption depends on this 16 is recomemded| |saltLength|number|No|any number|32|Used for password generation| |iterations|number|No|any number|1|Used by pbkdf2 to drive key, main factor is speed| |digest|string|No|sha25,sha512|sha256|Used by pbkdf2 to drive key|