Generate a Hash from string in Javascript

前端 未结 22 998
不知归路
不知归路 2020-11-22 03:34

I need to convert strings to some form of hash. Is this possible in JavaScript?

I\'m not utilizing a server-side language so I can\'t do it that way.

22条回答
  •  情话喂你
    2020-11-22 04:28

    A fast and concise one which was adapted from here:

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

提交回复
热议问题