scanreg Posted September 6, 2009 Share Posted September 6, 2009 I have the following function using crypt(): function validate_form() { global $db; $errors = array(); $encrypted_password = $db->getOne('SELECT password FROM users WHERE username = ?', array($_POST['username'])); if ($encrypted_password != crypt($_POST['password'], $encrypted_password)) { $errors[] = 'Please enter a valid username and password.'; } I don't understand why the $encrypted_password is in the crypt() arguments I know that crypt() can take a second argument for salt. However, shouldn't the comparison be more like: if ($encrypted_password != crypt($_POST['password']) It's just testing whether the stored encrypted password equals an encrypted password sent through a form. Why is $encrypted_password in the crypt() function as well? Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/173317-solved-trying-to-understand-crypt-function/ Share on other sites More sharing options...
Alex Posted September 6, 2009 Share Posted September 6, 2009 That doesn't look right. Quote Link to comment https://forums.phpfreaks.com/topic/173317-solved-trying-to-understand-crypt-function/#findComment-913607 Share on other sites More sharing options...
PFMaBiSmAd Posted September 6, 2009 Share Posted September 6, 2009 The first two, 9, 12, or 16 characters, depending on the encryption type, of the "encrypted" output is the random salt that was generated when the original value was processed. The crypt() function needs that random salt when it processes the value you are trying to compare with the original. Quote Link to comment https://forums.phpfreaks.com/topic/173317-solved-trying-to-understand-crypt-function/#findComment-913610 Share on other sites More sharing options...
scanreg Posted September 6, 2009 Author Share Posted September 6, 2009 I "think" I'm getting you The crypt() "skims off" the front-end (the salt) of the second argument value and then uses that skimmed-off part (salt) as the salt for the submitted password......and then statement does the comparison: if ($encrypted_password != crypt($_POST['password'], $encrypted_password)) Am I on target? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/173317-solved-trying-to-understand-crypt-function/#findComment-913617 Share on other sites More sharing options...
PFMaBiSmAd Posted September 6, 2009 Share Posted September 6, 2009 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/173317-solved-trying-to-understand-crypt-function/#findComment-913700 Share on other sites More sharing options...
scanreg Posted September 6, 2009 Author Share Posted September 6, 2009 Wahoo!!!! Thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/173317-solved-trying-to-understand-crypt-function/#findComment-913761 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.