Sha1.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. export class Sha1 {
  2. private blockstart: any;
  3. private W = new Array(80);
  4. private H0 = 0x67452301;
  5. private H1 = 0xEFCDAB89;
  6. private H2 = 0x98BADCFE;
  7. private H3 = 0x10325476;
  8. private H4 = 0xC3D2E1F0;
  9. private A;
  10. private B;
  11. private C;
  12. private D;
  13. private E;
  14. private temp;
  15. public msg;
  16. private msg_len;
  17. private word_array;
  18. /*msg :要传入验证的的 sha1字符串*/
  19. constructor(msg) {
  20. this.word_array = new Array();
  21. this.msg = this.Utf8Encode(msg);
  22. this.msg_len = this.msg.length;
  23. for (let i = 0; i < this.msg_len - 3; i += 4) {
  24. let j = this.msg.charCodeAt(i) << 24 | this.msg.charCodeAt(i + 1) << 16 |
  25. this.msg.charCodeAt(i + 2) << 8 | this.msg.charCodeAt(i + 3);
  26. this.word_array.push(j);
  27. }
  28. this.initMsg(this.msg_len);
  29. }
  30. public initMsg(msg_len) {
  31. let i;
  32. switch (msg_len % 4) {
  33. case 0:
  34. i = 0x080000000;
  35. break;
  36. case 1:
  37. i = this.msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  38. break;
  39. case 2:
  40. i = this.msg.charCodeAt(msg_len - 2) << 24 | this.msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  41. break;
  42. case 3:
  43. i = this.msg.charCodeAt(msg_len - 3) << 24 | this.msg.charCodeAt(msg_len - 2) << 16 | this.msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  44. break;
  45. }
  46. this.word_array.push(i);
  47. while ((this.word_array.length % 16) != 14) this.word_array.push(0);
  48. this.word_array.push(msg_len >>> 29);
  49. this.word_array.push((msg_len << 3) & 0x0ffffffff);
  50. this.getTemp();
  51. }
  52. public getTemp() {
  53. for (this.blockstart = 0; this.blockstart < this.word_array.length; this.blockstart += 16) {
  54. for (let i = 0; i < 16; i++) this.W[i] = this.word_array[this.blockstart + i];
  55. for (let i = 16; i <= 79; i++) this.W[i] = this.rotate_left(this.W[i - 3] ^ this.W[i - 8] ^ this.W[i - 14] ^ this.W[i - 16], 1);
  56. this.A = this.H0;
  57. this.B = this.H1;
  58. this.C = this.H2;
  59. this.D = this.H3;
  60. this.E = this.H4;
  61. for (let i = 0; i <= 19; i++) {
  62. this.temp = (this.rotate_left(this.A, 5) + ((this.B & this.C) | (~this.B & this.D)) + this.E + this.W[i] + 0x5A827999) & 0x0ffffffff;
  63. this.E = this.D;
  64. this.D = this.C;
  65. this.C = this.rotate_left(this.B, 30);
  66. this.B = this.A;
  67. this.A = this.temp;
  68. }
  69. for (let i = 20; i <= 39; i++) {
  70. this.temp = (this.rotate_left(this.A, 5) + (this.B ^ this.C ^ this.D) + this.E + this.W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  71. this.E = this.D;
  72. this.D = this.C;
  73. this.C = this.rotate_left(this.B, 30);
  74. this.B = this.A;
  75. this.A = this.temp;
  76. }
  77. for (let i = 40; i <= 59; i++) {
  78. this.temp = (this.rotate_left(this.A, 5) + ((this.B & this.C) | (this.B & this.D) | (this.C & this.D)) + this.E + this.W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  79. this.E = this.D;
  80. this.D = this.C;
  81. this.C = this.rotate_left(this.B, 30);
  82. this.B = this.A;
  83. this.A = this.temp;
  84. }
  85. for (let i = 60; i <= 79; i++) {
  86. this.temp = (this.rotate_left(this.A, 5) + (this.B ^ this.C ^ this.D) + this.E + this.W[i] + 0xCA62C1D6) & 0x0ffffffff;
  87. this.E = this.D;
  88. this.D = this.C;
  89. this.C = this.rotate_left(this.B, 30);
  90. this.B = this.A;
  91. this.A = this.temp;
  92. }
  93. this.H0 = (this.H0 + this.A) & 0x0ffffffff;
  94. this.H1 = (this.H1 + this.B) & 0x0ffffffff;
  95. this.H2 = (this.H2 + this.C) & 0x0ffffffff;
  96. this.H3 = (this.H3 + this.D) & 0x0ffffffff;
  97. this.H4 = (this.H4 + this.E) & 0x0ffffffff;
  98. }
  99. }
  100. /**获取sha1加密字符串*/
  101. public hex_sha1() {
  102. let temp = this.cvt_hex(this.H0) + this.cvt_hex(this.H1) + this.cvt_hex(this.H2) + this.cvt_hex(this.H3) + this.cvt_hex(this.H4);
  103. return temp.toLowerCase();
  104. }
  105. private rotate_left(n, s) {
  106. var t4 = (n << s) | (n >>> (32 - s));
  107. return t4;
  108. };
  109. private lsb_hex(val) {
  110. var str = "";
  111. var i;
  112. var vh;
  113. var vl;
  114. for (i = 0; i <= 6; i += 2) {
  115. vh = (val >>> (i * 4 + 4)) & 0x0f;
  116. vl = (val >>> (i * 4)) & 0x0f;
  117. str += vh.toString(16) + vl.toString(16);
  118. }
  119. return str;
  120. };
  121. private cvt_hex(val) {
  122. var str = "";
  123. var i;
  124. var v;
  125. for (i = 7; i >= 0; i--) {
  126. v = (val >>> (i * 4)) & 0x0f;
  127. str += v.toString(16);
  128. }
  129. return str;
  130. };
  131. private Utf8Encode(string) {
  132. string = string.replace(/\r\n/g, "\n");
  133. var utftext = "";
  134. for (var n = 0; n < string.length; n++) {
  135. var c = string.charCodeAt(n);
  136. if (c < 128) {
  137. utftext += String.fromCharCode(c);
  138. }
  139. else if ((c > 127) && (c < 2048)) {
  140. utftext += String.fromCharCode((c >> 6) | 192);
  141. utftext += String.fromCharCode((c & 63) | 128);
  142. }
  143. else {
  144. utftext += String.fromCharCode((c >> 12) | 224);
  145. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  146. utftext += String.fromCharCode((c & 63) | 128);
  147. }
  148. }
  149. return utftext;
  150. };
  151. }