How do you implement one way string hashing in PHP7?

A1WEBSITEPRO QuestionsCategory: PHPHow do you implement one way string hashing in PHP7?
Scott T asked 5 years ago

How do you implement one way string hashing in PHP7?

How do you implement one way string hashing in PHP7? was last modified: November 28th, 2018 by Scott T
1 Answers
Best Answer
Maximus Mccullough Staff answered 5 years ago

If you want to do one-way string hashing, the function you will use is crypt() This will return a hashed string that uses DES, MD5 or Blowfish algorithms. Keep in mind that this can act differently on different operating systems. PHP7 will check to see which algorithms are available and what it will use when it is installed. As this is a one-way encryption algorithm, there is no decrypt function to reverse it.
Some constants are used together with the crypt() function. Their value is set by PHP when it is installed. The following is a list of these constants:

  • [CRYPT_STD_DES] – Standard DES-based hash with two character salt from the alphabet “./0-9A-Za-z”. If you use invalid characters in the salt, it will cause this function to fail.
  • [CRYPT_EXT_DES] – Extended DES-based hash with a nine character salt consisting of an underscore followed by 4 bytes of iteration count and 4 bytes of salt. This is encoded as printable characters, 6 bits per character, least significant character first. The values 0 to 63 are encoded as “./0-9A-Za-z”. If you use invalid characters in the salt it will cause the function to fail.
  • [CRYPT_MD5] – MD5 hashing with a 12 character salt starting with $1$
  • [CRYPT_BLOWFISH] – Blowfish hashing with a salt starting with $2a$,  $2x$, or $2y$, a two digit cost parameters “$”, and 22 characters from the alphabet “./0-9A-Za-z”. If you use characters outside of the alphabet, it will cause this function to return a zero-length string. The “$” parameter is the base-2 logarithm of the iteration count for the underlying Blowfish-bashed hashing algorithmeter and has to be in range 04-31. If the values are outside of this range it will cause the function to fail.
  • [CRYPT_SHA_256] – SHA-256 hash with a 16 character salt starting with  $5$. If the salt string starts with “rounds=<N>$”, the numeric value of N is used to show how many times the hashing loop needs to be executed, just like the cost parameter on Blowfish. The default number of rounds is 5000, there is a minimum of 1000 and a maximum of 999,999,999. Any selection of N that is outside of this range gets truncated to the nearest limit.
  • [CRYPT_SHA_512] – SHA-512 hash with a 16 character salt starting with $6$. If the salt string starts with “rounds=<N>$”, the numeric value of N is utilized to show how many times the hashing loop needs to be executed, the same as the cost parameter on Blowfish. The default number of rounds is 5000, there is a minimum of 1000 and a maximum of 999,999,999. Any selection of N outside of this range gets truncated to the nearest limit.

On systems where this function supports multiple algorithms, the constants above are set to “1” if supported and “0” otherwise.

Answer for How do you implement one way string hashing in PHP7? was last modified: November 28th, 2018 by Maximus Mccullough