https://github.com/heliomarpm/cryptoh
A clean and easy-to-use cryptography utility library for Node.js, built on top of the native crypto module.
https://github.com/heliomarpm/cryptoh
crypto crypto-helper cryptography cryptography-algorithms cryptography-library cryptoh helpers helpers-library md5 node-library security sha1 sha256 sha512 utils-library
Last synced: 6 months ago
JSON representation
A clean and easy-to-use cryptography utility library for Node.js, built on top of the native crypto module.
- Host: GitHub
- URL: https://github.com/heliomarpm/cryptoh
- Owner: heliomarpm
- License: mit
- Created: 2025-06-01T19:52:28.000Z (7 months ago)
- Default Branch: develop
- Last Pushed: 2025-07-07T13:46:33.000Z (6 months ago)
- Last Synced: 2025-07-18T12:55:08.544Z (6 months ago)
- Topics: crypto, crypto-helper, cryptography, cryptography-algorithms, cryptography-library, cryptoh, helpers, helpers-library, md5, node-library, security, sha1, sha256, sha512, utils-library
- Language: TypeScript
- Homepage: https://heliomarpm.github.io/cryptoh
- Size: 1.02 MB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: docs/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: docs/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[![DeepScan grade][url-deepscan-badge]][url-deepscan]
[![CodeFactor][url-codefactor-badge]][url-codefactor]
[![Test][url-test-badge]][url-test]
[![Coverage][url-coverage-badge]][url-coverage-report]
[![NPM version][url-npm-badge]][url-npm]
[![Downloads][url-downloads-badge]][url-downloads]
[![PayPal][url-paypal-badge]][url-paypal]
[![Ko-fi][url-kofi-badge]][url-kofi]
[![Liberapay][url-liberapay-badge]][url-liberapay]
[![GitHub Sponsors][url-github-sponsors-badge]][url-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:**
```bash
npm install @heliomarpm/cryptoh
```
### โ๏ธ Example Code
```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
See the [API documentation](https://heliomarpm.github.io/cryptoh) 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`
- Securely compares a plain text value with a given hash. \
`verify(text: string, hash: string, algorithm?: HashAlgorithm): Promise`
### ๐ฒ cryptoh.random
- Generates a cryptographically secure random salt as a hex string. Default length: 16 bytes. \
`generateSalt(length?: number): Promise`
### ๐ 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`
- Verifies the authenticity of a digital signature. \
`verify(data: string, signature: string, publicKey: string, algorithm?: HashAlgorithm): Promise`
## ๐ฆ 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:
- [Code of Conduct](docs/CODE_OF_CONDUCT.md)
- [Contributing Guide](docs/CONTRIBUTING.md)
Thank you to everyone who has already contributed to the project!
###### Made with [contrib.nn](https://contrib.nn.ci).
### โค๏ธ 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][url-paypal-badge]][url-paypal]
[![Ko-fi][url-kofi-badge]][url-kofi]
[![Liberapay][url-liberapay-badge]][url-liberapay]
[![GitHub Sponsors][url-github-sponsors-badge]][url-github-sponsors]
## ๐ License
[MIT ยฉ Heliomar P. Marques](LICENSE) ๐
----
[url-paypal-badge]: https://img.shields.io/badge/donate%20on-paypal-1C1E26?style=for-the-badge&labelColor=1C1E26&color=0475fe
[url-paypal]: https://bit.ly/paypal-sponsor-heliomarpm
[url-kofi-badge]: https://img.shields.io/badge/kofi-1C1E26?style=for-the-badge&labelColor=1C1E26&color=ff5f5f
[url-kofi]: https://ko-fi.com/heliomarpm
[url-liberapay-badge]: https://img.shields.io/badge/liberapay-1C1E26?style=for-the-badge&labelColor=1C1E26&color=f6c915
[url-liberapay]: https://liberapay.com/heliomarpm
[url-github-sponsors-badge]: https://img.shields.io/badge/GitHub%20-Sponsor-1C1E26?style=for-the-badge&labelColor=1C1E26&color=db61a2
[url-github-sponsors]: https://github.com/sponsors/heliomarpm
[url-codeql-badge]: https://github.com/heliomarpm/cryptoh/actions/workflows/codeql.yml/badge.svg
[url-codeql]: https://github.com/heliomarpm/cryptoh/security/code-scanning
[url-test-badge]: https://github.com/heliomarpm/cryptoh/actions/workflows/0.test.yml/badge.svg
[url-test]: https://github.com/heliomarpm/cryptoh/actions/workflows/0.test.yml
[url-coverage-badge2]: https://img.shields.io/badge/coverage-dynamic.svg?label=coverage&color=informational&style=flat&logo=jest&query=$.coverage&url=https://heliomarpm.github.io/cryptoh/coverage-badge.json
[url-coverage-badge]: https://img.shields.io/endpoint?url=https://heliomarpm.github.io/cryptoh/coverage/coverage-badge.json
[url-coverage-report]: https://heliomarpm.github.io/cryptoh/coverage
[url-release-badge]: https://github.com/heliomarpm/cryptoh/actions/workflows/3.release.yml/badge.svg
[url-release]: https://github.com/heliomarpm/cryptoh/actions/workflows/3.release.yml
[url-publish-badge]: https://github.com/heliomarpm/cryptoh/actions/workflows/4.publish-npm.yml/badge.svg
[url-publish]: https://github.com/heliomarpm/cryptoh/actions/workflows/4.publish-npm.yml
[url-npm-badge]: https://img.shields.io/npm/v/@heliomarpm/cryptoh.svg
[url-npm]: https://www.npmjs.com/package/@heliomarpm/cryptoh
[url-downloads-badge]: https://img.shields.io/npm/dm/@heliomarpm/cryptoh.svg
[url-downloads]: http://badge.fury.io/js/@heliomarpm/cryptoh.svg
[url-deepscan-badge]: https://deepscan.io/api/teams/19612/projects/29822/branches/955507/badge/grade.svg
[url-deepscan]: https://deepscan.io/dashboard#view=project&tid=19612&pid=29822&bid=955507
[url-codefactor-badge]: https://www.codefactor.io/repository/github/heliomarpm/cryptoh/badge
[url-codefactor]: https://www.codefactor.io/repository/github/heliomarpm/cryptoh