๐ 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 Usage โ
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 fixernpm run format
โ run formatternpm run test
โ run unit testsnpm run test:c
โ run unit tests with coveragenpm run commit
- run conventional commits checknpm run release:test
โ dry run semantic releasenpm 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. ๐
๐ License โ
MIT ยฉ Heliomar P. Marques ๐