JavaScript/HASH:修订间差异
来自康健生活
创建页面,内容为“== 标题文本 == == HASH CODE == 实现一: {{leftrightflex|<syntaxhighlight lang="javascript"> String.prototype.HASHCode = function(){ let hash = 0; for (let i = 0; i < this.length; i++) { hash = ((hash << 5) - hash) + this.charCodeAt(i); // Convert to 32bit integer hash = hash & hash; } return hash; } console.log("Hello World!".HASHCode());</syntaxhighlight>|{{consoleout|<pre>-969099747 </pre>}}}}” |
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|” |
||
| (未显示同一用户的13个中间版本) | |||
| 第1行: | 第1行: | ||
== | == 引言 == | ||
== | == 哈希化 == | ||
== 哈希函数 == | |||
== 哈希表 == | |||
== 冲突 == | |||
== 探测 == | |||
=== 线性探测 === | |||
=== 二次探测 === | |||
== 哈希码(HASH CODE) == | |||
哈希码是一种从长字符串计算小(32 位)摘要数字键的方法。 | |||
=== 负散列码 === | |||
当哈希码的计算值超出Integer.MAX_VALUE (<math>2 ^ {31}</math>),计算值将得到一个负整数。 | |||
=== 相移掩模(shift mask) === | |||
<syntaxhighlight lang="javascript" inline> | |||
(key.hashCode() & 0x7fffffff) % M</syntaxhighlight>,其中 M 是表的大小。 | |||
=== 霍纳算法 === | |||
实现一: | 实现一: | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
String.prototype.HASHCode = function(){ | String.prototype.HASHCode = function(){ | ||
let hash = 0; | let hash = 0; | ||
| 第13行: | 第31行: | ||
} | } | ||
console.log("Hello World!".HASHCode());</syntaxhighlight>|{{consoleout|<pre>-969099747 | console.log("Hello World!".HASHCode());</syntaxhighlight>|{{consoleout|<pre>-969099747 | ||
</pre>}}}} | |||
实现二: | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
String.prototype.HASHCode = function(){ | |||
const MAX_VALUE = 0x7fffffff; | |||
const MIN_VALUE = -0x80000000; | |||
let hash = 0; | |||
for (let i = 0; i < this.length; i++) { | |||
hash = hash * 31 + this.charCodeAt(i); | |||
if(hash > MAX_VALUE || hash < MIN_VALUE) hash &= 0xFFFFFFFF; | |||
} | |||
return hash; | |||
} | |||
console.log("Hello World!".HASHCode()); </syntaxhighlight>|{{consoleout|<pre>-969099747 | |||
</pre>}}}} | |||
== SHA == | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
String.prototype.SHACode = async function(){ | |||
const msgUint8 = new TextEncoder().encode(this) | |||
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8) | |||
const hashArray = Array.from(new Uint8Array(hashBuffer)) | |||
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('') | |||
return hashHex; | |||
} | |||
console.log(await "Hello World!".SHACode()); </syntaxhighlight>|{{consoleout|<pre> | |||
7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069 | |||
</pre>}}}} | </pre>}}}} | ||
2026年11月26日 (四) 09:05的最新版本
引言
哈希化
哈希函数
哈希表
冲突
探测
线性探测
二次探测
哈希码(HASH CODE)
哈希码是一种从长字符串计算小(32 位)摘要数字键的方法。
负散列码
当哈希码的计算值超出Integer.MAX_VALUE (),计算值将得到一个负整数。
相移掩模(shift mask)
(key.hashCode() & 0x7fffffff) % M,其中 M 是表的大小。
霍纳算法
实现一:
String.prototype.HASHCode = function(){
let hash = 0;
for (let i = 0; i < this.length; i++) {
hash = ((hash << 5) - hash) + this.charCodeAt(i);
// Convert to 32bit integer
hash = hash & hash;
}
return hash;
}
console.log("Hello World!".HASHCode());
-969099747
实现二:
String.prototype.HASHCode = function(){
const MAX_VALUE = 0x7fffffff;
const MIN_VALUE = -0x80000000;
let hash = 0;
for (let i = 0; i < this.length; i++) {
hash = hash * 31 + this.charCodeAt(i);
if(hash > MAX_VALUE || hash < MIN_VALUE) hash &= 0xFFFFFFFF;
}
return hash;
}
console.log("Hello World!".HASHCode());
-969099747
SHA
String.prototype.SHACode = async function(){
const msgUint8 = new TextEncoder().encode(this)
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8)
const hashArray = Array.from(new Uint8Array(hashBuffer))
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
return hashHex;
}
console.log(await "Hello World!".SHACode());
7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069
No categories