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

crypt-vault

v0.1.2

Published

A Crypt Vault library for encryption and decryption of data using AES-256-CBC algorithm.

Downloads

29

Readme

Encryption Package

A TypeScript encryption package designed for both Node.js and browser environments, providing AES-256-CBC encryption, compression, and utilities like UUID generation and password hashing.

Installation

Please contact your admin to grant you the read access to this repo, then please run cmd:

For Yarn

yarn add crypt-vault

For npm

npm install crypt-vault

Setup

To initialize the encryption module, call initializeEncryption with a secret key and optional initialization vector (IV):

import { initializeEncryption, encrypt, decrypt } from 'crypt-vault';

// Initialize Encryption with secretKey and IV (optional)
initializeEncryption('your-secret-key', 'optional-iv');

Functions Overview

1. initializeEncryption(secretKey: string, iv?: string): void

Initializes the encryption class with a 32-byte secretKey and an optional 16-byte iv.

  • Parameters:
    • secretKey (string): A 32-byte string used for encryption.
    • iv (string, optional): A 16-byte initialization vector. If not provided, a random IV is generated.

2. encrypt(data: any): string

Encrypts the input data using AES-256-CBC after compressing it.

  • Parameters:
    • data (any): The data to be encrypted. It can be an object, array, or string.
  • Returns:
    • string: The base64 encoded encrypted string.

3. decrypt(data: string): any

Decrypts the input string (which must be in the format encrypted by encrypt) and decompresses it.

  • Parameters:
    • data (string): The base64 encoded string to be decrypted.
  • Returns:
    • any: The decrypted and decompressed data, parsed back to its original form (object, array, or string).

4. dynamicEncrypt(data: any, ENCRYPTION_KEY: string): string | undefined

Encrypts the given data using a custom encryption key, instead of the one initialized globally.

  • Parameters:

    • data (any): The data to be encrypted.
    • ENCRYPTION_KEY (string): A 32-byte string used as the encryption key.
  • Returns:

    • string | undefined: The base64 encoded encrypted string, or undefined if encryption fails.

5. dynamicDecrypt(data: string, ENCRYPTION_KEY: string): any

Decrypts the input string using a custom encryption key.

  • Parameters:

    • data (string): The base64 encoded string to be decrypted.
    • ENCRYPTION_KEY (string): A 32-byte string used as the decryption key.
  • Returns:

    • any: The decrypted and decompressed data, parsed back to its original form.

6. compressData(str: string): string

Compresses the input string using Gzip and converts it to a base64 string.

  • Parameters:
    • str (string): The string to be compressed.
  • Returns:
    • string: The base64 encoded compressed string.

7. decompressData(str: string): string

Decompresses a base64 encoded compressed string (originally compressed by compressData).

  • Parameters:
    • str (string): The base64 encoded compressed string.
  • Returns:
    • string: The decompressed string.

8. uuidv4(): string

Generates a version 4 UUID.

  • Returns:
    • string: A UUID v4 string (e.g., xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx).

9. getHash(data: string): string

Generates a SHA-256 hash of the input string and encodes it in base64.

  • Parameters:
    • data (string): The string to be hashed.
  • Returns:
    • string: The base64 encoded hash of the input.

10. hashPassword(password: string): string

Hashes the password with a randomly generated salt using SHA-256.

  • Parameters:
    • password (string): The password to be hashed.
  • Returns:
    • string: A hashed password with the salt, in the format salt$hash.

11. comparePassword(password: string, hashedPassword: string): boolean

Compares a plaintext password with a hashed password.

  • Parameters:
    • password (string): The plaintext password.
    • hashedPassword (string): The hashed password, in the format salt$hash.
  • Returns:
    • boolean: true if the password matches the hash, otherwise false.

Example Usage

Encrypting and Decrypting Data:

initializeEncryption('mysecretkey1234567890123456789012');

// Encrypt data
const encryptedData = encrypt({ message: 'Hello, World!' });
console.log('Encrypted Data:', encryptedData);

// Decrypt data
const decryptedData = decrypt(encryptedData);
console.log('Decrypted Data:', decryptedData);

Dynamic Encryption and Decryption:

const customKey = 'mycustomkey1234567890123456789012';
const encryptedData = dynamicEncrypt({ message: 'Hello, Custom!' }, customKey);
console.log('Dynamically Encrypted Data:', encryptedData);

const decryptedData = dynamicDecrypt(encryptedData!, customKey);
console.log('Dynamically Decrypted Data:', decryptedData);

Password Hashing and Comparison:

const hashedPassword = hashPassword('mySecretPassword');
console.log('Hashed Password:', hashedPassword);

const isMatch = comparePassword('mySecretPassword', hashedPassword);
console.log('Password Match:', isMatch);

Generating UUID and SHA256 Hash:

console.log('Generated UUID:', uuidv4());

const hash = getHash('Hello, Hash!');
console.log('Generated Hash:', hash);

License

MIT © Sarvadhi Solution