Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

sha1-browser.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Adapted from Chris Veness' SHA1 code at
  2. // http://www.movable-type.co.uk/scripts/sha1.html
  3. 'use strict';
  4. function f(s, x, y, z) {
  5. switch (s) {
  6. case 0: return (x & y) ^ (~x & z);
  7. case 1: return x ^ y ^ z;
  8. case 2: return (x & y) ^ (x & z) ^ (y & z);
  9. case 3: return x ^ y ^ z;
  10. }
  11. }
  12. function ROTL(x, n) {
  13. return (x << n) | (x>>> (32 - n));
  14. }
  15. function sha1(bytes) {
  16. var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];
  17. var H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
  18. if (typeof(bytes) == 'string') {
  19. var msg = unescape(encodeURIComponent(bytes)); // UTF8 escape
  20. bytes = new Array(msg.length);
  21. for (var i = 0; i < msg.length; i++) bytes[i] = msg.charCodeAt(i);
  22. }
  23. bytes.push(0x80);
  24. var l = bytes.length/4 + 2;
  25. var N = Math.ceil(l/16);
  26. var M = new Array(N);
  27. for (var i=0; i<N; i++) {
  28. M[i] = new Array(16);
  29. for (var j=0; j<16; j++) {
  30. M[i][j] =
  31. bytes[i * 64 + j * 4] << 24 |
  32. bytes[i * 64 + j * 4 + 1] << 16 |
  33. bytes[i * 64 + j * 4 + 2] << 8 |
  34. bytes[i * 64 + j * 4 + 3];
  35. }
  36. }
  37. M[N - 1][14] = ((bytes.length - 1) * 8) /
  38. Math.pow(2, 32); M[N - 1][14] = Math.floor(M[N - 1][14]);
  39. M[N - 1][15] = ((bytes.length - 1) * 8) & 0xffffffff;
  40. for (var i=0; i<N; i++) {
  41. var W = new Array(80);
  42. for (var t=0; t<16; t++) W[t] = M[i][t];
  43. for (var t=16; t<80; t++) {
  44. W[t] = ROTL(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
  45. }
  46. var a = H[0];
  47. var b = H[1];
  48. var c = H[2];
  49. var d = H[3];
  50. var e = H[4];
  51. for (var t=0; t<80; t++) {
  52. var s = Math.floor(t/20);
  53. var T = ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t] >>> 0;
  54. e = d;
  55. d = c;
  56. c = ROTL(b, 30) >>> 0;
  57. b = a;
  58. a = T;
  59. }
  60. H[0] = (H[0] + a) >>> 0;
  61. H[1] = (H[1] + b) >>> 0;
  62. H[2] = (H[2] + c) >>> 0;
  63. H[3] = (H[3] + d) >>> 0;
  64. H[4] = (H[4] + e) >>> 0;
  65. }
  66. return [
  67. H[0] >> 24 & 0xff, H[0] >> 16 & 0xff, H[0] >> 8 & 0xff, H[0] & 0xff,
  68. H[1] >> 24 & 0xff, H[1] >> 16 & 0xff, H[1] >> 8 & 0xff, H[1] & 0xff,
  69. H[2] >> 24 & 0xff, H[2] >> 16 & 0xff, H[2] >> 8 & 0xff, H[2] & 0xff,
  70. H[3] >> 24 & 0xff, H[3] >> 16 & 0xff, H[3] >> 8 & 0xff, H[3] & 0xff,
  71. H[4] >> 24 & 0xff, H[4] >> 16 & 0xff, H[4] >> 8 & 0xff, H[4] & 0xff
  72. ];
  73. }
  74. module.exports = sha1;