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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@vvlad1973/crypto

v2.1.3

Published

A JavaScript class for encrypting and decrypting text using specified cryptographic parameters (algorithm, password, etc.)

Readme

@vvlad1973/crypto

A TypeScript library for encrypting and decrypting text using AES-256-CTR encryption with PBKDF2 key derivation.

This library uses Node.js native crypto module for secure encryption operations.

Features

  • AES-256-CTR encryption
  • PBKDF2 key derivation with configurable parameters
  • Support for both number and Buffer initialization vectors
  • TypeScript support with full type definitions
  • Dual API: constructor with options object or separate parameters
  • UUID v4 generation utility
  • Zero external dependencies (uses native Node.js crypto)

Installation

npm install @vvlad1973/crypto

Usage

Importing

import Crypto, { CryptoOptions, isCrypto } from '@vvlad1973/crypto';

Basic Usage

import Crypto from '@vvlad1973/crypto';

const password = 'your-password';
const salt = 'your-salt';

// Create an instance using separate parameters
const crypto = new Crypto(password, salt);

// Encrypt a text
const plainText = 'Hello, World!';
const encryptedText = crypto.encrypt(plainText);
console.log('Encrypted:', encryptedText);

// Decrypt the text
const decryptedText = crypto.decrypt(encryptedText);
console.log('Decrypted:', decryptedText);

Using Options Object

import Crypto, { CryptoOptions } from '@vvlad1973/crypto';

const options: CryptoOptions = {
  password: 'your-password',
  salt: 'your-salt',
  algorithm: 'SHA512',
  iterations: 1000,
  keyLength: 32,
  iv: 5
};

const crypto = new Crypto(options);
const encrypted = crypto.encrypt('Secret message');
const decrypted = crypto.decrypt(encrypted);

Using Buffer IV

import Crypto from '@vvlad1973/crypto';
import { randomBytes } from 'crypto';

// Generate a random 16-byte initialization vector
const ivBuffer = randomBytes(16);

const crypto = new Crypto({
  password: 'your-password',
  salt: 'your-salt',
  iv: ivBuffer
});

const encrypted = crypto.encrypt('Secret message');

Generating UUIDs

import Crypto from '@vvlad1973/crypto';

// Generate a UUID v4
const uuid = Crypto.getUUID();
console.log(uuid); // e.g., '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'

Type Guard

import Crypto, { isCrypto } from '@vvlad1973/crypto';

const crypto = new Crypto('password', 'salt');

if (isCrypto(crypto)) {
  // TypeScript knows crypto has encrypt/decrypt methods
  const encrypted = crypto.encrypt('text');
}

API

Constructor

Using separate parameters

new Crypto(
  password: string,
  salt: string,
  algorithm?: string,
  iterations?: number,
  keyLength?: number,
  iv?: number | Buffer
)

Using options object

new Crypto(options: CryptoOptions)

CryptoOptions interface:

interface CryptoOptions {
  password: string;      // Password for key derivation
  salt: string;          // Salt for key derivation
  algorithm?: string;    // Hash algorithm (default: 'SHA512')
  iterations?: number;   // PBKDF2 iterations (default: 1000)
  keyLength?: number;    // Key length in bytes (default: 32)
  iv?: number | Buffer;  // Initialization vector (default: random 16 bytes)
}

Parameters:

  • password - The password used for PBKDF2 key derivation
  • salt - The salt used for PBKDF2 key derivation
  • algorithm - Hash algorithm for PBKDF2 (default: 'SHA512')
  • iterations - Number of PBKDF2 iterations (default: 1000)
  • keyLength - Derived key length in bytes (default: 32 for AES-256)
  • iv - Initialization vector: either a number (converted to 16-byte Buffer) or a Buffer directly (default: random 16 bytes)

Instance Methods

encrypt(text: string): string

Encrypts the given text using AES-256-CTR encryption.

  • text - The plain text to encrypt
  • Returns: The encrypted text as a hexadecimal string

decrypt(text: string): string

Decrypts the given encrypted text using AES-256-CTR decryption.

  • text - The encrypted text as a hexadecimal string
  • Returns: The decrypted plain text

Static Methods

Crypto.getUUID(): string

Generates a random UUID v4 string using Node.js native crypto.

  • Returns: A UUID v4 string (e.g., '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d')

Utility Functions

isCrypto(object: any): object is Crypto

Type guard function to check if an object is an instance of Crypto.

  • object - The object to check
  • Returns: true if the object is a Crypto instance, false otherwise

Testing

This library uses Vitest for testing with comprehensive coverage requirements.

Run all tests

npm test

Run tests in watch mode

npm run test:watch

Run tests with UI

npm run test:ui

Generate coverage report

npm run test:coverage

Coverage Requirements

  • Lines: 90%
  • Functions: 85%
  • Branches: 90%
  • Statements: 90%

Current coverage: 100% across all metrics

Building

To build the TypeScript project:

npm run build

This will compile TypeScript files to the dist directory with type definitions.

Documentation

To generate TypeDoc documentation:

npm run doc

Documentation will be generated in the docs directory.

License

This project is licensed under the MIT License with Commercial Use - see the LICENSE file for details.

Author

Vladislav Vnukovskiy [email protected]