Skip to content


Cryptography Helper

๐Ÿ“š 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:

bash
npm install @heliomarpm/cryptoh

โœ๏ธ Example Usage โ€‹

typescript
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 โ€‹

๐Ÿ”’ 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 lint โ€” run linter and fixer
  • npm run format โ€” run formatter
  • npm run test โ€” run unit tests
  • npm run test:c โ€” run unit tests with coverage
  • 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. ๐Ÿ˜‰

PayPalKo-fiLiberapayGitHub Sponsors

๐Ÿ“ License โ€‹

MIT ยฉ Heliomar P. Marques ๐Ÿ”