PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/website/source/php/strings/md5.html

http://github.com/kvz/phpjs
HTML | 285 lines | 265 code | 20 blank | 0 comment | 0 complexity | 52143ec768899793e3d847c6734bfb31 MD5 | raw file
  1. ---
  2. warning: 'This file is auto generated by `npm run web:inject`, do not edit by hand'
  3. examples:
  4. - md5('Kevin van Zonneveld')
  5. returns:
  6. - '''6e658d4bfcb59cc13f96c14450ac40b9'''
  7. dependencies: []
  8. authors:
  9. original by:
  10. - 'Webtoolkit.info (https://www.webtoolkit.info/)'
  11. improved by:
  12. - 'Michael White (https://getsprink.com)'
  13. - Jack
  14. - 'Kevin van Zonneveld (https://kvz.io)'
  15. bugfixed by:
  16. - 'Kevin van Zonneveld (https://kvz.io)'
  17. input by:
  18. - 'Brett Zamir (https://brett-zamir.me)'
  19. notes:
  20. - >-
  21. Keep in mind that in accordance with PHP, the whole string is buffered and
  22. then
  23. hashed. If available, we'd recommend using Node's native crypto modules
  24. directly
  25. in a steaming fashion for faster and more efficient hashing
  26. type: function
  27. layout: function
  28. title: PHP's md5 in JavaScript
  29. description: >-
  30. Heres what our current JavaScript equivalent to <a
  31. href="https://php.net/manual/en/function.md5.php">PHP's md5</a> looks like.
  32. function: md5
  33. category: strings
  34. language: php
  35. permalink: php/strings/md5/
  36. alias:
  37. - /functions/php/md5/
  38. - /functions/strings/md5/
  39. - /php/md5/
  40. - /functions/md5/
  41. ---
  42. {% codeblock lang:javascript %}module.exports = function md5 (str) {
  43. // discuss at: https://locutus.io/php/md5/
  44. // original by: Webtoolkit.info (https://www.webtoolkit.info/)
  45. // improved by: Michael White (https://getsprink.com)
  46. // improved by: Jack
  47. // improved by: Kevin van Zonneveld (https://kvz.io)
  48. // input by: Brett Zamir (https://brett-zamir.me)
  49. // bugfixed by: Kevin van Zonneveld (https://kvz.io)
  50. // note 1: Keep in mind that in accordance with PHP, the whole string is buffered and then
  51. // note 1: hashed. If available, we'd recommend using Node's native crypto modules directly
  52. // note 1: in a steaming fashion for faster and more efficient hashing
  53. // example 1: md5('Kevin van Zonneveld')
  54. // returns 1: '6e658d4bfcb59cc13f96c14450ac40b9'
  55. let hash
  56. try {
  57. const crypto = require('crypto')
  58. const md5sum = crypto.createHash('md5')
  59. md5sum.update(str)
  60. hash = md5sum.digest('hex')
  61. } catch (e) {
  62. hash = undefined
  63. }
  64. if (hash !== undefined) {
  65. return hash
  66. }
  67. const utf8Encode = require('../xml/utf8_encode')
  68. let xl
  69. const _rotateLeft = function (lValue, iShiftBits) {
  70. return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits))
  71. }
  72. const _addUnsigned = function (lX, lY) {
  73. let lX4, lY4, lX8, lY8, lResult
  74. lX8 = (lX & 0x80000000)
  75. lY8 = (lY & 0x80000000)
  76. lX4 = (lX & 0x40000000)
  77. lY4 = (lY & 0x40000000)
  78. lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF)
  79. if (lX4 & lY4) {
  80. return (lResult ^ 0x80000000 ^ lX8 ^ lY8)
  81. }
  82. if (lX4 | lY4) {
  83. if (lResult & 0x40000000) {
  84. return (lResult ^ 0xC0000000 ^ lX8 ^ lY8)
  85. } else {
  86. return (lResult ^ 0x40000000 ^ lX8 ^ lY8)
  87. }
  88. } else {
  89. return (lResult ^ lX8 ^ lY8)
  90. }
  91. }
  92. const _F = function (x, y, z) {
  93. return (x & y) | ((~x) & z)
  94. }
  95. const _G = function (x, y, z) {
  96. return (x & z) | (y & (~z))
  97. }
  98. const _H = function (x, y, z) {
  99. return (x ^ y ^ z)
  100. }
  101. const _I = function (x, y, z) {
  102. return (y ^ (x | (~z)))
  103. }
  104. const _FF = function (a, b, c, d, x, s, ac) {
  105. a = _addUnsigned(a, _addUnsigned(_addUnsigned(_F(b, c, d), x), ac))
  106. return _addUnsigned(_rotateLeft(a, s), b)
  107. }
  108. const _GG = function (a, b, c, d, x, s, ac) {
  109. a = _addUnsigned(a, _addUnsigned(_addUnsigned(_G(b, c, d), x), ac))
  110. return _addUnsigned(_rotateLeft(a, s), b)
  111. }
  112. const _HH = function (a, b, c, d, x, s, ac) {
  113. a = _addUnsigned(a, _addUnsigned(_addUnsigned(_H(b, c, d), x), ac))
  114. return _addUnsigned(_rotateLeft(a, s), b)
  115. }
  116. const _II = function (a, b, c, d, x, s, ac) {
  117. a = _addUnsigned(a, _addUnsigned(_addUnsigned(_I(b, c, d), x), ac))
  118. return _addUnsigned(_rotateLeft(a, s), b)
  119. }
  120. const _convertToWordArray = function (str) {
  121. let lWordCount
  122. const lMessageLength = str.length
  123. const lNumberOfWordsTemp1 = lMessageLength + 8
  124. const lNumberOfWordsTemp2 = (lNumberOfWordsTemp1 - (lNumberOfWordsTemp1 % 64)) / 64
  125. const lNumberOfWords = (lNumberOfWordsTemp2 + 1) * 16
  126. const lWordArray = new Array(lNumberOfWords - 1)
  127. let lBytePosition = 0
  128. let lByteCount = 0
  129. while (lByteCount < lMessageLength) {
  130. lWordCount = (lByteCount - (lByteCount % 4)) / 4
  131. lBytePosition = (lByteCount % 4) * 8
  132. lWordArray[lWordCount] = (lWordArray[lWordCount] |
  133. (str.charCodeAt(lByteCount) << lBytePosition))
  134. lByteCount++
  135. }
  136. lWordCount = (lByteCount - (lByteCount % 4)) / 4
  137. lBytePosition = (lByteCount % 4) * 8
  138. lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition)
  139. lWordArray[lNumberOfWords - 2] = lMessageLength << 3
  140. lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29
  141. return lWordArray
  142. }
  143. const _wordToHex = function (lValue) {
  144. let wordToHexValue = ''
  145. let wordToHexValueTemp = ''
  146. let lByte
  147. let lCount
  148. for (lCount = 0; lCount <= 3; lCount++) {
  149. lByte = (lValue >>> (lCount * 8)) & 255
  150. wordToHexValueTemp = '0' + lByte.toString(16)
  151. wordToHexValue = wordToHexValue + wordToHexValueTemp.substr(wordToHexValueTemp.length - 2, 2)
  152. }
  153. return wordToHexValue
  154. }
  155. let x = []
  156. let k
  157. let AA
  158. let BB
  159. let CC
  160. let DD
  161. let a
  162. let b
  163. let c
  164. let d
  165. const S11 = 7
  166. const S12 = 12
  167. const S13 = 17
  168. const S14 = 22
  169. const S21 = 5
  170. const S22 = 9
  171. const S23 = 14
  172. const S24 = 20
  173. const S31 = 4
  174. const S32 = 11
  175. const S33 = 16
  176. const S34 = 23
  177. const S41 = 6
  178. const S42 = 10
  179. const S43 = 15
  180. const S44 = 21
  181. str = utf8Encode(str)
  182. x = _convertToWordArray(str)
  183. a = 0x67452301
  184. b = 0xEFCDAB89
  185. c = 0x98BADCFE
  186. d = 0x10325476
  187. xl = x.length
  188. for (k = 0; k < xl; k += 16) {
  189. AA = a
  190. BB = b
  191. CC = c
  192. DD = d
  193. a = _FF(a, b, c, d, x[k + 0], S11, 0xD76AA478)
  194. d = _FF(d, a, b, c, x[k + 1], S12, 0xE8C7B756)
  195. c = _FF(c, d, a, b, x[k + 2], S13, 0x242070DB)
  196. b = _FF(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE)
  197. a = _FF(a, b, c, d, x[k + 4], S11, 0xF57C0FAF)
  198. d = _FF(d, a, b, c, x[k + 5], S12, 0x4787C62A)
  199. c = _FF(c, d, a, b, x[k + 6], S13, 0xA8304613)
  200. b = _FF(b, c, d, a, x[k + 7], S14, 0xFD469501)
  201. a = _FF(a, b, c, d, x[k + 8], S11, 0x698098D8)
  202. d = _FF(d, a, b, c, x[k + 9], S12, 0x8B44F7AF)
  203. c = _FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1)
  204. b = _FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE)
  205. a = _FF(a, b, c, d, x[k + 12], S11, 0x6B901122)
  206. d = _FF(d, a, b, c, x[k + 13], S12, 0xFD987193)
  207. c = _FF(c, d, a, b, x[k + 14], S13, 0xA679438E)
  208. b = _FF(b, c, d, a, x[k + 15], S14, 0x49B40821)
  209. a = _GG(a, b, c, d, x[k + 1], S21, 0xF61E2562)
  210. d = _GG(d, a, b, c, x[k + 6], S22, 0xC040B340)
  211. c = _GG(c, d, a, b, x[k + 11], S23, 0x265E5A51)
  212. b = _GG(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA)
  213. a = _GG(a, b, c, d, x[k + 5], S21, 0xD62F105D)
  214. d = _GG(d, a, b, c, x[k + 10], S22, 0x2441453)
  215. c = _GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681)
  216. b = _GG(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8)
  217. a = _GG(a, b, c, d, x[k + 9], S21, 0x21E1CDE6)
  218. d = _GG(d, a, b, c, x[k + 14], S22, 0xC33707D6)
  219. c = _GG(c, d, a, b, x[k + 3], S23, 0xF4D50D87)
  220. b = _GG(b, c, d, a, x[k + 8], S24, 0x455A14ED)
  221. a = _GG(a, b, c, d, x[k + 13], S21, 0xA9E3E905)
  222. d = _GG(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8)
  223. c = _GG(c, d, a, b, x[k + 7], S23, 0x676F02D9)
  224. b = _GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A)
  225. a = _HH(a, b, c, d, x[k + 5], S31, 0xFFFA3942)
  226. d = _HH(d, a, b, c, x[k + 8], S32, 0x8771F681)
  227. c = _HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122)
  228. b = _HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C)
  229. a = _HH(a, b, c, d, x[k + 1], S31, 0xA4BEEA44)
  230. d = _HH(d, a, b, c, x[k + 4], S32, 0x4BDECFA9)
  231. c = _HH(c, d, a, b, x[k + 7], S33, 0xF6BB4B60)
  232. b = _HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70)
  233. a = _HH(a, b, c, d, x[k + 13], S31, 0x289B7EC6)
  234. d = _HH(d, a, b, c, x[k + 0], S32, 0xEAA127FA)
  235. c = _HH(c, d, a, b, x[k + 3], S33, 0xD4EF3085)
  236. b = _HH(b, c, d, a, x[k + 6], S34, 0x4881D05)
  237. a = _HH(a, b, c, d, x[k + 9], S31, 0xD9D4D039)
  238. d = _HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5)
  239. c = _HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8)
  240. b = _HH(b, c, d, a, x[k + 2], S34, 0xC4AC5665)
  241. a = _II(a, b, c, d, x[k + 0], S41, 0xF4292244)
  242. d = _II(d, a, b, c, x[k + 7], S42, 0x432AFF97)
  243. c = _II(c, d, a, b, x[k + 14], S43, 0xAB9423A7)
  244. b = _II(b, c, d, a, x[k + 5], S44, 0xFC93A039)
  245. a = _II(a, b, c, d, x[k + 12], S41, 0x655B59C3)
  246. d = _II(d, a, b, c, x[k + 3], S42, 0x8F0CCC92)
  247. c = _II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D)
  248. b = _II(b, c, d, a, x[k + 1], S44, 0x85845DD1)
  249. a = _II(a, b, c, d, x[k + 8], S41, 0x6FA87E4F)
  250. d = _II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0)
  251. c = _II(c, d, a, b, x[k + 6], S43, 0xA3014314)
  252. b = _II(b, c, d, a, x[k + 13], S44, 0x4E0811A1)
  253. a = _II(a, b, c, d, x[k + 4], S41, 0xF7537E82)
  254. d = _II(d, a, b, c, x[k + 11], S42, 0xBD3AF235)
  255. c = _II(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB)
  256. b = _II(b, c, d, a, x[k + 9], S44, 0xEB86D391)
  257. a = _addUnsigned(a, AA)
  258. b = _addUnsigned(b, BB)
  259. c = _addUnsigned(c, CC)
  260. d = _addUnsigned(d, DD)
  261. }
  262. const temp = _wordToHex(a) + _wordToHex(b) + _wordToHex(c) + _wordToHex(d)
  263. return temp.toLowerCase()
  264. }
  265. {% endcodeblock %}