@enonic/fnv-plus

中文文档
1.3.0发布于 2019/06/17MITCommonJS零依赖近 30 天下载 10.2万

Javascript FNV-1a Hashing Algorithm up to 1024 bits, with highly optimized 32bit and 52bit implementations.

原始使用文档
文档目录6 节
  1. Concept
  2. Install
  3. Usage
  4. API
  5. Fast variants
  6. License

fnv-plus

NPM version Build status

Javascript FNV-1a Hashing Algorithm up to 1024 bits, with highly optimized 32bit and 52bit implementations.

Concept

The FNV-1a hash algorithm, often simply called "fnv", disperses hashes throughout the n-bit hash space with very good dispersion and is very fast.

Use this module to generate unique hash/checksum values for Javascript strings or objects. Note: The FNV-1a algorithm is not even remotely suitable as a cryptographic pseudo-random generator, and should not be used to secure any thing for any reason. It is designed for uniqueness, not randomness.

Why fnv-plus?
  • It is the fastest FNV implementation available for Node.js (thanks desudesutalk). See Benchmarks.
  • It is the only npm module that is capable of generating fnv hashes for keyspaces larger than 32 bits.
  • fnv-plus is well-tested. Many other fnv implementations offer no unit tests to prove they work and are performant.
  • fnv-plus implements a 52bit version of FNV-1a which provides a larger hash space while still making use of Javascript's 53-bit integer space.
New in 1.3.x
New in 1.2.x
  • You can easily define custom seeds.
  • the hash() function can now take arbitrary Javascript objects as input.
  • changed default bitlength to 52

Install

$ npm install fnv-plus --save

Usage

var fnv = require('fnv-plus'),
    astring = 'hello world',
    ahash52 = fnv.hash(astring),        // 52-bit hash by default
    ahash64 = fnv.hash(astring, 64);    // 64-bit hash specified

console.log(ahash52.hex() == 'a65e7023cd59e');    //true
console.log(ahash52.str() == 'stglysbf6m');       //true
console.log(ahash52.dec() == '2926792616498590'); //true

console.log(ahash64.hex() == '779a65e7023cd2e7');     //true
console.log(ahash64.str() == '1th7cxzlyc0dj');        //true
console.log(ahash64.dec() == '8618312879776256743');  //true

// fast variants
console.log(fnv.fast1a32hex(astring) == 'd58b3fa7');      //true
console.log(fnv.fast1a52hex(astring) == 'a65e7023cd59e'); //true

fnv.seed('foobar testseed');
console.log(fnv.hash(astring, 64).hex() == ahash64.hex()); // false
// ^^ because the default seed is not 'foobar testseed'

API

fnv.hash(string, bitlength)

  • Hash a string using the given bit length (52 is default)
  • returns a FnvHash object

fnv.seed(string)

  • Seed the algorithm to produce different values. Hashing the same value with different seeds will very likely result in different results. To the extent your seed can be random, it can serve as a source of randomness, but nonetheless is not a replacement for a crypgographic PRG (pseudo-random generator).
  • default seed is chongo <Landon Curt Noll> /\>./\\

fnv.useUTF8(bool)

  • Controls UTF-8 awareness of hash functions
  • default is false

FnvHash.str()

Returns the hashed value as an ascii string

FnvHash.hex()

Returns the hashed value as a hexadecimal string

FnvHash.dec()

Returns the hashed value as a decimal string

Fast variants

This functions runs faster because they have no lib-overhead (see benchmarks for more info). They always compute 1a version of hashes and always use default seed. Directly returns hash values (not FnvHash object).

fnv.fast1a32(string)

  • Calculate FNV-1a 32bit hash
  • returns int

fnv.fast1a32hex(string)

  • Calculate FNV-1a 32bit hash
  • returns hex string

fnv.fast1a52(string)

  • Calculate FNV-1a 52bit hash
  • returns int

fnv.fast1a52hex(string)

  • Calculate FNV-1a 52bit hash
  • returns hex string

fnv.fast1a64(string)

  • Calculate FNV-1a 64bit hash
  • returns hex string

fnv.fast1a32utf(string)

  • Calculate FNV-1a 32bit hash
  • handles UTF-8 strings
  • returns int

fnv.fast1a32hexutf(string)

  • Calculate FNV-1a 32bit hash
  • handles UTF-8 strings
  • returns hex string

fnv.fast1a52utf(string)

  • Calculate FNV-1a 52bit hash
  • handles UTF-8 strings
  • returns int

fnv.fast1a52hexutf(string)

  • Calculate FNV-1a 52bit hash
  • handles UTF-8 strings
  • returns hex string

fnv.fast1a64utf(string)

  • Calculate FNV-1a 64bit hash
  • handles UTF-8 strings
  • returns hex string

License

MIT

版本与发布记录

@enonic/fnv-plus 共发布 1 个版本,首次发布于 2019/06/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.0latest2019/06/177 年前

关于 @enonic/fnv-plus 的常见问题

@enonic/fnv-plus 是什么?

Javascript FNV-1a Hashing Algorithm up to 1024 bits, with highly optimized 32bit and 52bit implementations.

如何安装 @enonic/fnv-plus?

在项目目录运行 npm install @enonic/fnv-plus,或使用 pnpm add / yarn add / bun add。

@enonic/fnv-plus 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/enonic__fnv-plus 是否存在。

@enonic/fnv-plus 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@enonic/fnv-plus 可以商用吗?

@enonic/fnv-plus 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@enonic/fnv-plus 的下载量有多少?

根据 npm 官方下载统计,@enonic/fnv-plus 在 2026/10/14 – 2026/11/12 这 30 天内被下载 101,862 次,日均约 3,395 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@enonic/fnv-plus 还在维护吗?

最新版本 1.3.0 发布于 2019/06/17(7 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/06/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。