PageRenderTime 385ms CodeModel.GetById 201ms app.highlight 9ms RepoModel.GetById 172ms app.codeStats 0ms

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

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