Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Hash Calculator</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <style>td{padding:10px;}</style>
- </head>
- <body>
- <div>Hash Calculator</div>
- <br>
- <div>
- <form method="POST" id = "form1" enctype="multipart/form-data">
- <input id="file" type="file">
- <br>
- <table>
- <tr>
- <td style="text-align: right;"> Beginning bit off-set:</td>
- <td><input type = "text" name="start" id="start"></td>
- </tr>
- <tr>
- <td style="text-align: right;"> Last bit off-set:</td>
- <td><input type = "text" name="lastbit" id="lastbit"></td>
- </tr>
- <tr>
- <td style="text-align: right;"> Segment Size:</td>
- <td><input type = "text" name="chunksize" id="chunk"></td>
- </tr>
- <tr>
- <td style="text-align: right;"> Hash Function:</td>
- <td>
- <select name="hashtype" id="hashtype">
- <option value="MD5">MD5</option>
- <option value="SHA1">SHA1</option>
- <option value="SHA256">SHA256</option>
- </select>
- </td>
- </tr>
- </table>
- <br>
- <input type="button" name="calculate" id="calculate" value="Calculate Hash">
- </form>
- <pre id=log></pre>
- <br>
- <pre id=log1></pre>
- <br>
- <pre id=log2></pre>
- </div>
- <script src="js/jquery-3.1.1.min.js" type="text/javascript"></script>
- <script src="js/spark-md5.min.js" type="text/javascript"></script>
- <script src="js/asmcrypto.js" type="text/javascript"></script>
- <script src="js/script.js" type="text/javascript"></script>
- </body>
- </html>
- /*
- * Setting initial value and global varible
- */
- var startbit = 0;
- var lastbit = 0;
- var chunkSize = 0;
- var type = 'md5';
- var file = '';
- var filesize = 0;
- var percent = 0;
- var log=document.getElementById("log");
- var log1=document.getElementById("log1");
- var log2=document.getElementById("log2");
- /*
- * Function to add number of 0 to keep range so every number has same digit.
- * @param {type} number eg : 1
- * @param {type} targetLength eg: 4
- * @returns {leftPad.output|String} output : 0001
- */
- function leftPad(number, targetLength)
- {
- var output = number + '';
- while (output.length < targetLength) {
- output = '0' + output;
- }
- return output;
- }
- /*
- * it clear all the details which has been print on the screen
- */
- function clearlog()
- {
- log.style.display="inline-block";
- log.innerHTML="";
- log1.style.display="inline-block";
- log1.innerHTML="";
- log2.style.display="inline-block";
- log2.innerHTML="";
- }
- /*
- * Validating start bit, end bit, segment(chunk size)
- */
- function validate()
- {
- var output1 = true;
- //For starting bit
- if(startbit == '')
- {
- log.innerHTML+="n Beginning bit off-set cannot not be emptyn";
- output1 = false;
- }
- else if(isNaN(startbit) || startbit < 0 )
- {
- log.innerHTML+="n Beginning bit off-set should be a number and should be 0 greater than 0 n";
- output1 = false;
- }
- else if(startbit>filesize)
- {
- log.innerHTML+="n Beginning bit off-set should less than file size ("+filesize+") n";
- output1 = false;
- }
- //For Ending bit
- if(lastbit == '')
- {
- log.innerHTML+="n Last bit off-set cannot not be emptyn";
- output1 = false;
- }
- else if(isNaN(lastbit) || lastbit < 1 )
- {
- log.innerHTML+="n Last bit off-set should be a number and should be greater than 0 n";
- output1 = false;
- }
- else if(lastbit>filesize)
- {
- log.innerHTML+="n Last bit off-set should less than file size ("+filesize+") n";
- output1 = false;
- }
- //For segment
- if(chunkSize == '')
- {
- log.innerHTML+="n Segment Size cannot not be emptyn";
- output1 = false;
- }
- else if(isNaN(chunkSize) || chunkSize < 1 )
- {
- log.innerHTML+="n Segment Size should be a number and should be greater than 0 n";
- output1 = false;
- }
- else if(chunkSize>filesize)
- {
- log.innerHTML+="n Segment Size should less than file size ("+filesize+") n";
- output1 = false;
- }
- else if((chunkSize%8) != 0)
- {
- log.innerHTML+="n Segment Size should be a mulitple of 8 n";
- output1 = false;
- }
- return output1;
- }
- /*
- * Getting file Details on file select and setting default to input field
- */
- $(document).on('change','#file',function ()
- {
- file = this.files[0];
- chunkSize = 1048576*8; //in bytes read in chunks of 1MB
- chunks = Math.ceil(file.size / chunkSize);
- lastbit = (file.size*8);
- filesize = lastbit;
- $('#start').val(startbit);
- $('#lastbit').val(lastbit);
- $('#chunk').val(chunkSize);
- clearlog();
- });
- $("#calculate").click(function()
- {
- clearlog();
- //Fetching value from input
- startbit = $('#start').val();
- lastbit = $('#lastbit').val();
- chunkSize = $('#chunk').val();
- type = $('#hashtype').val();
- if(file == '') //check for file is selected or not
- {
- log.innerHTML+="n Please Select file to proceed n";
- return false;
- }
- //validate for error if it does not rteurn error it will proceed else shows error
- if(validate())
- {
- //Conversion in bytes
- filesize = lastbit-startbit;
- var startbit1 = Math.ceil(startbit/8);
- var chunkSize1 = Math.ceil(chunkSize/8);
- var filesize1 = Math.ceil(filesize/8);
- chunks = Math.ceil(filesize1 / chunkSize1);
- //padding length of 0 for segment no
- var padd_length = parseInt((chunks+'').length);
- //Last segment size
- var last_segment = ((filesize1)%chunkSize1)*8;
- if(last_segment == 0) last_segment = (chunkSize1*8);
- if(type == 'SHA1') {var hash1 = new asmCrypto.SHA1; var hash2 = new asmCrypto.SHA1;}
- else if(type == 'SHA256') {var hash1 = new asmCrypto.SHA256; var hash2 = new asmCrypto.SHA256;}
- else {var hash1 = new SparkMD5.ArrayBuffer();var hash2 = new SparkMD5.ArrayBuffer();}
- //using bobslice to partion the file and process it
- var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
- currentChunk = 0,
- myChunk = 0,
- dummydata = new Array(),
- output1 = '', //Store hash value and display when called for column1
- output2 = '', //Store hash value and display when called for column2
- frOnload = function(e) //for column1
- {
- dummydata.push(e.target.result);
- if(type == 'MD5')
- {
- hash1.append(e.target.result); // append array buffer for collumn one
- $.each(dummydata, function (index, value) {
- hash2.append(value); // append array buffer for collumn two
- });
- output1 = hash1.end();
- output2 = hash2.end();
- }
- else
- {
- var arr1 ='';
- var arr2 = '';
- hash1.reset();
- hash2.reset();
- hash1.process(e.target.result); // append array buffer for collumn one
- $.each(dummydata, function (index, value) {
- hash2.process(value); //append array buffer for collumn two
- });
- //Getting output in decimal
- arr1 = hash1.finish().result;
- arr2 = hash2.finish().result;
- output1 = '';
- output2 = '';
- //converting output in hexadecimal
- $.each(arr1, function (index, value) {
- value = value.toString(16);
- if(value.length == 1)
- {
- value = '0'+value;
- }
- output1 +=value;
- });
- $.each(arr2, function (index, value) {
- value = value.toString(16);
- if(value.length == 1)
- {
- value = '0'+value;
- }
- output2 +=value;
- });
- }
- //condition to process till the chunk is not completed
- currentChunk++;
- if(currentChunk <= chunks)
- {
- loadNext();
- }
- },
- frOnerror = function () {
- log.innerHTML+="n oops, something went wrong.";
- };
- function loadNext()
- {
- //for column1
- var fileReader = new FileReader();
- fileReader.onload = frOnload;
- fileReader.onerror = frOnerror;
- var start = startbit1+(currentChunk * chunkSize1),
- end = ((start + chunkSize1) >= filesize1) ? filesize1 : start + chunkSize1;
- //call to frOnload to get hash value for column1
- fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
- //print segemwnt with hash value on each line
- if(currentChunk !=0)
- {
- log2.innerHTML+=" Segement Hash #"+leftPad(parseInt(currentChunk),padd_length)+":"+output1+"   "+output2+"n";
- percent = Math.round((currentChunk/chunks)*100); //transform to near integer value 0.1-0.4 as 0% and 0.5-0.9 as 1%
- log1.innerHTML = " Total: "+leftPad(parseInt(percent),3)+"%n";
- }
- };
- //Printing Details
- log.innerHTML = "n Filename: "+file.name+" n Hash: "+type+" n Segment Length: "+chunkSize+" n Segment Count:"+chunks+" n Last Segment Length: "+last_segment;
- log1.innerHTML = " Total: "+leftPad(parseInt(percent),3)+"%n"; //initial percent to 0
- //
- loadNext();
- }
- return false;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement