Search code examples
mysqlencryptionpostfix-mtaencryption-asymmetric

Postfix + MySQL ENCRYPT(), How does it verify the password with randomizing salt?


I've implemented my mail server as dictated here.

It works perfectly fine. My curiousity revolves around entering users into the database and authenticating them

Running:

INSERT INTO users (email, password) VALUES ('sales@example.com', ENCRYPT('password'));

Multiple times will give a different hash for the encrypted password as its utilizing a random salt. I.e. If I enter sales@example.com three times with the same password each hash is different...

My question to this is, how is it that the Postfix server can actually authenticate the password when a user logs in via a mail client?

There isn't any problem per say as it works fine, more just to satisfy my curiosity so I can fully understand whats going on behind the scenes to properly authenticate the encrypted password.


Solution

  • Read man crypt: it returns the salt in the first two chars of the return value.

    So the salt is not lost, you can compare the encrypted string to the result of crypt( 'pass', $first_two_chars_of_encrypted_value ).