/_octopress/source/functions/sha1/index.markdown

https://gitlab.com/orvi2014/phpjs · Markdown · 180 lines · 153 code · 27 blank · 0 comment · 0 complexity · 38e75515162c59878a74ee14749cc95e MD5 · raw file

  1. ---
  2. layout: page
  3. title: "JavaScript sha1 function"
  4. comments: true
  5. sharing: true
  6. footer: true
  7. alias:
  8. - /functions/view/sha1:512
  9. - /functions/view/sha1
  10. - /functions/view/512
  11. - /functions/sha1:512
  12. - /functions/512
  13. ---
  14. <!-- Generated by Rakefile:build -->
  15. A JavaScript equivalent of PHP's sha1
  16. {% codeblock strings/sha1.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/sha1.js raw on github %}
  17. function sha1(str) {
  18. // discuss at: http://phpjs.org/functions/sha1/
  19. // original by: Webtoolkit.info (http://www.webtoolkit.info/)
  20. // improved by: Michael White (http://getsprink.com)
  21. // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  22. // input by: Brett Zamir (http://brett-zamir.me)
  23. // depends on: utf8_encode
  24. // example 1: sha1('Kevin van Zonneveld');
  25. // returns 1: '54916d2e62f65b3afa6e192e6a601cdbe5cb5897'
  26. var rotate_left = function(n, s) {
  27. var t4 = (n << s) | (n >>> (32 - s));
  28. return t4;
  29. };
  30. /*var lsb_hex = function (val) { // Not in use; needed?
  31. var str="";
  32. var i;
  33. var vh;
  34. var vl;
  35. for ( i=0; i<=6; i+=2 ) {
  36. vh = (val>>>(i*4+4))&0x0f;
  37. vl = (val>>>(i*4))&0x0f;
  38. str += vh.toString(16) + vl.toString(16);
  39. }
  40. return str;
  41. };*/
  42. var cvt_hex = function(val) {
  43. var str = '';
  44. var i;
  45. var v;
  46. for (i = 7; i >= 0; i--) {
  47. v = (val >>> (i * 4)) & 0x0f;
  48. str += v.toString(16);
  49. }
  50. return str;
  51. };
  52. var blockstart;
  53. var i, j;
  54. var W = new Array(80);
  55. var H0 = 0x67452301;
  56. var H1 = 0xEFCDAB89;
  57. var H2 = 0x98BADCFE;
  58. var H3 = 0x10325476;
  59. var H4 = 0xC3D2E1F0;
  60. var A, B, C, D, E;
  61. var temp;
  62. str = this.utf8_encode(str);
  63. var str_len = str.length;
  64. var word_array = [];
  65. for (i = 0; i < str_len - 3; i += 4) {
  66. j = str.charCodeAt(i) << 24 | str.charCodeAt(i + 1) << 16 | str.charCodeAt(i + 2) << 8 | str.charCodeAt(i + 3);
  67. word_array.push(j);
  68. }
  69. switch (str_len % 4) {
  70. case 0:
  71. i = 0x080000000;
  72. break;
  73. case 1:
  74. i = str.charCodeAt(str_len - 1) << 24 | 0x0800000;
  75. break;
  76. case 2:
  77. i = str.charCodeAt(str_len - 2) << 24 | str.charCodeAt(str_len - 1) << 16 | 0x08000;
  78. break;
  79. case 3:
  80. i = str.charCodeAt(str_len - 3) << 24 | str.charCodeAt(str_len - 2) << 16 | str.charCodeAt(str_len - 1) <<
  81. 8 | 0x80;
  82. break;
  83. }
  84. word_array.push(i);
  85. while ((word_array.length % 16) != 14) {
  86. word_array.push(0);
  87. }
  88. word_array.push(str_len >>> 29);
  89. word_array.push((str_len << 3) & 0x0ffffffff);
  90. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  91. for (i = 0; i < 16; i++) {
  92. W[i] = word_array[blockstart + i];
  93. }
  94. for (i = 16; i <= 79; i++) {
  95. W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  96. }
  97. A = H0;
  98. B = H1;
  99. C = H2;
  100. D = H3;
  101. E = H4;
  102. for (i = 0; i <= 19; i++) {
  103. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  104. E = D;
  105. D = C;
  106. C = rotate_left(B, 30);
  107. B = A;
  108. A = temp;
  109. }
  110. for (i = 20; i <= 39; i++) {
  111. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  112. E = D;
  113. D = C;
  114. C = rotate_left(B, 30);
  115. B = A;
  116. A = temp;
  117. }
  118. for (i = 40; i <= 59; i++) {
  119. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  120. E = D;
  121. D = C;
  122. C = rotate_left(B, 30);
  123. B = A;
  124. A = temp;
  125. }
  126. for (i = 60; i <= 79; i++) {
  127. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  128. E = D;
  129. D = C;
  130. C = rotate_left(B, 30);
  131. B = A;
  132. A = temp;
  133. }
  134. H0 = (H0 + A) & 0x0ffffffff;
  135. H1 = (H1 + B) & 0x0ffffffff;
  136. H2 = (H2 + C) & 0x0ffffffff;
  137. H3 = (H3 + D) & 0x0ffffffff;
  138. H4 = (H4 + E) & 0x0ffffffff;
  139. }
  140. temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  141. return temp.toLowerCase();
  142. }
  143. {% endcodeblock %}
  144. - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/sha1.js)
  145. Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
  146. the closest match to this hashtable-like data structure.
  147. Please also note that php.js offers community built functions and goes by the
  148. [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
  149. functions that are far from perfect, in the hopes to spark better contributions.
  150. Do you have one? Then please just:
  151. - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/strings/sha1.js)
  152. ### Other PHP functions in the strings extension
  153. {% render_partial _includes/custom/strings.html %}