An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          


Crypto Helper

Cryptography Helper

[![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