请用js 写个实现数据结构中哈希表的拉链法解决方法、写个例子出来、感谢、问题解决追加分、

JavaScript011

请用js 写个实现数据结构中哈希表的拉链法解决方法、写个例子出来、感谢、问题解决追加分、,第1张

1. 计算出字符串的三个哈希值 (一个用来确定位置,另外两个用来校验)

2. 察看哈希表中的这个位置

3. 哈希表中这个位置为空吗?假如为空,则肯定该字符串不存在,返回

4. 假如存在,则检查其他两个哈希值是否也匹配,假如匹配,则表示找到了该字符串,返回

5. 移到下一个位置,假如已经越界,则表示没有找到,返回

6. 看看是不是又回到了原来的位置,假如是,则返回没找到

7. 回到3

怎么样,很简单的算法吧,但确实是天才的idea, 其实最优秀的算法往往是简单有效的算法。

使用js sha512加密的方法:

1、首先去git上下载sha512.js引入需要调用的页面上。

2、调用方法,在head和</head>之间的script标签写入以下js代码:

function calcHash() {

try {

var hashInput = document.getElementById("hashInputText")

var hashInputType = document.getElementById("hashInputType")

var hashVariant = document.getElementById("hashVariant")

var hashRounds = document.getElementById("hashRounds")

var hashOutputType = document.getElementById("hashOutputType")

var hashOutput = document.getElementById("hashOutputText")

var hashObj = new jsSHA(

hashVariant.options[hashVariant.selectedIndex].value,

hashInputType.options[hashInputType.selectedIndex].value,

{numRounds: parseInt(hashRounds.value, 10)}

)

hashObj.update(hashInput.value)

hashOutput.value = hashObj.getHash(hashOutputType.options[hashOutputType.selectedIndex].value)

} catch(e) {

hashOutput.value = e.message

}

}

function calcHMAC() {

try {

var hmacText = document.getElementById("hmacInputText")

var hmacTextType = document.getElementById("hmacTextType")

var hmacKeyInput = document.getElementById("hmacInputKey")

var hmacKeyInputType = document.getElementById("hmacKeyType")

var hmacVariant = document.getElementById("hmacVariant")

var hmacOutputType = document.getElementById("hmacOutputType")

var hmacOutput = document.getElementById("hmacOutputText")

var hmacObj = new jsSHA(

hmacVariant.options[hmacVariant.selectedIndex].value,

hmacTextType.options[hmacTextType.selectedIndex].value

)

hmacObj.setHMACKey(

hmacKeyInput.value,

hmacKeyInputType.options[hmacKeyInputType.selectedIndex].value

)

hmacObj.update(hmacText.value)

hmacOutput.value = hmacObj.getHMAC(hmacOutputType.options[hmacOutputType.selectedIndex].value)

} catch(e) {

hmacOutput.value = e.message

}

}

其中jsSHA是sha512.js内已经实现的方法。