How to compare two passwords, one encrypted with the MySQL ENCRYPT function, when the other one is clear ?

Here’s how to compare:

  • Get the encrypted_password
  • Get the not_encrypted_password
  • Compare the passwords by encoding the not_encrypted_password using the two first characters of the encrypted_password as salt for the crypt method.

In Ruby language, this means coding something like this:

not_encrypted_password.crypt(encrypted_password[0,2]) == encrypted_password

return true if passwords are maching. Youpi !

I did this article to never forget this… Hoping it would be helpful for someone else !