Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
imurmurhash
Advanced tools
The imurmurhash npm package is a JavaScript implementation of the MurmurHash3 hashing algorithm. It's designed for efficiently generating 32-bit hashes from strings. This package is useful for tasks such as generating hash keys for data caching, creating compact unique identifiers for objects, and implementing hash-based data structures like bloom filters.
Hash Initialization and Update
This feature demonstrates initializing a new hash instance and updating it with a string. The `result` method is then used to get the hash value.
const MurmurHash3 = require('imurmurhash');
const hash = new MurmurHash3();
hash.hash('Hello, world!');
console.log(hash.result());
Incremental Hash Updates
This feature shows how to incrementally update the hash with multiple pieces of data. This is useful for hashing data streams or large datasets in chunks.
const MurmurHash3 = require('imurmurhash');
const hash = new MurmurHash3();
hash.hash('Hello, ');
hash.hash('world!');
console.log(hash.result());
Resetting Hash State
This feature illustrates how to reset the hash to its initial state after computing a hash value, allowing the same hash instance to be reused for hashing new data.
const MurmurHash3 = require('imurmurhash');
const hash = new MurmurHash3().hash('Hello, world!');
console.log(hash.result());
hash.reset();
hash.hash('Another string');
console.log(hash.result());
This package provides a pure JavaScript implementation of the MurmurHash algorithms. It's similar to imurmurhash but offers both MurmurHash2 and MurmurHash3 algorithms. The API is slightly different, and it may not support incremental updates like imurmurhash.
murmurhash3js is another implementation of the MurmurHash3 algorithm. It differs from imurmurhash by providing both 32-bit and 128-bit hash functions. This package might be preferred when a wider range of hash sizes is needed.
While not limited to MurmurHash, hash-wasm offers a wide range of hashing algorithms implemented in WebAssembly for high performance. It includes MurmurHash3 among its supported algorithms. This package is suitable for applications requiring high-speed hashing or support for multiple hash algorithms.
An incremental implementation of the MurmurHash3 (32-bit) hashing algorithm for JavaScript based on Gary Court's implementation with kazuyukitanimura's modifications.
This version works significantly faster than the non-incremental version if you need to hash many small strings into a single hash, since string concatenation (to build the single string to pass the non-incremental version) is fairly costly. In one case tested, using the incremental version was about 50% faster than concatenating 5-10 strings and then hashing.
To use iMurmurHash in the browser, download the latest version and include it as a script on your site.
<script type="text/javascript" src="/scripts/imurmurhash.min.js"></script>
<script>
// Your code here, access iMurmurHash using the global object MurmurHash3
</script>
To use iMurmurHash in Node.js, install the module using NPM:
npm install imurmurhash
Then simply include it in your scripts:
MurmurHash3 = require('imurmurhash');
// Create the initial hash
var hashState = MurmurHash3('string');
// Incrementally add text
hashState.hash('more strings');
hashState.hash('even more strings');
// All calls can be chained if desired
hashState.hash('and').hash('some').hash('more');
// Get a result
hashState.result();
// returns 0xe4ccfe6b
Get a hash state object, optionally initialized with the given string and seed. Seed must be a positive integer if provided. Calling this function without the new
keyword will return a cached state object that has been reset. This is safe to use as long as the object is only used from a single thread and no other hashes are created while operating on this one. If this constraint cannot be met, you can use new
to create a new state object. For example:
// Use the cached object, calling the function again will return the same
// object (but reset, so the current state would be lost)
hashState = MurmurHash3();
...
// Create a new object that can be safely used however you wish. Calling the
// function again will simply return a new state object, and no state loss
// will occur, at the cost of creating more objects.
hashState = new MurmurHash3();
Both methods can be mixed however you like if you have different use cases.
Incrementally add string to the hash. This can be called as many times as you want for the hash state object, including after a call to result()
. Returns this
so calls can be chained.
Get the result of the hash as a 32-bit positive integer. This performs the tail and finalizer portions of the algorithm, but does not store the result in the state object. This means that it is perfectly safe to get results and then continue adding strings via hash
.
// Do the whole string at once
MurmurHash3('this is a test string').result();
// 0x70529328
// Do part of the string, get a result, then the other part
var m = MurmurHash3('this is a');
m.result();
// 0xbfc4f834
m.hash(' test string').result();
// 0x70529328 (same as above)
Reset the state object for reuse, optionally using the given seed (defaults to 0 like the constructor). Returns this
so calls can be chained.
Copyright (c) 2013 Gary Court, Jens Taylor
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
An incremental implementation of MurmurHash3
The npm package imurmurhash receives a total of 38,174,623 weekly downloads. As such, imurmurhash popularity was classified as popular.
We found that imurmurhash demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.