Logo of ohash

ohash

基于 murmurhash3,用纯 JS 编写的超快速哈希库

采用纯 JavaScript 编写的超高速哈希库

用法

安装包

# npm
npm install ohash

# yarn
yarn add ohash

# pnpm
pnpm install ohash

导入

// ESM
import { hash, objectHash, murmurHash, sha256 } from "ohash";

// CommonJS
const { hash, objectHash, murmurHash, sha256 } = require("ohash");

hash(object, options?)

使用 objectHash 将对象值转换为字符串哈希,然后应用带 Base64 编码的 sha256(截取长度为 10)。

用法

import { hash } from "ohash";

// "dZbtA7f0lK"
console.log(hash({ foo: "bar" }));

objectHash(object, options?)

将嵌套对象值转换为稳定安全的字符串以进行哈希处理。

用法

import { objectHash } from "ohash";

// "object:1:string:3:foo:string:3:bar,"
console.log(objectHash({ foo: "bar" }));

isEqual(obj1, obj2, options?)

使用引用相等性和稳定对象哈希比较两个对象。

用法

import { isEqual } from "ohash";

// true
console.log(isEqual({ a: 1, b: 2 }, { b: 2, a: 1 }));

diff(obj1, obj2, options?)

使用嵌套哈希比较两个对象。返回一个包含更改的数组。

返回值为一个包含 $key$hash$value$props 的差异条目数组。进行日志记录时,会显示更改日志的字符串版本。

用法

import { diff } from "ohash";

const createObject = () => ({
  foo: "bar",
  nested: {
    y: 123,
    bar: {
      baz: "123",
    },
  },
});

const obj1 = createObject();
const obj2 = createObject();

obj2.nested.x = 123;
delete obj2.nested.y;
obj2.nested.bar.baz = 123;

const diff = diff(obj1, obj2);

// [-] Removed nested.y
// [~] Changed nested.bar.baz from "123" to 123
// [+] Added   nested.x
console.log(diff(obj1, obj2));

murmurHash(str)

使用 MurmurHash3 将输入字符串(任意长度)转换为 32 位正整数。

用法

import { murmurHash } from "ohash";

// "2708020327"
console.log(murmurHash("Hello World"));

sha256

从输入字符串创建安全的 SHA 256 摘要。

import { sha256 } from "ohash";

// "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e"
console.log(sha256("Hello World"));

sha256base64

从输入字符串创建带 Base64 编码的安全 SHA 256 摘要。

import { sha256base64 } from "ohash";

// "pZGm1Av0IEBKARczz7exkNYsZb8LzaMrV7J32a2fFG4"
console.log(sha256base64("Hello World"));

💻 开发

  • 克隆此仓库
  • 使用 corepack enable 启用 Corepack(对于 Node.js < 16.10,请使用 npm i -g corepack
  • 使用 pnpm install 安装依赖项
  • 使用 pnpm dev 运行交互式测试

许可

用心制作 💛

根据 MIT 许可证 发布。

基于 Scott Puleopuleos/object-hash,以及 Gary CourtAustin Applebyperezd/node-murmurhashgarycourt/murmurhash-js 的实现,以及 brix/crypto-js