{"id":28219742,"url":"https://github.com/no2chem/bigint-hash","last_synced_at":"2025-06-11T09:31:15.866Z","repository":{"id":57159622,"uuid":"149641087","full_name":"no2chem/bigint-hash","owner":"no2chem","description":"💪🔢🔒 bigint-hash: Hashing for TC39 BigInt Proposal ","archived":false,"fork":false,"pushed_at":"2019-03-18T21:51:44.000Z","size":304,"stargazers_count":12,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-18T03:12:06.999Z","etag":null,"topics":["bigint","bindings","crypto","ethereum","hash","keccak","n-api","node","nodejs","sha","sha-1","sha-2","sha-3","xxhash"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/no2chem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-20T16:46:55.000Z","updated_at":"2023-12-16T10:43:03.000Z","dependencies_parsed_at":"2022-09-07T20:12:11.356Z","dependency_job_id":null,"html_url":"https://github.com/no2chem/bigint-hash","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/no2chem%2Fbigint-hash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/no2chem%2Fbigint-hash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/no2chem%2Fbigint-hash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/no2chem%2Fbigint-hash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/no2chem","download_url":"https://codeload.github.com/no2chem/bigint-hash/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/no2chem%2Fbigint-hash/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259238776,"owners_count":22826795,"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":["bigint","bindings","crypto","ethereum","hash","keccak","n-api","node","nodejs","sha","sha-1","sha-2","sha-3","xxhash"],"created_at":"2025-05-18T03:11:24.053Z","updated_at":"2025-06-11T09:31:15.857Z","avatar_url":"https://github.com/no2chem.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 💪🔢🔒 bigint-hash: Hashing for TC39 BigInt Proposal \n[![NPM Package](https://img.shields.io/npm/v/bigint-hash.svg?style=flat-square)](https://www.npmjs.org/package/bigint-hash)\n[![Build Status](https://img.shields.io/travis/com/no2chem/bigint-hash.svg?branch=master\u0026style=flat-square)](https://travis-ci.com/no2chem/bigint-hash)\n[![Coverage Status](https://img.shields.io/coveralls/no2chem/bigint-hash.svg?style=flat-square)](https://coveralls.io/r/no2chem/bigint-hash)\n![node](https://img.shields.io/node/v/bigint-hash.svg?style=flat-square)\n\n[bigint-hash](https://www.npmjs.org/package/bigint-hash) provides common hashing routines (MD5, SHA, SHA-2, SHA-3, Keccak, xxHash) that use N-API routines to return [TC39 Proposed BigInts](https://github.com/tc39/proposal-bigint) as digests instead of Buffers, strings or\nUint8Arrays. BigInts are especially useful in the context of hashes because they can be compared 10-100x faster than\nbuffers (see [here](https://github.com/no2chem/bigint-buffer#why-use-bigints)). In addition, bigints are\nmanaged much more efficiently than Buffers, which are allocated outside the V8 heap. bigint-hash also provides routines\nwhich are optimized for the common case of hashing a single buffer. As a result, this library is about\n5x faster than Node.js's built in [crypto API](https://nodejs.org/api/crypto.html), even though the same underlying \nOpenSSL library is used. In the browser, bigint-hash falls back to polyfills provided for crypto libraries. \nbigint-hash aims for:\n\n- __Drop-in compatibility with Node.js's crypto API.__  While we don't support stream functionality yet, in most cases you\ncan replace references to Node.js's API with bigint-hash.\n\n- __High performance.__ bigint-hash aims to be the fastest way to obtain a hash in Node.js, using N-API bindings to\noptimized libraries whenever possible. This results in up to a 7x speedup over existing libraries, even when\nnot using bigint. With xxHash, we can hash at ~2.5M hashes/second. See benchmarks [here](https://github.com/no2chem/bigint-hash#performance).\n\n- __Support for a wide variety of hash algorithms.__ bigint-hash currently supports MD5, the SHA-1, SHA-2 family of\nhashing algorithms (with OpenSSL) and SHA-3 and Keccak (through the eXtended Keccak Code Package). We also now support\nxxHash, an extremely fast non-cryptographic hash algorithm. More algorithms may be supported in the future.\n\n# Usage\n\nInstall via [NPM]((https://www.npmjs.org/package/bigint-hash)):\n```\n\u003e npm install --save bigint-hash\n```\n\nYou can find API documentation [here](https://no2chem.github.io/bigint-hash).\n\nTo get a Hash instance, use the `getHasher` function with a supported hash algorithm. You can find supported\ntypes in the documentation for the [HashType enum](https://no2chem.github.io/bigint-hash/enums/hashtype.html), or\nyou can pass most OpenSSL algorithm names (like `'sha256'`).\n```\nimport {getHasher, OutputType, HashType} from 'bigint-hash';\n\n// As with Node.js crypto API\ngetHasher(HashType.SHA1).update(Buffer.from('hello world')).digest();\n\n// To get a bigInt, use either:\ngetHasher(HashType.SHA1).update(Buffer.from('hello world')).digest(OutputType.BigInt);\ngetHasher(HashType.SHA1).update(Buffer.from('hello world')).digestAsBigInt();\n```\n\nOnly hashing a single buffer? Use the one-shot API for better performance:\n```\nimport {hashAsBigInt, hashAsBuffer} from 'bigint-hash';\n\nhashAsBigInt(HashType.SHA1, Buffer.from('hello world'));\nhashAsBuffer(HashType.SHA1, Buffer.from('hello world'));\n```\n\n# Performance\n\nSo how does it all perform? Benchmarks can be run by using `npm run benchmark`.\n\nFor example, for SHA256:\n```\nRunning SHA256...\nnode-crypto-buf: 274981±6.02% ops/s 3636.62±893.055 ns/op (64 runs)\nnode-crypto-bigint: 267242±5.91% ops/s 3741.93±930.464 ns/op (68 runs)\nbiginthash-buf: 403311±13.51% ops/s 2479.48±1439.719 ns/op (71 runs)\nbiginthash-bigint: 505348±34.12% ops/s 1978.83±2733.946 ns/op (63 runs)\nbiginthash-oneshot-bigint: 1010083±0.65% ops/s 990.02±31.829 ns/op (93 runs)\nbiginthash-oneshot-buffer: 591309±1.96% ops/s 1691.16±155.995 ns/op (85 runs)\nFastest is biginthash-oneshot-bigint\n```\n\nHere, `node-crypto-buf` is the standard Node.js API, and `node-crypto-bigint` is that same API\nwith the overhead of converting to a bigint (using [bigint-buffer](https://github.com/no2chem/bigint-buffer)).\n\nDoing the conversion to bigint natively, `biginthash-bigint`, results in 1.8x better performance. Interestingly,\n`biginthash-buf`, which calls OpenSSL and should perform the same as `node-crypto-buf`, performs 1.4x better, which\nshould be investigated. \n\nUsing the one-shot API `biginthash-oneshot-bigint` yields about 3.4x better performance, thanks to not having to\nallocate an external context to hold hashing state and schedule garbage collection.\n\nFor SHA3, we compare against the widely used [keecak](https://github.com/cryptocoinjs/keccak) library.\n```\nRunning SHA3_256...\nsha3-buf: 140667±13.36% ops/s 7108.97±4278.996 ns/op (78 runs)\nsha3-bigint: 146018±1.24% ops/s 6848.49±395.861 ns/op (84 runs)\nbiginthash-buf: 365119±8.46% ops/s 2738.83±1010.152 ns/op (73 runs)\nbiginthash-bigint: 450430±29.27% ops/s 2220.10±2652.172 ns/op (64 runs)\nbiginthash-oneshot-bigint: 1010901±0.34% ops/s 989.22±16.845 ns/op (94 runs)\nbiginthash-oneshot-buffer: 584744±1.52% ops/s 1710.15±123.457 ns/op (87 runs)\nFastest is biginthash-oneshot-bigint\n```\n\n`biginthash-oneshot-bigint` performs 7x faster than the `sha3-buf`, which just uses the `keccak` library. \nEven `biginthash-buf` performs 2.6x better than `sha3-buf`. This is because bigint-hash uses an optimized version\nfrom the eXtended Keccak Code Package, while keccak uses the reference implementation.\n\nThere's also a significant amount of noise in the non-oneshot APIs. This is probably due to garbage collection overheads,\nas the Hasher must allocate temporary state, the noise is probably due to garbage collection kicking in much more frequently. \nWith the oneshot API there is significantly less noise, as less garbage is generated in the first place.\n\nVersion 0.2.0 introduced support for xxHash. This provides very fast (but non-cryptographic hashes):\n\n```\nRunning xxHash64...\nxxhash-node: 470517±8.23% ops/s 2125.32±697.178 ns/op (61 runs)\nxxhashjs-digest: 148855±1.42% ops/s 6717.97±460.271 ns/op (90 runs)\nxxhashjs-oneshot: 142618±2.80% ops/s 7011.76±927.270 ns/op (86 runs)\nbiginthash-buf: 423390±17.42% ops/s 2361.89±1625.679 ns/op (60 runs)\nbiginthash-bigint: 738984±21.47% ops/s 1353.21±1204.176 ns/op (66 runs)\nbiginthash-oneshot-bigint: 2435419±1.67% ops/s 410.61±32.956 ns/op (89 runs)\nbiginthash-oneshot-buffer: 762413±11.00% ops/s 1311.62±658.480 ns/op (80 runs)\nFastest is biginthash-oneshot-bigint\n```\n\nInterestingly, the bigint version is 3x more performant than the buffer version, highlighting\nhow much more buffer allocation costs over bigints.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fno2chem%2Fbigint-hash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fno2chem%2Fbigint-hash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fno2chem%2Fbigint-hash/lists"}