js 对字符串hash加密

      String.prototype.hashCode = function(){
        if (Array.prototype.reduce){
            return this.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);              
        } 
        var hash = 0;
        if (this.length === 0) return hash;
        for (var i = 0; i < this.length; i++) {
            var character  = this.charCodeAt(i);
            hash  = ((hash<<5)-hash)+character;
            hash = hash & hash; // Convert to 32bit integer
        }
        return hash;
    }
var hash = new String(‘something’).hashCode();

 

转载于:https://www.cnblogs.com/wrongcode/p/11015660.html

你可能感兴趣的:(js 对字符串hash加密)