常用字符串hash算法的node封裝

字號(hào):


    把一些常用的字符串hash算法做了一下封裝
    [js]代碼:
    // hash.js
    exports.additive = function (key, prime) {
    key = key instanceof Buffer ? key : new Buffer(key);
    prime = prime == undefined ? 0xffffffff : prime;
    for (var hash = key.length, i = 0; i < key.length; i++) {
    hash += key[i];
    }
    return (hash % prime);
    }
    exports.rotating = function(key) {
    key = key instanceof Buffer ? key : new Buffer(key);
    for (var hash = key.length, i = 0; i> 28) ^ key[i];
    }
    return hash;
    }
    exports.bernstein = function(key, prime) {
    key = key instanceof Buffer ? key : new Buffer(key);
    prime = prime == undefined ? 0xffffffff : prime;
    var hash = 0;
    for (i=0; i> 7;
    hash += hash << 3;
    hash ^= hash >> 17;
    hash += hash << 5;
    return hash;
    }
    exports.fnv1a = function (key){
    key = key instanceof Buffer ? key : new Buffer(key);
    var p = 16777619, hash = 0x811C9DC5;
    for(var i=0; i< key.length; i++) {
    hash = (hash ^ key[i]) * p;
    }
    hash += hash << 13;
    hash ^= hash >> 7;
    hash += hash << 3;
    hash ^= hash >> 17;
    hash += hash << 5;
    return hash;
    }