iMurmurHash.JS
基于JavaScript的MurmurHash3(32位)哈希算法的增量实现 Gary Court的实施。
安装
要在浏览器中使用iMurmurHash, 下载最新版本 并将其作为脚本包含在您的网站上。
<script type="text/javascript" src="/scripts/imurmurhash.min.js"></script>
<script>
// Your code here, access iMurmurHash using the global object MurmurHash3
</script>
要在Node.js中使用iMurmurHash,请使用NPM安装该模块:
npm install imurmurhash
然后只需将其包含在脚本中:
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 0x29d3f1e3
功能
MurmurHash3 ([string], [seed])
获取哈希状态对象,可选择使用给定的初始化 字符串 和 种子。 种子 如果提供,则必须为正整数。调用此函数而不使用 new 关键字将返回一个已重置的缓存状态对象。只要对象仅在单个线程中使用,并且在对该对象进行操作时不创建其他哈希值,就可以安全使用。如果不能满足此约束,则可以使用 new 来创建一个新的状态对象。例如:
// 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();
如果您有不同的用例,这两种方法都可以混合使用。
MurmurHash3.prototype。散列(字符串)
增量地添加 字符串 到散列。可以根据需要对哈希状态对象调用该方法多次,包括在调用 result(). 返回 this 所以调用可以被链接。
MurmurHash3.prototype。结果()
获取哈希结果为32位正整数。这将执行算法的尾部和终结器部分,但不会将结果存储在状态对象中。这意味着获得结果然后继续通过添加字符串是完全安全的 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)
MurmurHash3.prototype。重置(种子)
重置状态对象以便重用,可选地使用给定的 种子 (与构造函数一样默认为0)。返回 this 所以调用可以被链接。
许可证(麻省理工学院)
版权所有(c) 2013加里法院,延斯泰勒
特此许可,免费授予任何人获得的副本 本软件及相关文档文件(“软件”),以处理 本软件不受任何限制,包括但不限于 使用、复制、修改、合并、发布、分发、再许可和/或出售的副本 软件,并允许软件提供给的人这样做, 但须符合以下条件:
上述版权声明和本许可声明应包括在所有 本软件的副本或实质性部分。
本软件是“按原样”提供的,没有任何形式的、明示的或明示的保证 默示,包括但不限于适销性、适用性的保证 为特定目的而非侵权。在任何情况下,作者或 版权持有人对任何索赔、损害或其他责任负有责任,无论 在合同、侵权或其他方面的诉讼中,由 与该软件的联系或对该软件的使用或其他交易。