string-crypto
TypeScript icon, indicating that this package has built-in type declarations

3.0.2 • Public • Published

String Crypto logo depicting a ball of yarn being weaved by a vintage looking key

String Crypto

Tests Try string-crypto on RunKit NPM Downloads

All Contributors

Small, simple, and secure library to encrypt and decrypt strings using PBKDF2 for key derivation and AES (defaulted to 256-bit / SHA512).

This project is open to updates by its users, I ensure that PRs are relevant to the community. In other words, if you find a bug or want a new feature, please help us by becoming one of the contributors ✌️ ! See the contributing section.

Breaking Changes in v3

  • Digest Option Removed: The ability to specify a digest method for key derivation has been removed due to the underlying library (crypto-js) not supporting this functionality directly. We switched to using crypto-js since Node's crypto is commonly breaking due to a missmatch with openssl versions.
  • Key Size Specification: Key size can now be specified in bits as an option when encrypting or decrypting, providing users with the flexibility to define the strength of encryption according to their needs.
  • Default Salt Generation: The default salt is now generated dynamically for each encryption operation if not specified by the user, enhancing security by preventing the use of static salts.

Features

  • Encryption and decryption of strings using PBKDF2 (for key derivation) and AES (Advanced Encryption Standard) in CBC mode.
  • Dynamic generation of a secure random salt for each encryption operation when not specified.
  • Customizable number of iterations for key derivation, enhancing the security against brute force attacks.
  • Key size can be specified in bits, allowing for AES-128, AES-192, and AES-256 encryption strengths.
  • Simplified CLI and library interfaces for straightforward usage in encrypting and decrypting strings.
  • Secure random initialization vector (IV) generation for each encryption operation, ensuring ciphertext uniqueness.

Like this module? ❤

Please consider:

Usage CLI

Encrypting a String

To encrypt a string, use the encrypt command with the required options:

npx string-crypto encrypt --string "Your String Here" --password "YourPassword"

Optional parameters:

  • --salt: Custom salt for encryption. If omitted, a random salt will be used.
  • --iterations: Number of iterations (default is 1000).
  • --keySize: Key size in bits (default is 256).

Decrypting a String

To decrypt a string, use the decrypt command with the required options:

npx string-crypto decrypt --string "EncryptedStringHere" --password "YourPassword"

Optional parameters are the same as for encryption, except --digest is no longer available.

Help

For more information or to see all available options, you can use the -h or --help flag:

npx string-crypto --help

Usage Library

npm i string-crypto
pnpm i string-crypto
yarn add string-crypto
import StringCrypto from 'string-crypto';

const stringToProtect = 'What is the largest (rational) number n such that there are positive integers p, q, r such that 1 - 1/p - 1/q - 1/r = 1/n?';

const password = 'Oh-no,not-again';

const sc = new StringCrypto();

let encryptedString = sc.encryptString(stringToProtect, password);

console.log('Encrypted String:', encryptedString);

console.log('Decrypted String:', sc.decryptString(encryptedString, password));

Options

const options = {
  salt: '2f0ijf2039j23r09j2fg45o9ng98um4o',
  iterations: 1000,
  keySize: 256, // Specify key size in bits
};

const sc = new StringCrypto(options);

Development and build scripts

I chose Rollup to handle the transpiling, compression, and any other transformations needed to get your Typescript code running as quickly and performant as possible.

This repo uses runkit.js to validate code sanity. Why? Because www.npmjs.com uses Runkit to allow potential users to play with your module, live on their browser, which is one of the best ways to convince someone to use your modules in their code. Runkit will look for the runkit.js by default and display that as the initial playground for the user, so by making it the default validation method during development, this encourages proper communication with the users of your code.

Development

yarn dev

Uses concurrently to run Rollup in watch mode (which means it will transpile to dist when you save changes to your code), as well as Nodemon to listen for changes in the dist directory and re-run the runkit.js as you modify your source! This includes running node with the --inspect flag so you can inspect your code using Google Chrome Dev Tools (by opening chrome://inspect in your browser), you're welcome ;)

Build

yarn build

This command will build the dist/index.js, uglified and tree-shaken so it loads/runs faster.

It also generates a source map and a dist/index.d.ts type file for Typescript importing convenience.

Contributing

Yes, thank you! This plugin is community-driven, most of its features are from different authors. Please update the docs and tests and add your name to the package.json file.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Jean Lescure

🚧 💻 📓 ⚠️ 💡 📖

Diana Lescure

📖 👀 🎨

Package Sidebar

Install

npm i string-crypto

Weekly Downloads

1,569

Version

3.0.2

License

Apache-2.0

Unpacked Size

186 kB

Total Files

13

Last publish

Collaborators

  • jeanlescure