home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / firefox / cryptohasher.js < prev    next >
Text File  |  2006-08-07  |  3KB  |  81 lines

  1. function G_CryptoHasher() {
  2. this.debugZone = "cryptohasher";
  3. this.decoder_ = new G_Base64();
  4. this.hasher_ = Cc["@mozilla.org/security/hash;1"]
  5. .createInstance(Ci.nsICryptoHash);
  6. this.initialized_ = false;
  7. }
  8. G_CryptoHasher.algorithms = {
  9. MD2: Ci.nsICryptoHash.MD2,
  10. MD5: Ci.nsICryptoHash.MD5,
  11. SHA1: Ci.nsICryptoHash.SHA1,
  12. SHA256: Ci.nsICryptoHash.SHA256,
  13. SHA384: Ci.nsICryptoHash.SHA384,
  14. SHA512: Ci.nsICryptoHash.SHA512,
  15. };
  16. G_CryptoHasher.prototype.init = function(algorithm) {
  17. var validAlgorithm = false;
  18. for (var alg in G_CryptoHasher.algorithms)
  19. if (algorithm == G_CryptoHasher.algorithms[alg])
  20. validAlgorithm = true;
  21. if (!validAlgorithm)
  22. throw new Error("Invalid algorithm: " + algorithm);
  23. this.initialized_ = true;
  24. this.hasher_.init(algorithm);
  25. }
  26. G_CryptoHasher.prototype.updateFromString = function(input) {
  27. if (!this.initialized_)
  28. throw new Error("You must initialize the hasher first!");
  29. this.hasher_.update(this.decoder_.arrayifyString(input), input.length);
  30. }
  31. G_CryptoHasher.prototype.updateFromArray = function(input) {
  32. if (!this.initialized_)
  33. throw new Error("You must initialize the hasher first!");
  34. this.hasher_.update(input, input.length);
  35. }
  36. G_CryptoHasher.prototype.updateFromStream = function(stream) {
  37. if (!this.initialized_)
  38. throw new Error("You must initialize the hasher first!");
  39. this.hasher_.updateFromStream(stream, stream.available());
  40. }
  41. G_CryptoHasher.prototype.digestRaw = function() {
  42. return this.hasher_.finish(false /* not b64 encoded */);
  43. }
  44. G_CryptoHasher.prototype.digestBase64 = function() {
  45. return this.hasher_.finish(true /* b64 encoded */);
  46. }
  47. G_CryptoHasher.prototype.digestHex = function() {
  48. var raw = this.digestRaw();
  49. return this.toHex_(raw);
  50. }
  51. G_CryptoHasher.prototype.toHex_ = function(str) {
  52. var hexchars = '0123456789ABCDEF';
  53. var hexrep = new Array(str.length * 2);
  54. for (var i = 0; i < str.length; ++i) {
  55. hexrep[i * 2] = hexchars.charAt((str.charCodeAt(i) >> 4) & 15);
  56. hexrep[i * 2 + 1] = hexchars.charAt(str.charCodeAt(i) & 15);
  57. }
  58. return hexrep.join('');
  59. }
  60. function TEST_G_CryptoHasher() {
  61. if (G_GDEBUG) {
  62. var z = "cryptohasher UNITTEST";
  63. G_debugService.enableZone(z);
  64. G_Debug(z, "Starting");
  65. var md5 = function(str) {
  66. var hasher = new G_CryptoHasher();
  67. hasher.init(G_CryptoHasher.algorithms.MD5);
  68. hasher.updateFromString(str);
  69. return hasher.digestHex().toLowerCase();
  70. };
  71. var vectors = {"": "d41d8cd98f00b204e9800998ecf8427e",
  72. "a": "0cc175b9c0f1b6a831c399e269772661",
  73. "abc": "900150983cd24fb0d6963f7d28e17f72",
  74. "message digest": "f96b697d7cb7938d525a2f31aaf161d0",
  75. "abcdefghijklmnopqrstuvwxyz": "c3fcd3d76192e4007dfb496cca67e13b",
  76. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789": "d174ab98d277d9f5a5611c2c9f419d9f",
  77. "12345678901234567890123456789012345678901234567890123456789012345678901234567890": "57edf4a22be3c955ac49da2e2107b67a"};
  78. G_Debug(z, "PASSED");
  79. }
  80. }
  81.