1

How many maximum characters can be in the $string variable?

$string = 'test';

echo md5($string);

Thank you in advance for your answers.

ModaL
  • 189
  • 1
  • 1
  • 9
  • You can easily go read up on that on https://www.php.net/manual/en/function.md5.php – CBroe Dec 10 '20 at 10:19
  • @CBroe, I don't see that there is an answer to my question. – ModaL Dec 10 '20 at 10:27
  • 1
    Sorry, misread what you meant. There is no limit (other than that PHP & memory limit might dictate.) Your comment under the currently existing answer, _“how many characters can be stored in it”_, makes rather little sense though - this does not “store” any data, this calculates a hash. – CBroe Dec 10 '20 at 10:29
  • 1
    I can't tell for sure, perhaps there isn't a specific limit beyond [PHP maximum string size](https://stackoverflow.com/questions/3189040/what-is-the-maximum-length-of-a-string-in-php). – Álvaro González Dec 10 '20 at 10:36

2 Answers2

1

A md5 is always 32 characters long, i think you can limit it

Note: As of PHP 7.0.0, there are no particular restrictions regarding the length of a string on 64-bit builds. On 32-bit builds and in earlier versions, a string can be as large as up to 2GB (2147483647 bytes maximum)

you can find doc

Tijo John
  • 686
  • 1
  • 9
  • 20
  • I don't mean the length of md5, but how many characters can be stored in it – ModaL Dec 10 '20 at 10:28
  • 2
    Your input can be as long as possible in your current programming language – Tijo John Dec 10 '20 at 10:33
  • 1
    Note: As of PHP 7.0.0, there are no particular restrictions regarding the length of a string on 64-bit builds. On 32-bit builds and in earlier versions, a string can be as large as up to 2GB (2147483647 bytes maximum) – Tijo John Dec 10 '20 at 10:35
1

If you need to calculate MD5 hashes of huge strings, perhaps you want to use files so you can save memory and calculate the hash with md5_file().

I've made a quick test with a 527 GB disk image (PHP/7.4 x64 on Windows 10). It was painfully slow (in my case the bottleneck was the file being on a slow disk) but the script had a peak memory usage of 2 MB.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360