{"id":26758462,"url":"https://github.com/alessiofrittoli/crypto-key","last_synced_at":"2026-04-05T01:33:41.652Z","repository":{"id":278440991,"uuid":"898674671","full_name":"alessiofrittoli/crypto-key","owner":"alessiofrittoli","description":"Lightweight TypeScript library for Node.js Cryptographic keys","archived":false,"fork":false,"pushed_at":"2025-12-04T17:20:58.000Z","size":631,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-08T01:31:14.583Z","etag":null,"topics":["cipher","cripto-keys","cryptography","hash","hmac","scrypt"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/@alessiofrittoli/crypto-key","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alessiofrittoli.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"license.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":["alessiofrittoli"]}},"created_at":"2024-12-04T20:27:06.000Z","updated_at":"2025-12-04T17:20:56.000Z","dependencies_parsed_at":"2025-12-05T13:05:53.290Z","dependency_job_id":null,"html_url":"https://github.com/alessiofrittoli/crypto-key","commit_stats":null,"previous_names":["alessiofrittoli/crypto-key"],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/alessiofrittoli/crypto-key","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alessiofrittoli%2Fcrypto-key","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alessiofrittoli%2Fcrypto-key/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alessiofrittoli%2Fcrypto-key/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alessiofrittoli%2Fcrypto-key/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alessiofrittoli","download_url":"https://codeload.github.com/alessiofrittoli/crypto-key/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alessiofrittoli%2Fcrypto-key/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31421869,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-05T00:25:07.052Z","status":"ssl_error","status_checked_at":"2026-04-05T00:25:05.923Z","response_time":60,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cipher","cripto-keys","cryptography","hash","hmac","scrypt"],"created_at":"2025-03-28T16:20:12.233Z","updated_at":"2026-04-05T01:33:41.630Z","avatar_url":"https://github.com/alessiofrittoli.png","language":"TypeScript","funding_links":["https://github.com/sponsors/alessiofrittoli"],"categories":[],"sub_categories":[],"readme":"# Crypto Key 🔑\n\n[![NPM Latest Version][version-badge]][npm-url] [![Coverage Status][coverage-badge]][coverage-url] [![Socket Status][socket-badge]][socket-url] [![NPM Monthly Downloads][downloads-badge]][npm-url] [![Dependencies][deps-badge]][deps-url]\n\n[![GitHub Sponsor][sponsor-badge]][sponsor-url]\n\n[version-badge]: https://img.shields.io/npm/v/%40alessiofrittoli%2Fcrypto-key\n[npm-url]: https://npmjs.org/package/%40alessiofrittoli%2Fcrypto-key\n[coverage-badge]: https://coveralls.io/repos/github/alessiofrittoli/crypto-key/badge.svg\n[coverage-url]: https://coveralls.io/github/alessiofrittoli/crypto-key\n[socket-badge]: https://socket.dev/api/badge/npm/package/@alessiofrittoli/crypto-key\n[socket-url]: https://socket.dev/npm/package/@alessiofrittoli/crypto-key/overview\n[downloads-badge]: https://img.shields.io/npm/dm/%40alessiofrittoli%2Fcrypto-key.svg\n[deps-badge]: https://img.shields.io/librariesio/release/npm/%40alessiofrittoli%2Fcrypto-key\n[deps-url]: https://libraries.io/npm/%40alessiofrittoli%2Fcrypto-key\n\n[sponsor-badge]: https://img.shields.io/static/v1?label=Fund%20this%20package\u0026message=%E2%9D%A4\u0026logo=GitHub\u0026color=%23DB61A2\n[sponsor-url]: https://github.com/sponsors/alessiofrittoli\n\n## Lightweight TypeScript library for Node.js Cryptographic keys\n\n### Table of Contents\n\n- [Getting started](#getting-started)\n- [API Reference](#api-reference)\n  - [`Hash` Class](#hash-class)\n    - [Methods](#methods)\n      - [`Hash.digest()`](#hashdigest)\n      - [`Hash.isValid()`](#hashisvalid)\n    - [Example Usage](#example-usage)\n  - [`Hmac` Class](#hmac-class)\n    - [Methods](#methods-1)\n      - [`Hmac.digest()`](#hmacdigest)\n      - [`Hmac.isValid()`](#hmacisvalid)\n    - [Example Usage](#example-usage-1)\n  - [`Scrypt` Class](#scrypt-class)\n    - [Scrypt Type Interfaces](#scrypt-type-interfaces)\n      - [ScryptOptions](#scryptoptions)\n      - [ScryptHashOptions](#scrypthashoptions)\n    - [Methods](#methods-2)\n      - [`Scrypt.hash()`](#scrypthash)\n      - [`Scrypt.isValid()`](#scryptisvalid)\n    - [Example Usage](#example-usage-2)\n- [Development](#development)\n  - [Install depenendencies](#install-depenendencies)\n  - [Build the source code](#build-the-source-code)\n  - [ESLint](#eslint)\n  - [Jest](#jest)\n- [Contributing](#contributing)\n- [Security](#security)\n- [Credits](#made-with-)\n\n---\n\n### Getting started\n\nThe `crypto-key` library it's part of the [`crypto`](https://npmjs.com/search?q=%40alessiofrittoli%2Fcrypto) utility libraries and can be installed by running the following command:\n\n```bash\nnpm i @alessiofrittoli/crypto-key\n```\n\nor using `pnpm`\n\n```bash\npnpm i @alessiofrittoli/crypto-key\n```\n\n---\n\n### API Reference\n\nThis module supports different input data types and it uses the [`coerceToUint8Array`](https://npmjs.com/package/@alessiofrittoli/crypto-buffer#coercetouint8array) utility function from [`@alessiofrittoli/crypto-buffer`](https://npmjs.com/package/@alessiofrittoli/crypto-buffer) to convert it to a `Uint8Array`.\n\n- See [`coerceToUint8Array`](https://npmjs.com/package/@alessiofrittoli/crypto-buffer#coercetouint8array) for more informations about the supported input types.\n\n#### `Hash` Class\n\nThe `Hash` class provides utility methods for hashing strings and validating hashed values using cryptographic algorithms.\\\nIt supports a variety of algorithms based on the OpenSSL version available on the platform.\n\n##### Methods\n\n###### `Hash.digest()`\n\nGenerates a hash of a given string using a specified cryptographic algorithm.\n\n\u003cdetails\u003e\n\u003csummary\u003eParameters\u003c/summary\u003e\n\n| Parameter   | Type                          | Default   | Description                                  |\n|-------------|-------------------------------|-----------|----------------------------------------------|\n| `input`     | `crypto.BinaryLike`           | -         | The data to be hashed.                       |\n| `algorithm` | `Algo.Hash \\| Algo.OtherHash` | 'SHA-256' | (Optional) The cryptographic hash algorithm. |\n\n\u003c/details\u003e\n\n**Returns**\n\nType: `Buffer`\n\nThe resulting hash digest Buffer.\n\n---\n\n###### `Hash.isValid()`\n\nCompares input data with a hashed value to verify if they match.\n\n\u003cdetails\u003e\n\u003csummary\u003eParameters\u003c/summary\u003e\n\n| Parameter   | Type                          | Default   | Description                               |\n|-------------|-------------------------------|-----------|-------------------------------------------|\n| `input`     | `crypto.BinaryLike`           | -         | The raw input data.                       |\n| `digest`    | `CoerceToUint8ArrayInput`     | -         | The hash digest value to compare against. |\n| `algorithm` | `Algo.Hash \\| Algo.OtherHash` | 'SHA-256' | (Optional) The hash algorithm previously used while generating the `hashed` data. |\n\n\u003c/details\u003e\n\n**Returns**\n\nType: `boolean`\n\n`true` if the input matches the hashed `digest` value, `false` otherwise.\n\n---\n\n##### Example Usage\n\n###### Generating a Hash\n\n```ts\nimport { Hash } from '@alessiofrittoli/crypto-key'\n// or\nimport { Hash } from '@alessiofrittoli/crypto-key/Hash'\n\nconsole.log(\n  Hash.digest( 'raw string value' )\n    .toString( 'hex' )\n)\n```\n\n###### Validating a Hash\n\n```ts\nimport { Hash } from '@alessiofrittoli/crypto-key'\n// or\nimport { Hash } from '@alessiofrittoli/crypto-key/Hash'\n\nconst rawInput\t= 'raw string value'\nconst hash\t\t= Hash.digest( rawInput )\nconst isValid\t= Hash.isValid( rawInput, hash )\n\nconsole.log( isValid ) // Outputs: `true`\n```\n\n---\n\n#### `Hmac` Class\n\nThe `Hmac` class provides utility methods for generating and validating HMAC (Hash-based Message Authentication Code) values using cryptographic algorithms.\n\n##### Methods\n\n###### `Hmac.digest()`\n\nGenerates a Hash-based Message Authentication Code (HMAC) for a given message using a secret key.\n\n\u003cdetails\u003e\n\u003csummary\u003eParameters\u003c/summary\u003e\n\n| Parameter   | Type                | Default   | Description                                     |\n|-------------|---------------------|-----------|-------------------------------------------------|\n| `message`   | `crypto.BinaryLike` | -         | The data to be hashed.                          |\n| `secret`    | `crypto.BinaryLike \\| crypto.KeyObject` | - | The secret key to use for generating the HMAC. |\n| `algorithm` | `Algo.Hash`         | 'SHA-256' | (Optional) The cryptographic hash algorithm.    |\n| `encoding`  | `BufferEncoding`    | -         | (Optional) The encoding for the output string.  |\n| `options`   | `stream.TransformOptions` | -   | (Optional) Additional stream transform options. |\n\n\u003c/details\u003e\n\n**Returns**\n\nType: `HmacReturnType\u003cT\u003e`\n\nThe resulting HMAC value. If `encoding` is specified, the output is a string in the specified encoding; otherwise, a `Buffer`.\n\n---\n\n###### `Hmac.isValid()`\n\nValidates a given HMAC value against a message and secret key.\n\n\u003cdetails\u003e\n\u003csummary\u003eParameters\u003c/summary\u003e\n\n| Parameter   | Type                | Default   | Description                                     |\n|-------------|---------------------|-----------|-------------------------------------------------|\n| `digest`    | `CoerceToUint8ArrayInput` | -   | The HMAC digest to validate.                    |\n| `message`   | `crypto.BinaryLike` | -         | The original message used to generate the HMAC. |\n| `secret`    | `crypto.BinaryLike \\| crypto.KeyObject` | - | The secret key used for generating the HMAC. |\n| `algorithm` | `Algo.Hash`         | 'SHA-256' | (Optional) The hash algorithm used for generating the HMAC. |\n| `encoding`  | `BufferEncoding`    | -         | (Optional) The encoding used to generate the hash output. |\n| `options`   | `stream.TransformOptions` | -   | (Optional) Additional stream transform options used for generating the HMAC. |\n\n\u003c/details\u003e\n\n**Returns**\n\nType: `boolean`\n\n`true` if the provided HMAC matches the generated one. `false` otherwise.\n\n---\n\n##### Example Usage\n\n###### Generating an HMAC\n\n```ts\nimport { Hmac } from '@alessiofrittoli/crypto-key'\n// or\nimport { Hmac } from '@alessiofrittoli/crypto-key/Hmac'\n\nconsole.log(\n  Hmac.digest( 'raw string value', 'mysecretkey', 'SHA-256', 'hex' )\n) // Outputs the HMAC value in HEX format.\n```\n\n###### Validating an HMAC\n\n```ts\nimport { Hmac } from '@alessiofrittoli/crypto-key'\n// or\nimport { Hmac } from '@alessiofrittoli/crypto-key/Hmac'\n\nconst message\t= 'raw string value'\nconst secret\t= 'mysecretkey'\nconst hmac\t\t= Hmac.digest( message, secret )\n\nconsole.log(\n  Hmac.isValid( hmac, message, secret )\n) // Outputs: `true`\n```\n\n---\n\n#### `Scrypt` Class\n\nThe `Scrypt` class provides methods for hashing and securely comparing keys using the `scrypt` key derivation algorithm.\\\nIt supports customizable options for computational cost, memory usage, and parallelization to balance security and performance.\n\n##### Scrypt Type Interfaces\n\n###### `ScryptOptions`\n\nDefines customization options for the `scrypt` algorithm.\n\n\u003cdetails\u003e\n\u003csummary\u003eProperties\u003c/summary\u003e\n\n| Property          | Type     | Default   | Description |\n|-------------------|----------|-----------|-------------|\n| `cost`            | `number` | `16384`   | Computational cost factor. Must be a power of 2. Higher values increase security but are slower. |\n| `blockSize`       | `number` | `8`       | Memory cost factor. Higher values increase memory usage, improving security against GPU attacks. |\n| `parallelization` | `number` | `1`       | Parallelization factor. Determines how many threads can be used. |\n| `maxmem`          | `number` | `32 * 1024 * 1024` (32MB) | Maximum memory (in bytes) to be used during key derivation. |\n| `N`               | `number` | `16384`   | Alias for `cost`. |\n| `r`               | `number` | `8`       | Alias for `blockSize`. |\n| `p`               | `number` | `1`       | Alias for `parallelization`. |\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eSuggested options\u003c/summary\u003e\n\n| Scope                       | `cost` (N) | `blockSize` (r) | `parallelization` (p) | `maxmem` |\n|-----------------------------|------------|-----------------|-----------------------|----------|\n| Standard security (default) | `16384`    | `8`             | `1`                   | 32 MB    |\n| High security               | `65536`    | `16`            | `1`                   | 64 MB    |\n| Limited resources           | `8192`     | `4`             | `1`                   | 16 MB    |\n\n\u003c/details\u003e\n\n---\n\n###### `ScryptHashOptions`\n\nDefines options for the `hash` and `isValid` methods.\n\n\u003cdetails\u003e\n\u003csummary\u003eProperties\u003c/summary\u003e\n\n| Property     | Type            | Default   | Description |\n|--------------|-----------------|-----------|-------------|\n| `length`     | `number`        | `64`      | The hash length. Must be between `16` and `256`. |\n| `saltLength` | `number`        | `32`      | The salt length. Must be between `16` and `64`. |\n| `options`    | `ScryptOptions` | See above | Custom options for the `scrypt` algorithm. |\n\n\u003c/details\u003e\n\n---\n\n##### Methods\n\n###### `Scrypt.hash()`\n\nGenerates a hash for a given key using the `scrypt` key derivation algorithm.\n\n\u003cdetails\u003e\n\u003csummary\u003eParameters\u003c/summary\u003e\n\n| Parameter | Type                | Default | Description |\n|-----------|---------------------|---------|-------------|\n| `key`     | `crypto.BinaryLike` | -       | The key to hash. |\n| `options` | `ScryptHashOptions` | -       | (Optional) Configuration options for hashing. |\n\n\u003c/details\u003e\n\n**Returns**\n\nType: `Buffer`\n\nA Buffer containing the salt (first saltLength bytes) followed by the derived hash.\n\n---\n\n###### `Scrypt.isValid()`\n\nValidates the given `key` with the given `hash`.\n\n\u003cdetails\u003e\n\u003csummary\u003eParameters\u003c/summary\u003e\n\n| Parameter | Type                | Default | Description |\n|-----------|---------------------|---------|-------------|\n| `key`     | `crypto.BinaryLike` | -       | The key to validate. |\n| `hash`    | `CoerceToUint8ArrayInput` | - | The hash to compare against. |\n| `options` | `ScryptHashOptions` | -       | (Optional) Configuration options. Must match those used for hashing. |\n\n\u003c/details\u003e\n\n**Returns**\n\nType: `boolean`\n\n`true` if the key is valid, `false` otherwise.\n\n---\n\n##### Example Usage\n\n###### Hashing a Key\n\n```ts\nimport { Scrypt } from '@alessiofrittoli/crypto-key'\n// or\nimport { Scrypt } from '@alessiofrittoli/crypto-key/Scrypt'\n\nconsole.log(\n  Scrypt.hash( 'user-provided-password' )\n    .toString( 'hex' )\n) // Outputs the hash in HEX format.\n```\n\n###### Validating a Key\n\n```ts\nimport { Scrypt } from '@alessiofrittoli/crypto-key'\n// or\nimport { Scrypt } from '@alessiofrittoli/crypto-key/Scrypt'\n\nimport { Scrypt } from './Scrypt';\n\nconst password\t= 'user-provided-password';\nconst hash\t\t= Scrypt.hash( password, { length: 32, saltLength: 16 } )\n\nconsole.log(\n  Scrypt.isValid( password, hash, { length: 32, saltLength: 16 } )\n) // Outputs: true\n```\n\n---\n\n### Development\n\n#### Install depenendencies\n\n```bash\nnpm install\n```\n\nor using `pnpm`\n\n```bash\npnpm i\n```\n\n#### Build the source code\n\nRun the following command to test and build code for distribution.\n\n```bash\npnpm build\n```\n\n#### [ESLint](https://www.npmjs.com/package/eslint)\n\nwarnings / errors check.\n\n```bash\npnpm lint\n```\n\n#### [Jest](https://npmjs.com/package/jest)\n\nRun all the defined test suites by running the following:\n\n```bash\n# Run tests and watch file changes.\npnpm test:watch\n\n# Run tests in a CI environment.\npnpm test:ci\n```\n\n- See [`package.json`](./package.json) file scripts for more info.\n\nRun tests with coverage.\n\nAn HTTP server is then started to serve coverage files from `./coverage` folder.\n\n⚠️ You may see a blank page the first time you run this command. Simply refresh the browser to see the updates.\n\n```bash\ntest:coverage:serve\n```\n\n---\n\n### Contributing\n\nContributions are truly welcome!\n\nPlease refer to the [Contributing Doc](./CONTRIBUTING.md) for more information on how to start contributing to this project.\n\nHelp keep this project up to date with [GitHub Sponsor][sponsor-url].\n\n[![GitHub Sponsor][sponsor-badge]][sponsor-url]\n\n---\n\n### Security\n\nIf you believe you have found a security vulnerability, we encourage you to **_responsibly disclose this and NOT open a public issue_**. We will investigate all legitimate reports. Email `security@alessiofrittoli.it` to disclose any security vulnerabilities.\n\n### Made with ☕\n\n\u003ctable style='display:flex;gap:20px;'\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\n        \u003cimg alt=\"avatar\" src='https://avatars.githubusercontent.com/u/35973186' style='width:60px;border-radius:50%;object-fit:contain;'\u003e\n      \u003c/td\u003e\n      \u003ctd\u003e\n        \u003ctable style='display:flex;gap:2px;flex-direction:column;'\u003e\n          \u003ctbody\u003e\n              \u003ctr\u003e\n                \u003ctd\u003e\n                  \u003ca href='https://github.com/alessiofrittoli' target='_blank' rel='noopener'\u003eAlessio Frittoli\u003c/a\u003e\n                \u003c/td\u003e\n              \u003c/tr\u003e\n              \u003ctr\u003e\n                \u003ctd\u003e\n                  \u003csmall\u003e\n                    \u003ca href='https://alessiofrittoli.it' target='_blank' rel='noopener'\u003ehttps://alessiofrittoli.it\u003c/a\u003e |\n                    \u003ca href='mailto:info@alessiofrittoli.it' target='_blank' rel='noopener'\u003einfo@alessiofrittoli.it\u003c/a\u003e\n                  \u003c/small\u003e\n                \u003c/td\u003e\n              \u003c/tr\u003e\n          \u003c/tbody\u003e\n        \u003c/table\u003e\n      \u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falessiofrittoli%2Fcrypto-key","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falessiofrittoli%2Fcrypto-key","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falessiofrittoli%2Fcrypto-key/lists"}