Skip to content

A clean and easy-to-use cryptography utility library for Node.js, built on top of the native crypto module.

License

Notifications You must be signed in to change notification settings

heliomarpm/cryptoh

Crypto Helper
Cryptography Helper

DeepScan grade CodeFactor Test Coverage

NPM version Downloads

PayPal Ko-fi Liberapay GitHub Sponsors

πŸ“š Summary

A clean and easy-to-use cryptography utility library for Node.js built on top of the native crypto module. It provides modern hashing, secure random generation, RSA key pair management, and digital signature utilities with a clean API.

Requirements

  • Node.js v16+

πŸš€ Features

  • πŸ“Œ Hash text values using SHA-1, SHA-256, SHA-512, and MD5
  • πŸ”’ Compare hashed values securely using timingSafeEqual
  • πŸ”‘ Generate secure RSA 2048-bit key pairs
  • ✍️ Create and verify digital signatures
  • 🎲 Generate cryptographically secure random salts
  • πŸ“ Fully typed with TypeScript

πŸ”§ Usage

Install the library:

npm install @heliomarpm/cryptoh

✏️ Example Code

import cryptoh, { HashAlgorithm } from "cryptoh";

async function main() {
  // πŸ‘€ User registration (secure password storage)
  const password = "My$ecureP@ssword123";

  // Generate a unique salt for the user
  const salt = await cryptoh.random.generateSalt(16);

  // Concatenate password + salt and generate the hash
  const hashedPassword = await cryptoh.hash.generate(password + salt, HashAlgorithm.SHA512);

  console.log("Salt:", salt);
  console.log("Hashed password:", hashedPassword);

  // You would typically save this salt and hashedPassword to your database
  const storedCredentials = { salt, hashedPassword };

  // πŸ‘€ User login (password verification)
  const passwordAttempt = "My$ecureP@ssword123";

  // Recreate the hash with the stored salt and compare it to the stored hash
  const isPasswordValid = await cryptoh.hash.verify(
    passwordAttempt + storedCredentials.salt,
    storedCredentials.hashedPassword,
    HashAlgorithm.SHA512
  );

  console.log("Is password valid?", isPasswordValid); // true if matches

  // πŸ” Digital signature for sensitive payload (e.g., tokens, receipts, or important data)
  const payload = JSON.stringify({
    userId: 789,
    email: "user@example.com",
    timestamp: Date.now()
  });

  // Generate an RSA key pair
  const { publicKey, privateKey } = await cryptoh.keyPair.generate();

  // Sign the payload with the private key
  const signature = await cryptoh.sign.generate(payload, privateKey);

  console.log("Signature (base64):", Buffer.from(signature, "hex").toString("base64"));

  // Verify the signature using the public key
  const isSignatureValid = await cryptoh.sign.verify(payload, signature, publicKey);

  console.log("Is signature valid?", isSignatureValid); // true if signature matches
}

main();

πŸ“š API Reference

See the API documentation for a complete list of available functions and their signatures.

πŸ”’ cryptoh.hash

  • Hashes the given text using the specified algorithm (default: SHA-256).
    generate(text: string, algorithm?: HashAlgorithm): Promise<string>

  • Securely compares a plain text value with a given hash.
    verify(text: string, hash: string, algorithm?: HashAlgorithm): Promise<boolean>

🎲 cryptoh.random

  • Generates a cryptographically secure random salt as a hex string. Default length: 16 bytes.
    generateSalt(length?: number): Promise<string>

πŸ”‘ cryptoh.keyPair

  • Generates a 2048-bit RSA key pair with PEM encoding.
    generate(): Promise<{ publicKey: string, privateKey: string }>

✍️ cryptoh.sign

  • Generates a digital signature for the provided data using the private key.
    generate(data: string, privateKey: string, algorithm?: HashAlgorithm): Promise<string>

  • Verifies the authenticity of a digital signature.
    verify(data: string, signature: string, publicKey: string, algorithm?: HashAlgorithm): Promise<boolean>

πŸ“¦ Project Scripts

  • npm run check β€” runs formatter, linter and import sorting to the requested files
  • npm run format β€” run the formatter on a set of files
  • npm run lint β€” run various checks on a set of files
  • npm run test β€” run unit tests
  • npm run test:c β€” run unit tests with coverage
  • npm run docs:dev β€” run documentation locally
  • npm run commit - run conventional commits check
  • npm run release:test β€” dry run semantic release
  • npm run build β€” build library

πŸ“¦ Dependencies

βœ… Zero runtime dependencies β€” relies solely on Node.js native crypto module.
πŸ”„ All devDependencies are pinned to latest stable versions

🀝 Contributing

We welcome contributions! Please read:

Thank you to everyone who has already contributed to the project!

Made with contrib.nn.

❀️ Support this project

If this project helped you in any way, there are several ways to contribute.
Help us maintain and improve this template:

⭐ Starring the repository
🐞 Reporting bugs
πŸ’‘ Suggest features
🧾 Improving the documentation
πŸ“’ Share with others

πŸ’΅ Supporting via GitHub Sponsors, Ko-fi, Paypal or Liberapay, you decide. πŸ˜‰

PayPal Ko-fi Liberapay GitHub Sponsors

πŸ“ License

MIT Β© Heliomar P. Marques πŸ”