Skip to content

Instantly share code, notes, and snippets.

@acidsound
Last active January 16, 2018 18:39
Show Gist options
  • Save acidsound/406f1ba0275a8d7a27a24e9d08eb69a4 to your computer and use it in GitHub Desktop.
Save acidsound/406f1ba0275a8d7a27a24e9d08eb69a4 to your computer and use it in GitHub Desktop.
file to sha256hash
license: gpl-3.0
height: 600
blobToHash = (blob, cb)->
fileReader = new FileReader
fileReader.onload = (event)->
cb asmCrypto.SHA256.bytes event.target.result
fileReader.readAsArrayBuffer blob
document.addEventListener 'DOMContentLoaded', ->
document.getElementById 'fileUpload'
.addEventListener 'change', (e)->
blobToHash e.currentTarget.files[0], (hash)->
document.getElementById 'fileHash'
.textContent = Array.from hash
.map (o)-> o.toString 16
.join ''
document.getElementById 'url2hash'
.addEventListener 'submit', (e)->
fetch document.getElementById('url').value
.then (response)->
response.blob()
.then (blob)->
blobToHash blob, (hash)->
document.getElementById 'urlHash'
.textContent = Array.from hash
.map (o)-> o.toString 16
.join ''
e.preventDefault()
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>AsmCrypto</title>
<meta name="description" content="media to Hash">
<link rel="stylesheet" href="https://cdn.rawgit.com/acidsound/Tle-CSS/master/dist/tle.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/asmCrypto/0.0.11/asmcrypto.js"></script>
<script src="index.js"></script>
</head>
<body>
<form id="blob2hash" action="#">
<div>
<label for="fileUpload">uniq fileID</label>
<input id="fileUpload" type="file">
</div>
<label for="fileHash">fileHash</label>
<div>
<textarea id="fileHash"></textarea>
</div>
</form>
<form id="url2hash" action="#">
<div>
<label for="url">uniq URLID</label>
<input id="url" type="text">
</div>
<button id="getUrlButton">get URL</button>
</form>
<label for="urlHash">urlHash</label>
<div>
<textarea id="urlHash"></textarea>
</div>
</body>
</html>
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const blobToHash = function(blob, cb){
const fileReader = new FileReader;
fileReader.onload = event=> cb(asmCrypto.SHA256.bytes(event.target.result));
return fileReader.readAsArrayBuffer(blob);
};
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('fileUpload')
.addEventListener('change', e=>
blobToHash(e.currentTarget.files[0], hash=>
document.getElementById('fileHash')
.textContent = Array.from(hash).map(o=>o.toString(16)).join('')
)
);
return document.getElementById('url2hash')
.addEventListener('submit', function(e){
fetch(document.getElementById('url').value)
.then(response=> response.blob()).then(blob=>
blobToHash(blob, hash=>
document.getElementById('urlHash')
.textContent = Array.from(hash).map(o=>o.toString(16)).join('')
)
);
return e.preventDefault();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment