blobhasher.html 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2011 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <title>goog.crypt.BlobHasher</title>
  10. <script src="../base.js"></script>
  11. <script>
  12. goog.require('goog.crypt');
  13. goog.require('goog.crypt.BlobHasher');
  14. goog.require('goog.crypt.Md5');
  15. </script>
  16. <link rel="stylesheet" href="css/demo.css">
  17. </head>
  18. <body>
  19. <h1>goog.crypt.BlobHasher</h1>
  20. <table>
  21. <tr><td>File:</td><td>
  22. <input type="file" onchange="computeMD5(this.files[0]);">
  23. <input type="button" value="Abort" onclick="abort();">
  24. </td></tr>
  25. <tr><td>MD5:</td><td><div id="output" style="font-family:courier new,fixed" /></td></tr>
  26. </table>
  27. <script>
  28. var hashFn = new goog.crypt.Md5();
  29. var blobHasher = new goog.crypt.BlobHasher(hashFn);
  30. var startTime = 0;
  31. function computeMD5(file) {
  32. goog.events.listen(blobHasher, goog.crypt.BlobHasher.EventType.COMPLETE,
  33. function() {
  34. var hash = goog.crypt.byteArrayToHex(blobHasher.getHash());
  35. var time = goog.now() - startTime;
  36. display(hash + ' (' + time/1000 + 's)');
  37. });
  38. goog.events.listen(blobHasher, goog.crypt.BlobHasher.EventType.ABORT,
  39. function() {
  40. display('Aborted');
  41. });
  42. display('Computing...');
  43. startTime = goog.now();
  44. blobHasher.hash(file);
  45. }
  46. function abort() {
  47. blobHasher.abort();
  48. }
  49. function display(message) {
  50. document.getElementById('output').innerHTML = message;
  51. }
  52. </script>
  53. </body>
  54. </html>