在Javascript中从字符串生成哈希 [英] Generate a Hash from string in Javascript
本文介绍了在Javascript中从字符串生成哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将字符串转换为某种形式的哈希。这在JavaScript中是否可行?
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.
推荐答案
String.prototype.hashCode = function() {
var hash = 0, i, chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
资料来源:
http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas -string-hashcode-method /
这篇关于在Javascript中从字符串生成哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文