我需要将字符串转换为某种形式的哈希。这在JavaScript中是可能的吗?

我没有使用服务器端语言,所以我不能这样做。


当前回答

我需要一个类似的函数(但不同)来根据用户名和当前时间生成一个唯一的ish ID。因此:

window.newId = ->
  # create a number based on the username
  unless window.userNumber?
    window.userNumber = 0
  for c,i in window.MyNamespace.userName
    char = window.MyNamespace.userName.charCodeAt(i)
    window.MyNamespace.userNumber+=char
  ((window.MyNamespace.userNumber + Math.floor(Math.random() * 1e15) + new Date().getMilliseconds()).toString(36)).toUpperCase()

生产:

2DVFXJGEKL
6IZPAKFQFL
ORGOENVMG
... etc 

编辑2022年7月:正如@canRau指出的那样,shortid的作者现在更喜欢nanoidhttps://github.com/ai/nanoid/

其他回答

这是一个快速而简洁的版本,改编自这里:

String.prototype.hashCode = function() {
  var hash = 5381, i = this.length
  while(i)
    hash = (hash * 33) ^ this.charCodeAt(--i)
  return hash >>> 0;
}

我参加派对有点晚了,但你可以使用这个模块:加密:

const crypto = require('crypto');

const SALT = '$ome$alt';

function generateHash(pass) {
  return crypto.createHmac('sha256', SALT)
    .update(pass)
    .digest('hex');
}

此函数的结果始终是64个字符的字符串;类似于:“aa54e7563b1964037849528e7ba068eb7767b1fab74a8d80fe300828b996714a”

注意:即使使用最好的32位哈希,冲突也迟早会发生。哈希冲突概率可以计算为,近似为(参见此处)。这可能比直觉所暗示的更高:假设32位哈希和k=10000个项目,则发生冲突的概率为1.2%。77163个样本的概率为50%!(计算器)。我建议在底部使用变通方法。

在回答这个问题时哪种哈希算法最适合唯一性和速度?,伊恩·博伊德发表了一篇很好的深入分析。简而言之(正如我所解释的那样),他得出的结论是MurmurHash是最好的,其次是FNV-1a。esmiralha提出的Java String.hashCode()算法似乎是DJB2的变体。

FNV-1a的分布比DJB2更好,但速度较慢DJB2比FNV-1a更快,但倾向于产生更多的碰撞MurmurHash3比DJB2和FNV-1a更好更快(但优化的实现需要比FNV和DJB2更多的代码行)

这里有一些输入字符串较大的基准测试:http://jsperf.com/32-bit-hash当对短输入字符串进行散列处理时,相对于DJ2B和FNV-1a,杂音的性能会下降:http://jsperf.com/32-bit-hash/3

因此,总的来说,我会推荐杂音3。请参阅此处了解JavaScript实现:https://github.com/garycourt/murmurhash-js

如果输入字符串很短,性能比分发质量更重要,请使用DJB2(如esmiralha接受的答案所建议的)。

如果质量和小代码大小比速度更重要,我使用FNV-1a的这个实现(基于这个代码)。

/**
 * Calculate a 32 bit FNV-1a hash
 * Found here: https://gist.github.com/vaiorabbit/5657561
 * Ref.: http://isthe.com/chongo/tech/comp/fnv/
 *
 * @param {string} str the input value
 * @param {boolean} [asString=false] set to true to return the hash value as 
 *     8-digit hex string instead of an integer
 * @param {integer} [seed] optionally pass the hash of the previous chunk
 * @returns {integer | string}
 */
function hashFnv32a(str, asString, seed) {
    /*jshint bitwise:false */
    var i, l,
        hval = (seed === undefined) ? 0x811c9dc5 : seed;

    for (i = 0, l = str.length; i < l; i++) {
        hval ^= str.charCodeAt(i);
        hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
    }
    if( asString ){
        // Convert to 8 digit hex string
        return ("0000000" + (hval >>> 0).toString(16)).substr(-8);
    }
    return hval >>> 0;
}

提高碰撞概率

如这里所解释的,我们可以使用此技巧扩展哈希位大小:

function hash64(str) {
    var h1 = hash32(str);  // returns 32 bit (as 8 byte hex string)
    return h1 + hash32(h1 + str);  // 64 bit (as 16 byte hex string)
}

小心使用,但不要期望太多。

我有点惊讶,还没有人谈论新的SubtleCryptoAPI。

要从字符串中获取哈希,可以使用suble.desumest方法:

函数getHash(str,algo=“SHA-256”){let strBuf=newTextEncoder().encode(str);return crypto.define.digest(算法,strBuf).then(哈希=>{window.hash=哈希;//这里hash是arrayBuffer,//所以我们将其转换为十六进制版本let result=“”;const view=新数据视图(哈希);for(设i=0;i<hash.byteLength;i+=4){result+=('000000000'+view.getUint32(i).toString(16)).sslice(-8);}返回结果;});}getHash('hello world').then(哈希=>{console.log(哈希);});

我基于FNV的乘法+Xor方法的快速(非常长)一行:

my_string.split('').map(v=>v.charCodeAt(0)).reduce((a,v)=>a+((a<<7)+(a<<3))^v).toString(16);