Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.29 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Hash Calculator</title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
  8. <style>td{padding:10px;}</style>
  9. </head>
  10. <body>
  11. <div>Hash Calculator</div>
  12. <br>
  13. <div>
  14. <form method="POST" id = "form1" enctype="multipart/form-data">
  15. <input id="file" type="file">
  16. <br>
  17. <table>
  18. <tr>
  19. <td style="text-align: right;"> Beginning bit off-set:</td>
  20. <td><input type = "text" name="start" id="start"></td>
  21. </tr>
  22. <tr>
  23. <td style="text-align: right;"> Last bit off-set:</td>
  24. <td><input type = "text" name="lastbit" id="lastbit"></td>
  25. </tr>
  26. <tr>
  27. <td style="text-align: right;"> Segment Size:</td>
  28. <td><input type = "text" name="chunksize" id="chunk"></td>
  29. </tr>
  30. <tr>
  31. <td style="text-align: right;"> Hash Function:</td>
  32. <td>
  33. <select name="hashtype" id="hashtype">
  34. <option value="MD5">MD5</option>
  35. <option value="SHA1">SHA1</option>
  36. <option value="SHA256">SHA256</option>
  37. </select>
  38. </td>
  39. </tr>
  40. </table>
  41. <br>
  42. <input type="button" name="calculate" id="calculate" value="Calculate Hash">
  43. </form>
  44. <pre id=log></pre>
  45. <br>
  46. <pre id=log1></pre>
  47. <br>
  48. <pre id=log2></pre>
  49. </div>
  50. <script src="js/jquery-3.1.1.min.js" type="text/javascript"></script>
  51. <script src="js/spark-md5.min.js" type="text/javascript"></script>
  52. <script src="js/asmcrypto.js" type="text/javascript"></script>
  53. <script src="js/script.js" type="text/javascript"></script>
  54. </body>
  55. </html>
  56.  
  57. /*
  58. * Setting initial value and global varible
  59. */
  60. var startbit = 0;
  61. var lastbit = 0;
  62. var chunkSize = 0;
  63. var type = 'md5';
  64. var file = '';
  65. var filesize = 0;
  66. var percent = 0;
  67. var log=document.getElementById("log");
  68. var log1=document.getElementById("log1");
  69. var log2=document.getElementById("log2");
  70.  
  71. /*
  72. * Function to add number of 0 to keep range so every number has same digit.
  73. * @param {type} number eg : 1
  74. * @param {type} targetLength eg: 4
  75. * @returns {leftPad.output|String} output : 0001
  76. */
  77. function leftPad(number, targetLength)
  78. {
  79. var output = number + '';
  80. while (output.length < targetLength) {
  81. output = '0' + output;
  82. }
  83. return output;
  84. }
  85.  
  86. /*
  87. * it clear all the details which has been print on the screen
  88. */
  89. function clearlog()
  90. {
  91. log.style.display="inline-block";
  92. log.innerHTML="";
  93. log1.style.display="inline-block";
  94. log1.innerHTML="";
  95. log2.style.display="inline-block";
  96. log2.innerHTML="";
  97. }
  98.  
  99. /*
  100. * Validating start bit, end bit, segment(chunk size)
  101. */
  102. function validate()
  103. {
  104. var output1 = true;
  105. //For starting bit
  106. if(startbit == '')
  107. {
  108. log.innerHTML+="n Beginning bit off-set cannot not be emptyn";
  109. output1 = false;
  110. }
  111. else if(isNaN(startbit) || startbit < 0 )
  112. {
  113. log.innerHTML+="n Beginning bit off-set should be a number and should be 0 greater than 0 n";
  114. output1 = false;
  115. }
  116. else if(startbit>filesize)
  117. {
  118. log.innerHTML+="n Beginning bit off-set should less than file size ("+filesize+") n";
  119. output1 = false;
  120. }
  121.  
  122. //For Ending bit
  123. if(lastbit == '')
  124. {
  125. log.innerHTML+="n Last bit off-set cannot not be emptyn";
  126. output1 = false;
  127. }
  128. else if(isNaN(lastbit) || lastbit < 1 )
  129. {
  130. log.innerHTML+="n Last bit off-set should be a number and should be greater than 0 n";
  131. output1 = false;
  132. }
  133. else if(lastbit>filesize)
  134. {
  135. log.innerHTML+="n Last bit off-set should less than file size ("+filesize+") n";
  136. output1 = false;
  137. }
  138.  
  139. //For segment
  140. if(chunkSize == '')
  141. {
  142. log.innerHTML+="n Segment Size cannot not be emptyn";
  143. output1 = false;
  144. }
  145. else if(isNaN(chunkSize) || chunkSize < 1 )
  146. {
  147. log.innerHTML+="n Segment Size should be a number and should be greater than 0 n";
  148. output1 = false;
  149. }
  150. else if(chunkSize>filesize)
  151. {
  152. log.innerHTML+="n Segment Size should less than file size ("+filesize+") n";
  153. output1 = false;
  154. }
  155. else if((chunkSize%8) != 0)
  156. {
  157. log.innerHTML+="n Segment Size should be a mulitple of 8 n";
  158. output1 = false;
  159. }
  160. return output1;
  161. }
  162.  
  163. /*
  164. * Getting file Details on file select and setting default to input field
  165. */
  166. $(document).on('change','#file',function ()
  167. {
  168. file = this.files[0];
  169. chunkSize = 1048576*8; //in bytes read in chunks of 1MB
  170. chunks = Math.ceil(file.size / chunkSize);
  171. lastbit = (file.size*8);
  172. filesize = lastbit;
  173. $('#start').val(startbit);
  174. $('#lastbit').val(lastbit);
  175. $('#chunk').val(chunkSize);
  176. clearlog();
  177. });
  178.  
  179.  
  180. $("#calculate").click(function()
  181. {
  182.  
  183. clearlog();
  184. //Fetching value from input
  185. startbit = $('#start').val();
  186. lastbit = $('#lastbit').val();
  187. chunkSize = $('#chunk').val();
  188. type = $('#hashtype').val();
  189. if(file == '') //check for file is selected or not
  190. {
  191. log.innerHTML+="n Please Select file to proceed n";
  192. return false;
  193. }
  194. //validate for error if it does not rteurn error it will proceed else shows error
  195. if(validate())
  196. {
  197. //Conversion in bytes
  198. filesize = lastbit-startbit;
  199. var startbit1 = Math.ceil(startbit/8);
  200. var chunkSize1 = Math.ceil(chunkSize/8);
  201. var filesize1 = Math.ceil(filesize/8);
  202. chunks = Math.ceil(filesize1 / chunkSize1);
  203. //padding length of 0 for segment no
  204. var padd_length = parseInt((chunks+'').length);
  205. //Last segment size
  206. var last_segment = ((filesize1)%chunkSize1)*8;
  207. if(last_segment == 0) last_segment = (chunkSize1*8);
  208.  
  209. if(type == 'SHA1') {var hash1 = new asmCrypto.SHA1; var hash2 = new asmCrypto.SHA1;}
  210. else if(type == 'SHA256') {var hash1 = new asmCrypto.SHA256; var hash2 = new asmCrypto.SHA256;}
  211. else {var hash1 = new SparkMD5.ArrayBuffer();var hash2 = new SparkMD5.ArrayBuffer();}
  212. //using bobslice to partion the file and process it
  213. var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
  214. currentChunk = 0,
  215. myChunk = 0,
  216. dummydata = new Array(),
  217. output1 = '', //Store hash value and display when called for column1
  218. output2 = '', //Store hash value and display when called for column2
  219. frOnload = function(e) //for column1
  220. {
  221. dummydata.push(e.target.result);
  222. if(type == 'MD5')
  223. {
  224. hash1.append(e.target.result); // append array buffer for collumn one
  225. $.each(dummydata, function (index, value) {
  226. hash2.append(value); // append array buffer for collumn two
  227. });
  228. output1 = hash1.end();
  229. output2 = hash2.end();
  230.  
  231. }
  232. else
  233. {
  234. var arr1 ='';
  235. var arr2 = '';
  236. hash1.reset();
  237. hash2.reset();
  238. hash1.process(e.target.result); // append array buffer for collumn one
  239. $.each(dummydata, function (index, value) {
  240. hash2.process(value); //append array buffer for collumn two
  241. });
  242. //Getting output in decimal
  243. arr1 = hash1.finish().result;
  244. arr2 = hash2.finish().result;
  245. output1 = '';
  246. output2 = '';
  247.  
  248. //converting output in hexadecimal
  249. $.each(arr1, function (index, value) {
  250. value = value.toString(16);
  251. if(value.length == 1)
  252. {
  253. value = '0'+value;
  254. }
  255. output1 +=value;
  256. });
  257. $.each(arr2, function (index, value) {
  258. value = value.toString(16);
  259. if(value.length == 1)
  260. {
  261. value = '0'+value;
  262. }
  263. output2 +=value;
  264. });
  265. }
  266. //condition to process till the chunk is not completed
  267. currentChunk++;
  268. if(currentChunk <= chunks)
  269. {
  270. loadNext();
  271. }
  272. },
  273.  
  274. frOnerror = function () {
  275. log.innerHTML+="n oops, something went wrong.";
  276. };
  277. function loadNext()
  278. {
  279. //for column1
  280. var fileReader = new FileReader();
  281. fileReader.onload = frOnload;
  282. fileReader.onerror = frOnerror;
  283. var start = startbit1+(currentChunk * chunkSize1),
  284. end = ((start + chunkSize1) >= filesize1) ? filesize1 : start + chunkSize1;
  285.  
  286. //call to frOnload to get hash value for column1
  287. fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
  288.  
  289. //print segemwnt with hash value on each line
  290. if(currentChunk !=0)
  291. {
  292. log2.innerHTML+=" Segement Hash #"+leftPad(parseInt(currentChunk),padd_length)+":"+output1+" &nbsp&nbsp;"+output2+"n";
  293. percent = Math.round((currentChunk/chunks)*100); //transform to near integer value 0.1-0.4 as 0% and 0.5-0.9 as 1%
  294. log1.innerHTML = " Total: "+leftPad(parseInt(percent),3)+"%n";
  295. }
  296. };
  297.  
  298. //Printing Details
  299. log.innerHTML = "n Filename: "+file.name+" n Hash: "+type+" n Segment Length: "+chunkSize+" n Segment Count:"+chunks+" n Last Segment Length: "+last_segment;
  300. log1.innerHTML = " Total: "+leftPad(parseInt(percent),3)+"%n"; //initial percent to 0
  301. //
  302. loadNext();
  303. }
  304. return false;
  305. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement