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

crypto-shield

v1.6.2

Published

CryptoShield is a powerful and efficient Node.js library designed for seamless encryption and decryption using a variety of algorithms. Leveraging Node.js's built-in crypto module, this library empowers users with fine-grained control over their encryptio

Downloads

30

Readme

🛡️ crypto-shield

crypto-shield: Secure and Efficient Encryption and Decryption Library for Node.js

crypto-shield is a powerful and efficient Node.js library designed for seamless encryption and decryption using a variety of algorithms. Leveraging Node.js's built-in crypto module, this library empowers users with fine-grained control over their encryption processes, ensuring both security and flexibility.

Table of Contents

Features

  • 🚀 Encrypt and decrypt text effortlessly.
  • 📁 Encrypt and decrypt files securely.
  • 🔐 Supports a diverse set of encryption algorithms for enhanced security.
  • 🛠️ Customizable encryption options, allowing fine-tuning for specific use cases.
  • 🔑 Utilizes PBKDF2 for robust key derivation.
  • 🎯 Streamlined API for seamless integration into Node.js applications.
  • 🌐 Versatile support for both CommonJS (CJS) and ECMAScript Modules (ESM).
  • ⚡ Optimized performance ensures swift cryptographic operations.
  • 🛡️ Robust error handling for a reliable encryption and decryption experience.
  • 🤝 Well-documented and beginner-friendly, fostering ease of use.
  • 🌈 Compatible with a variety of Node.js projects, enhancing flexibility.
  • 🔄 Supports both asynchronous (async/await) and synchronous (Promise) programming paradigms.
  • 📦 Lightweight and dependency-free, keeping your project dependencies in check.
  • 🌐 Cross-platform compatibility for versatile deployment options.
  • 🛑 Thoroughly tested to ensure reliability and stability in diverse scenarios.

crypto-shield provides an extensive feature set to meet your encryption and decryption needs while offering simplicity, security, and flexibility in your Node.js projects.

Installation

Install the package using npm:

npm install crypto-shield

Install the package using yarn:

yarn add crypto-shield

Install the package using bun:

bun add crypto-shield

Usage

Initialization

// CommonJS
const CryptoShield = require("crypto-shield");
const encryptor = new CryptoShield();

// ESM
import CryptoShield from "crypto-shield";
const encryptor = new CryptoShield();

Setting the Secret Key

const secretKey = "my-secret-key";

// Set the key while initialize
const encryptor = new CryptoShield({ secretKey });

// OR

// Add secretKey after initialize it.
const encryptor = new CryptoShield();
encryptor.setSecretKey(secretKey);

Encrypting and Decrypting Text

const plaintext = "Hello, world!";

// Async/Await
(async () => {
  try {
    const encryptedText = await encryptor.encryptText(plaintext);
    console.log("Encrypted Text:", encryptedText); // 3992b5bf64591ebbe93708ffb2dc00e8a481b93d4c2c1b752509525b05a4f24316a9d3e82556e61e79c6c129db31b62cf57a910a3c3b1d0d64ab0dc41a4eaa5ce948442365d2ce0280

    const decryptedText = await encryptor.decryptText(encryptedText);
    console.log("Decrypted Text:", decryptedText); // Hello, world!
  } catch (error) {
    console.error("Error:", error);
  }
})();

// Promises
encryptor
  .encryptText(plaintext)
  .then((encryptedText) => {
    console.log("Encrypted Text:", encryptedText);

    return encryptor.decryptText(encryptedText);
  })
  .then((decryptedText) => {
    console.log("Decrypted Text:", decryptedText);
  })
  .catch((error) => {
    console.error("Error:", error);
  });

File Encryption and Decryption

const inputFile = "input.txt";
const outputFile = "output.txt";
const secretKey = "my-secret-key";

encryptor.setSecretKey(secretKey);

// Async/Await
(async () => {
  try {
    await encryptor.encryptFile(inputFile, outputFile);
    console.log("File encrypted successfully");

    await encryptor.decryptFile(outputFile);
    console.log("File decrypted successfully");
  } catch (error) {
    console.error("Error:", error);
  }
})();

// Promises
encryptor
  .encryptFile(inputFile, outputFile)
  .then(() => console.log("File encrypted successfully"))
  .then(() => encryptor.decryptFile(outputFile))
  .then(() => console.log("File decrypted successfully"))
  .catch((error) => console.error("Error:", error));

API Documentation

Class: CryptoShield

new CryptoShield(options)

Creates an instance of the CryptoShield class.

Parameters

  • options (optional): EncryptDecryptOptions - An object with configuration options.

EncryptDecryptOptions

The EncryptDecryptOptions interface defines the configuration options available when initializing an instance of the CryptoShield class.

Properties

  • algorithm (optional): Specifies the encryption algorithm to be used. Choose from the supported algorithms defined in EncryptionAlgorithm. Default is "aes-256-gcm".

  • iterations (optional): The number of iterations for the key derivation function (PBKDF2). Default is 5000.

  • keyLength (optional): The length of the encryption key. Choose from the common key lengths defined in KeyLength. Default is 32.

  • ivLength (optional): The length of the initialization vector (IV) used in the encryption process. Default is 12.

  • tagLength (optional): The length of the authentication tag used in some encryption algorithms. Choose from AuthTagLength. Default is 16.

  • salt (optional): The length of the salt used in the key derivation process. Default is 32.

  • encoding (optional): The encoding to be used for text representation. Default is "hex".

  • decoding (optional): The encoding to be used for text decoding. Default is "utf8".

  • secretKey (optional): The secret key used for encryption and decryption. If not provided, it can be set later using the setSecretKey method.

  • pbkdf2Algorithm (optional): The hash algorithm used in the key derivation process (PBKDF2). Choose from the supported algorithms defined in Pbkdf2Algorithm. Default is "sha512".

Usage

import CryptoShield, { EncryptDecryptOptions } from "crypto-shield";

// Example options
const options: EncryptDecryptOptions = {
  algorithm: "aes-128-gcm",
  iterations: 10000,
  keyLength: 16,
  ivLength: 16,
  tagLength: 8,
  salt: 64,
  encoding: "base64",
  decoding: "utf-8",
  secretKey: "my-secret-key",
  pbkdf2Algorithm: "sha256",
};

// Initialize CryptoShield with options
const encryptor = new CryptoShield(options);

// Use encryptor instance with the provided options

setSecretKey(secret: string): void

Set the secret key for encryption and decryption.

Parameters

  • secret (string): The secret key.

encryptText(text: string, secret?: string): Promise<string>

Encrypts the given text.

Parameters

  • text (string): The text to encrypt.
  • secret (optional, string): The secret key.

decryptText(encryptedText: string, secret?: string): Promise<string>

Decrypts the given encrypted text.

Parameters
  • encryptedText (string): The text to decrypt.
  • secret (optional, string): The secret key.

encryptFile(inputFilePath: string, outputFilePath?: string, secret?: string): Promise<boolean>

Encrypts a file.

Parameters

  • inputFilePath (string): The path to the input file.
  • outputFilePath (optional, string): The path to the output file. If not provided, the input file will be overwritten.
  • secret (optional, string): The secret key.

decryptFile(inputFilePath: string, outputFilePath?: string, secret?: string): Promise<boolean>

Decrypts a file.

Parameters

  • inputFilePath (string): The path to the input file.
  • outputFilePath (optional, string): The path to the output file. If not provided, the input file will be overwritten.
  • secret (optional, string): The secret key.

License

This project is licensed under the MIT License.