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

rahayu-nd

v1.0.0

Published

A cryptographic library for AES encryption/decryption and RSA key management.

Downloads

1

Readme

rajendra-hayuningrat

penulis bercerita, sesuatu itu akan mencapai kesempurnaan awal mulanya harus di pisahkan terlebih-dahulu untuk belajar memperbaiki diri masing-masing. Namun itu hanya sementara. setelah yang di pisahkan itu mengupgrade dirinya sendiri suatu saat akan di satukan kembali kalau sudah selaras. -Sastra Jendra Hayuningrat Pangruwating Diyu

Modul ini menyediakan metode enkripsi dan dekripsi menggunakan algoritma AES-GCM untuk enkripsi data dan RSA-OAEP untuk enkripsi kunci AES, menggunakan Node.js crypto library.

Background Concept and Flow

Background Concept: The Rahayu class is designed to facilitate cryptographic operations using AES encryption/decryption and RSA key management in Node.js applications. It provides functionalities to securely encrypt and decrypt data, manage RSA keys from files or URLs, and handle errors and logging effectively.

Flow of Operations:

  1. Initialization (constructor):

    • Accepts paths or URLs to the public and private RSA keys, along with optional parameters (options) such as AES key length, AES IV length, RSA key bits, logger instance, and encryption algorithm.
    • Initializes default values if optional parameters are not provided.
  2. Key Retrieval and Validation:

    • validatePathOrUrl(pathOrUrl): Validates if a given path or URL is accessible. Uses Axios to check if a URL is reachable or uses fs.access() for local file paths. Throws InvalidPathError if validation fails.
    • getKey(pathOrUrl): Retrieves a key asynchronously from either a local file path or a URL. Uses fs.readFile() for file paths and axios.get() for URLs. Handles errors if fetching fails.
  3. Encryption and Decryption:

    • encryptAESKeyWithRSA(aesKey): Encrypts an AES key using RSA-OAEP encryption. Retrieves the public RSA key, performs encryption, and returns the base64-encoded encrypted AES key.
    • decryptAESKeyWithRSA(encryptedAESKey): Decrypts a base64-encoded AES key using RSA-OAEP decryption. Retrieves the private RSA key, decrypts the AES key, and returns it as a Buffer.
  4. Logging:

    • createDefaultLogger(): Static method that creates a default Winston logger instance configured to log to console and a file (app.log). Used for logging errors and information during key fetching, encryption, and decryption operations.
  5. Error Handling:

    • Custom error classes (EncryptionError, DecryptionError, InvalidPathError) are defined to handle specific types of errors that may occur during cryptographic operations, key validation, or key retrieval.
  6. External Dependencies:

    • Relies on Node.js crypto module for AES encryption/decryption and RSA key management.
    • Uses axios for HTTP requests to fetch keys from URLs.
    • Utilizes fs module for file system operations like reading files.

Documentation

Rahayu Class

Constructor
/**
 * @class
 * Class for handling cryptographic operations including AES encryption/decryption and RSA key management.
 * @constructor
 * @param {string} publicKeyPathOrUrl - Path or URL to the public RSA key.
 * @param {string} privateKeyPathOrUrl - Path or URL to the private RSA key.
 * @param {Object} [options] - Additional options.
 * @param {number} [options.aesKeyLength] - Length of AES key in bytes.
 * @param {number} [options.aesIvLength] - Length of AES IV in bytes.
 * @param {number} [options.rsaKeyBits] - Number of bits for RSA key.
 * @param {Object} [options.logger] - Winston logger instance.
 * @param {string} [options.encryptionAlgorithm] - Encryption algorithm (default: AES-256-GCM).
 */
constructor(publicKeyPathOrUrl, privateKeyPathOrUrl, options = {}) { ... }
Methods
  • validatePathOrUrl(pathOrUrl)

    /**
     * Validates if the given path or URL is accessible.
     * @param {string} pathOrUrl - File path or URL to validate.
     * @returns {Promise<void>} - Resolves if path or URL is valid.
     * @throws {InvalidPathError} - If path or URL is invalid.
     */
    async validatePathOrUrl(pathOrUrl) { ... }
  • getKey(pathOrUrl)

    /**
     * Retrieves a key from file path or URL.
     * @param {string} pathOrUrl - File path or URL to retrieve the key from.
     * @returns {Promise<string>} - Resolves with the retrieved key.
     * @throws {Error} - If failed to retrieve the key.
     */
    async getKey(pathOrUrl) { ... }
  • encryptAESKeyWithRSA(aesKey)

    /**
     * Encrypts the AES key using RSA-OAEP.
     * @param {Buffer} aesKey - AES key to encrypt.
     * @returns {Promise<string>} - Resolves with the base64-encoded encrypted AES key.
     * @throws {EncryptionError} - If encryption fails.
     */
    async encryptAESKeyWithRSA(aesKey) { ... }
  • decryptAESKeyWithRSA(encryptedAESKey)

    /**
     * Decrypts the AES key using RSA-OAEP.
     * @param {string} encryptedAESKey - Base64-encoded encrypted AES key.
     * @returns {Promise<Buffer>} - Resolves with the decrypted AES key.
     * @throws {DecryptionError} - If decryption fails.
     */
    async decryptAESKeyWithRSA(encryptedAESKey) { ... }
  • fetchKey(url)

    /**
     * Fetches a key from a given URL.
     * @param {string} url - URL to fetch the key from.
     * @returns {Promise<string>} - Resolves with the fetched key.
     * @throws {Error} - If fetching the key fails.
     */
    async fetchKey(url) { ... }
  • createDefaultLogger()

    /**
     * Creates a default Winston logger instance.
     * @returns {Object} - Winston logger instance.
     */
    static createDefaultLogger() { ... }
Constants
  • DEFAULT_AES_KEY_LENGTH, DEFAULT_AES_IV_LENGTH, DEFAULT_RSA_KEY_BITS

    Constants defining default values for AES key length, AES IV length, and RSA key bits respectively.

  • AES_ALGORITHM

    Constant defining the default AES encryption algorithm (aes-256-gcm).

  • UTF8_ENCODING

    Constant defining UTF-8 encoding, used for encoding/decoding strings.

Error Classes
  • EncryptionError

    Custom error class for encryption failures.

  • DecryptionError

    Custom error class for decryption failures.

  • InvalidPathError

    Custom error class for invalid file paths or URLs.

Example Usage

// Example usage of Rahayu class
import Rahayu from './Rahayu';

async function exampleUsage() {
  const publicKeyPath = './keys/public.pem';
  const privateKeyPath = './keys/private.pem';

  const rahayuInstance = new Rahayu(publicKeyPath, privateKeyPath);

  try {
    // Encrypt AES key
    const aesKey = Buffer.from('supersecretkey', Rahayu.UTF8_ENCODING);
    const encryptedAESKey = await rahayuInstance.encryptAESKeyWithRSA(aesKey);
    console.log('Encrypted AES Key:', encryptedAESKey);

    // Decrypt AES key
    const decryptedAESKey = await rahayuInstance.decryptAESKeyWithRSA(encryptedAESKey);
    console.log('Decrypted AES Key:', decryptedAESKey.toString(Rahayu.UTF8_ENCODING));
  } catch (error) {
    console.error('Error:', error);
  }
}

exampleUsage();

Summary

The Rahayu class encapsulates robust cryptographic functionalities in Node.js, providing secure AES encryption/decryption and RSA key management. It emphasizes error handling, logging, and flexibility through customizable options. By using this class, developers can securely manage keys, encrypt sensitive data, and decrypt encrypted data in their applications.