汇联通执法队后台管理系统
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

sha-1.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. var hexcase = 0; /**//* hex output format. 0 - lowercase; 1 - uppercase */
  2. var chrsz = 8; /**//* bits per input character. 8 - ASCII; 16 - Unicode */
  3. /**//*
  4. * The main function to calculate message digest
  5. */
  6. function hex_sha1(s)
  7. {
  8. return binb2hex(core_sha1(AlignSHA1(s)));
  9. }
  10. /**//*
  11. * Perform a simple self-test to see if the VM is working
  12. */
  13. function sha1_vm_test()
  14. {
  15. return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
  16. }
  17. /**//*
  18. * Calculate the SHA-1 of an array of big-endian words, and a bit length
  19. */
  20. function core_sha1(blockArray)
  21. {
  22. var x = blockArray; //append padding
  23. var w = Array(80);
  24. var a = 1732584193;
  25. var b = -271733879;
  26. var c = -1732584194;
  27. var d = 271733878;
  28. var e = -1009589776;
  29. for(var i = 0; i < x.length; i += 16) //每次处理512位 16*32
  30. {
  31. var olda = a;
  32. var oldb = b;
  33. var oldc = c;
  34. var oldd = d;
  35. var olde = e;
  36. for(var j = 0; j < 80; j++) //对每个512位进行80步操作
  37. {
  38. if(j < 16) w[j] = x[i + j];
  39. else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
  40. var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
  41. safe_add(safe_add(e, w[j]), sha1_kt(j)));
  42. e = d;
  43. d = c;
  44. c = rol(b, 30);
  45. b = a;
  46. a = t;
  47. }
  48. a = safe_add(a, olda);
  49. b = safe_add(b, oldb);
  50. c = safe_add(c, oldc);
  51. d = safe_add(d, oldd);
  52. e = safe_add(e, olde);
  53. }
  54. return new Array(a, b, c, d, e);
  55. }
  56. /**//*
  57. * Perform the appropriate triplet combination function for the current iteration
  58. * 返回对应F函数的值
  59. */
  60. function sha1_ft(t, b, c, d)
  61. {
  62. if(t < 20) return (b & c) | ((~b) & d);
  63. if(t < 40) return b ^ c ^ d;
  64. if(t < 60) return (b & c) | (b & d) | (c & d);
  65. return b ^ c ^ d; //t<80
  66. }
  67. /**//*
  68. * Determine the appropriate additive constant for the current iteration
  69. * 返回对应的Kt值
  70. */
  71. function sha1_kt(t)
  72. {
  73. return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
  74. (t < 60) ? -1894007588 : -899497514;
  75. }
  76. /**//*
  77. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  78. * to work around bugs in some JS interpreters.
  79. * 将32位数拆成高16位和低16位分别进行相加,从而实现 MOD 2^32 的加法
  80. */
  81. function safe_add(x, y)
  82. {
  83. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  84. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  85. return (msw << 16) | (lsw & 0xFFFF);
  86. }
  87. /**//*
  88. * Bitwise rotate a 32-bit number to the left.
  89. * 32位二进制数循环左移
  90. */
  91. function rol(num, cnt)
  92. {
  93. return (num << cnt) | (num >>> (32 - cnt));
  94. }
  95. /**//*
  96. * The standard SHA1 needs the input string to fit into a block
  97. * This function align the input string to meet the requirement
  98. */
  99. function AlignSHA1(str){
  100. var nblk=((str.length+8)>>6)+1, blks=new Array(nblk*16);
  101. for(var i=0;i<nblk*16;i++)blks[i]=0;
  102. for(i=0;i<str.length;i++)
  103. blks[i>>2]|=str.charCodeAt(i)<<(24-(i&3)*8);
  104. blks[i>>2]|=0x80<<(24-(i&3)*8);
  105. blks[nblk*16-1]=str.length*8;
  106. return blks;
  107. }
  108. /**//*
  109. * Convert an array of big-endian words to a hex string.
  110. */
  111. function binb2hex(binarray)
  112. {
  113. var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  114. var str = "";
  115. for(var i = 0; i < binarray.length * 4; i++)
  116. {
  117. str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
  118. hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
  119. }
  120. return str;
  121. }