Skip to content

Instantly share code, notes, and snippets.

@petrvecera
Created July 12, 2019 11:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petrvecera/778cc7eba374d54c14288bf6094311db to your computer and use it in GitHub Desktop.
Save petrvecera/778cc7eba374d54c14288bf6094311db to your computer and use it in GitHub Desktop.
Calculate MD5 hash of file without the need to download it ( it's still downloads, bust just in the browser)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>title</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>
<script type="text/javascript">
function calculateMD5(blob) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.readAsArrayBuffer(blob);
reader.onloadend = function () {
var wordArray = CryptoJS.lib.WordArray.create(reader.result),
hash = CryptoJS.MD5(wordArray).toString();
resolve(hash)
};
})
}
function downloadAndCreateHash(){
const hashResultDiv = document.getElementById('hash-result')
fetch(document.getElementById('url-input').value)
.then(function(response) {
return response.blob()
})
.then(blob => {
return calculateMD5(blob)
})
.then(hash => {
hashResultDiv.innerHTML = hash
console.log(`Hash of the file is: ${hash}`)
})
.catch(err => {
hashResultDiv.innerHTML = err
console.error(err)
});
}
</script>
</head>
<body style="text-align: center;">
<h1>Enter the url of the file:</h1>
<input id="url-input" type="text" oninput="downloadAndCreateHash()" style="width: 400px">
<h2>MD5 hash of your file is:</h2>
<div id="hash-result">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment