SHA-256
以下のコードを実行
code:sha256.js
function addHtml() {
$('#container').append(`
<table border="1">
<tr>
<td>message</td>
<td><input type="text" id="message" onkeyup="calcSha256()" size="80" /></td>
</tr>
<tr>
<td>hash value</td>
<td><input type="text" id="hash" size="80" style="font-family:monospace;" /></td>
</tr>
</table>
`)
}
async function calcSha256() {
const message = new TextEncoder("utf-8").encode($("#message").val())
const hash = await crypto.subtle.digest("SHA-256", message)
$("#hash").val(buf2hexStr(hash))
}
function buf2hexStr(buffer) {
return Array.from(new Uint8Array(buffer))
.map(x => x.toString(16).padStart(2, "0"))
.join("")
}
(async () => {
await loadScript("./libs/jquery/jquery-3.6.3.min.js")
addHtml()
})()