Last active
July 31, 2021 09:00
-
-
Save moinologics/0a0f1acf0fe3279063d6a40d2208d5ab to your computer and use it in GitHub Desktop.
generating hash on client side browser, tested 1.7 GB file in 17 seconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Generating Hash within Browser</title> | |
</head> | |
<body> | |
<input type="file" name="" id="file" onchange="generate_hash(event.target.files[0])"> | |
<br><br><br> | |
<h4 id="hash_value"></h4> | |
</body> | |
</html> | |
<script> | |
function bytesToHexString(bytes) | |
{ | |
if (!bytes) return null | |
bytes = new Uint8Array(bytes) | |
var hexBytes = [] | |
for (var i = 0; i < bytes.length; i++) { | |
var byteString = bytes[i].toString(16) | |
if (byteString.length < 2) | |
byteString = "0" + byteString | |
hexBytes.push(byteString) | |
} | |
return hexBytes.join("") | |
} | |
async function generate_hash(file, algo='SHA-1') | |
{ | |
hash_el.innerText = '' | |
let start_time = performance.now() | |
let file_content = await (new Response(file)).arrayBuffer() | |
window.crypto.subtle.digest(algo, file_content).then(hash => { | |
let digest = bytesToHexString(hash) | |
let total_time = parseInt(performance.now() - start_time) | |
hash_el.innerText = `Algorithm: ${algo} | |
Hash: ${digest} | |
Time: ${total_time} milisecond` | |
}) | |
} | |
let hash_el = document.getElementById('hash_value') | |
hash_el.innerText = window.isSecureContext ? 'generated hash will show here' : 'https or localhost required' | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment