https://github.com/robertklep/node-metrohash
Node.js bindings for MetroHash
https://github.com/robertklep/node-metrohash
hash-functions javascript metrohash node
Last synced: 8 months ago
JSON representation
Node.js bindings for MetroHash
- Host: GitHub
- URL: https://github.com/robertklep/node-metrohash
- Owner: robertklep
- License: mit
- Created: 2015-05-28T12:30:51.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2025-01-22T09:07:53.000Z (over 1 year ago)
- Last Synced: 2025-09-28T23:16:08.385Z (9 months ago)
- Topics: hash-functions, javascript, metrohash, node
- Language: C++
- Size: 300 KB
- Stars: 27
- Watchers: 1
- Forks: 9
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# node-metrohash
Wrapper around [MetroHash](https://github.com/jandrewrogers/MetroHash).
[](https://travis-ci.org/robertklep/node-metrohash)
[](https://ci.appveyor.com/project/robertklep/node-metrohash/branch/master)
### Installation
```
$ npm install metrohash
```
### API change!
Between v1 and v2, the API for this library has changed to allow for maximum hashing speed.
The biggest change is that a calculated hash is now returned as a (hex-encoded) string instead of a `Buffer`.
If a `Buffer` is still required, it's easy to convert the string:
```
let buffer = Buffer.from(metrohash64('input'), 'hex');
```
Also, the `.hash()` methods for the hasher classes have been removed in favor of standalone functions (see below).
### Usage
The module exports 2 classes, `MetroHash64` and `MetroHash128`, and two functions, `metrohash64` and `metrohash128`.
The classes are meant for incremental hashing, the functions for standalone hash calculations.
The class constructors and functions accept an optional `seed` numerical argument, which defaults to `0`.
#### Class interface
``` javascript
const MetroHash64 = require('metrohash').MetroHash64;
// Constructor.
MetroHash64(seed? : number) : this
// Update.
MetroHash64#update(input : String | Buffer) : this
// Finalize and get hash digest.
MetroHash64#digest() : String
```
(likewise for `MetroHash128`).
#### Function interface
```
const metrohash64 = require('metrohash').metrohash64;
metrohash64(input : String | Buffer, seed? : number) : String
```
(likewise for `metrohash128`).
### Examples
``` javascript
//// Classes
const MetroHash64 = require('metrohash').MetroHash64;
// Instantiate using seed 123 (`new` is optional).
let hash = new MetroHash64(123);
// Update using a string as input.
hash.update('Hello, World!');
// The same as above:
// hash.update('Hello, ').update('World!');
// Finalize to get the digest as a hex string.
let digest = hash.digest();
//// Functions
const metrohash64 = require('metrohash').metrohash64;
let digest = metrohash64('Hello, World!', 123);
```
### Speed
From v2.0 onwards, MetroHash is [pretty fast](https://medium.com/@drainingsun/in-search-of-a-good-node-js-hashing-algorithm-8052b6923a3b).