{"id":15678301,"url":"https://github.com/mister-hope/bcrypt-ts","last_synced_at":"2025-05-07T04:24:36.582Z","repository":{"id":39886637,"uuid":"488571939","full_name":"Mister-Hope/bcrypt-ts","owner":"Mister-Hope","description":"Optimized bcrypt written in typescript with zero dependencies and 8KB Gzip size","archived":false,"fork":false,"pushed_at":"2025-05-03T01:33:17.000Z","size":1496,"stargazers_count":29,"open_issues_count":3,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-03T02:57:16.170Z","etag":null,"topics":["bcrypt","bcryptjs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mister-Hope.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"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},"funding":{"patreon":"Mister_Hope","open_collective":"vuepress-theme-hope","custom":["https://mister-hope.com/about/donate.html"]}},"created_at":"2022-05-04T12:04:10.000Z","updated_at":"2025-05-02T01:27:49.000Z","dependencies_parsed_at":"2023-11-13T03:29:35.775Z","dependency_job_id":"e338d699-a28f-4b38-ba9c-c93092f854c4","html_url":"https://github.com/Mister-Hope/bcrypt-ts","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mister-Hope%2Fbcrypt-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mister-Hope%2Fbcrypt-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mister-Hope%2Fbcrypt-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mister-Hope%2Fbcrypt-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mister-Hope","download_url":"https://codeload.github.com/Mister-Hope/bcrypt-ts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252293086,"owners_count":21724960,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["bcrypt","bcryptjs"],"created_at":"2024-10-03T16:19:46.433Z","updated_at":"2025-05-07T04:24:36.555Z","avatar_url":"https://github.com/Mister-Hope.png","language":"TypeScript","funding_links":["https://patreon.com/Mister_Hope","https://opencollective.com/vuepress-theme-hope","https://mister-hope.com/about/donate.html"],"categories":[],"sub_categories":[],"readme":"# bcrypt-ts\n\n[![npm](https://img.shields.io/npm/v/bcrypt-ts?color=%23ff00dd)](https://www.npmjs.com/package/bcrypt-ts)\n[![npm downloads](https://img.shields.io/npm/dw/bcrypt-ts)](https://www.npmjs.com/package/bcrypt-ts)\n[![types](https://img.shields.io/npm/types/bcrypt-ts)](https://mister-hope.github.io/bcrypt-ts/)\n\n[![Test](https://github.com/Mister-Hope/bcrypt-ts/actions/workflows/test.yml/badge.svg)](https://github.com/Mister-Hope/bcrypt-ts/actions/workflows/test.yml)\n[![DeepScan grade](https://deepscan.io/api/teams/15982/projects/28024/branches/898932/badge/grade.svg)](https://deepscan.io/dashboard#view=project\u0026tid=15982\u0026pid=28024\u0026bid=898932) [![codecov](https://codecov.io/gh/Mister-Hope/bcrypt-ts/graph/badge.svg?token=oO5gZq2aHe)](https://codecov.io/gh/Mister-Hope/bcrypt-ts)\n\nOptimized bcrypt in TypeScript with zero dependencies. Compatible to the C++ [bcrypt](https://npmjs.org/package/bcrypt) binding on Node.js and also working in the browser.\n\n## Why bcrypt-ts instead of bcrypt.js\n\n- bcrypt-ts is fully written in TypeScript\n- bcrypt-ts provides dual ESM/cjs mode for both Node.js and browser environments\n- Minified output\n- Tree shakable\n\n## Security considerations\n\nBesides incorporating a salt to protect against rainbow table attacks, bcrypt is an adaptive function: over time, the\niteration count can be increased to make it slower, so it remains resistant to brute-force search attacks even with\nincreasing computation power. ([see](http://en.wikipedia.org/wiki/Bcrypt))\n\nWhile bcrypt-ts is compatible with the C++ bcrypt binding, it is built in pure JavaScript and thus slower ([about 30%](https://github.com/dcodeIO/bcrypt.js/wiki/Benchmark)), effectively reducing the number of iterations that can be processed in an equal time span.\n\nThe maximum input length is 72 bytes (note that UTF-8 encoded characters use up to 4 bytes) and the length of generated\nhashes is 60 characters. Note that maximum input length is not implicitly checked by the library for compatibility with\nthe C++ binding on Node.js, but should be checked with `truncates(password)` where necessary.\n\n## Install\n\n### Node.js\n\nInstall the package:\n\n```bash\nnpm install bcrypt-ts\n```\n\n### CDN\n\njsDelivr:\n\n- `https://cdn.jsdelivr.net/npm/bcrypt-ts/dist/browser.mjs` (ESM)\n- `https://cdn.jsdelivr.net/npm/bcrypt-ts/dist/browser.umd.js` (UMD)\n\nunpkg:\n\n- `https://unpkg.com/bcrypt-ts/dist/browser.mjs` (ESM)\n- `https://unpkg.com/n/bcrypt-ts/dist/browser.umd.js` (UMD)\n\n## Usage\n\nOn Node.js, the inbuilt [crypto module](http://nodejs.org/api/crypto.html)'s randomBytes interface is used to obtain secure random numbers.\n\n### Browser\n\nIn the browser, bcrypt.js relies on [Web Crypto API](http://www.w3.org/TR/WebCryptoAPI)'s getRandomValues interface to obtain secure random numbers. If no cryptographically secure source of randomness is available, the package will **throw an error**.\n\n### How to choose between them\n\n- If you are using this package in pure Node.js environment, then you will probably use the node bundle.\n- If you are using bundler like webpack or Vite, then you will probably use the browser bundle.\n- If you meet any issues where an incorrect bundle is used, you can import from `bcrypt-ts/node` and `bcrypt-ts/browser` to manually select the bundle.\n\n### Usage - Sync\n\nTo hash a password:\n\n```ts\nimport { genSaltSync, hashSync } from \"bcrypt-ts\";\n\nconst salt = genSaltSync(10);\nconst result = hashSync(\"B4c0//\", salt);\n// Store hash in your password DB\n```\n\nTo check a password:\n\n```ts\nimport { compareSync } from \"bcrypt-ts\";\n\n// Load hash from your password DB\nconst hash = \"xxx\";\n\ncompareSync(\"B4c0//\", hash); // true\ncompareSync(\"not_bacon\", hash); // false\n```\n\nAuto-gen a salt and hash at the same time:\n\n```ts\nimport { hashSync } from \"bcrypt-ts\";\n\nconst result = hashSync(\"bacon\", 8);\n```\n\n### Usage - Async\n\nTo hash a password:\n\n```ts\nimport { genSalt, hash } from \"bcrypt-ts\";\n\nconst salt = await genSalt(10);\nconst result = await hash(\"B4c0//\", salt);\n// Store hash in your password DB\n```\n\nTo check a password:\n\n```ts\nimport { compare } from \"bcrypt-ts\";\n\n// Load hash from your password DB\nconst hash = \"xxxxxx\";\n\nawait compare(\"B4c0//\", hash); // true\nawait compare(\"not_bacon\", hash); // false\n```\n\nAuto-gen a salt and hash:\n\n```ts\nimport { hash } from \"bcrypt-ts\";\n\nconst result = await hash(\"B4c0//\", 10);\n// Store hash in your password DB\n```\n\n**Note:** Under the hood, asynchronous APIs split an operation into small chunks. After the completion of a chunk, the execution of the next chunk is placed on the back of the [JS event queue](https://developer.mozilla.org/en/docs/Web/JavaScript/EventLoop), efficiently yielding for other computation to execute.\n\n### Usage - Command Line\n\n```\n\nUsage: bcrypt \u003cinput\u003e [rounds|salt]\n\n```\n\n## API\n\n```ts\n/**\n * Synchronously tests a string against a hash.\n *\n * @param content String to compare\n * @param hash Hash to test against\n */\nexport const compareSync: (content: string, hash: string) =\u003e boolean;\n/**\n * Asynchronously compares the given data against the given hash.\n *\n * @param content Data to compare\n * @param hash Data to be compared to\n * @param progressCallback Callback successively called with the percentage of rounds completed\n *  (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.\n */\nexport const compare: (\n  content: string,\n  hash: string,\n  progressCallback?: ((percent: number) =\u003e void) | undefined,\n) =\u003e Promise\u003cboolean\u003e;\n\n/**\n * Synchronously generates a hash for the given string.\n *\n * @param contentString String to hash\n * @param salt Salt length to generate or salt to use, default to 10\n * @returns Resulting hash\n */\nexport const hashSync: (\n  contentString: string,\n  salt?: string | number,\n) =\u003e string;\n/**\n * Asynchronously generates a hash for the given string.\n *\n * @param contentString String to hash\n * @param salt Salt length to generate or salt to use\n * @param progressCallback Callback successively called with the percentage of rounds completed\n *  (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.\n */\nexport const hash: (\n  contentString: string,\n  salt: number | string,\n  progressCallback?: ((progress: number) =\u003e void) | undefined,\n) =\u003e Promise\u003cstring\u003e;\n\n/**\n * Gets the number of rounds used to encrypt the specified hash.\n *\n * @param hash Hash to extract the used number of rounds from\n * @returns Number of rounds used\n * @throws {Error} If `hash` is not a string\n */\nexport const getRounds: (hash: string) =\u003e number;\n/**\n * Gets the salt portion from a hash. Does not validate the hash.\n *\n * @param hash Hash to extract the salt from\n * @returns Extracted salt part\n * @throws {Error} If `hash` is not a string or otherwise invalid\n */\nexport const getSalt: (hash: string) =\u003e string;\n\n/**\n * Synchronously generates a salt.\n *\n * @param rounds Number of rounds to use, defaults to 10 if omitted\n * @returns Resulting salt\n * @throws {Error} If a random fallback is required but not set\n */\nexport const genSaltSync: (rounds?: number) =\u003e string;\n/**\n * Asynchronously generates a salt.\n *\n * @param rounds Number of rounds to use, defaults to 10 if omitted\n */\nexport const genSalt: (rounds?: number) =\u003e Promise\u003cstring\u003e;\n```\n\n## Credits\n\n- Based on [bcrypt.js](https://github.com/dcodeIO/bcrypt.js)\n\n  - Based on work started by Shane Girish at [bcrypt-nodejs](https://github.com/shaneGirish/bcrypt-nodejs)\n    - Based on [javascript-bcrypt](http://code.google.com/p/javascript-bcrypt/) (New BSD-licensed).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmister-hope%2Fbcrypt-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmister-hope%2Fbcrypt-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmister-hope%2Fbcrypt-ts/lists"}