add hashstring

This commit is contained in:
Nicholas Jitkoff 2022-06-13 00:05:12 -07:00
parent e260fd3814
commit 557c0a0784
2 changed files with 20 additions and 3 deletions

View file

@ -255,6 +255,24 @@ function decompressDataURL(dataURL, preamble, callback) {
}
}
async function hashString(string, base = 36) {
const arrayBuffer = await(new TextEncoder().encode(string))
const hashAsArrayBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);
const uint8ViewOfHash = new Uint8Array(hashAsArrayBuffer);
const hashAsHex = Array.from(uint8ViewOfHash).map(b => b.toString(16).padStart(2, '0')).join('');
if (base == 16) return hashAsHex;
if (base == 36) {
const guid = BigInt("0x" + hashAsHex);
const asBase36 = guid.toString(36).toLowerCase();
return asBase36;
}
const hashAsBase64 = btoa(String.fromCharCode.apply(null, uint8ViewOfHash));
hashAsBase64.replace(/=/g,'').replace(/[\+\/+]/g, "-").toLowerCase();
return hashAsBase64;
}
function compressString(string, encoding = LZMA64_MARKER, callback) {
if (encoding == LZMA64_MARKER) {
loadScript("/js/lzma/lzma_worker-min.js", () => {
@ -382,6 +400,7 @@ export {
decompressString,
compressDataURL,
decompressDataURL,
hashString,
BASE64_MARKER,
LZMA64_MARKER,
GZIP64_MARKER,

View file

@ -1,3 +1 @@
<!doctype html>
<meta charset=utf-8>
<script src="/render.js"></script>`
<!doctype html><meta charset=utf-8><script src="/render.js"></script>