Nodejs 加密的最短哈希值是多少

use*_*657 17 node.js cryptojs

Nodejs 加密模块中的最短字符串哈希算法是什么?有没有类似于 crc32 的东西,它产生 8 个字符的 string ,但不幸的是加密本身不支持(我知道有外部模块,但我仅限于内置加密)。哈希冲突概率对于我的应用程序(缓存突发)并不重要。

Del*_*ite 38

shake256您可以使用支持该选项的 XOF 哈希函数outputLength(以字节为单位):

const crypto = require("crypto");

function createHash(data, len) {
    return crypto.createHash("shake256", { outputLength: len })
      .update(data)
      .digest("hex");
}

console.log(createHash("foo", 2))
// 1af9
console.log(createHash("foo", 8))
// 1af97f7818a28edf
Run Code Online (Sandbox Code Playgroud)