I created a custom form for the registration, but I am not getting how to store the password in hash format in the database.

I am using the following code, but I am not getting the value Drupal saves in the database for the password.

require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
echo user_hash_password('taliadmin');

Why am I getting a different value?

  • I can't understand what you are asking. You want to put user password, unencrypted, and get the same hash Drupal is getting? – Mołot Nov 6 '14 at 14:29
  • i want to store password in hash format like drupal do.. so tell me which method i need to use? – DeDevelopers Nov 6 '14 at 14:34
  • 3
    I guess just use the password default field... it will do the job automatically :o – Djouuuuh Nov 6 '14 at 14:35
  • i created custom module not using defualt signup module of drupa.. – DeDevelopers Nov 6 '14 at 14:36
  • 1
    drupal.org/node/1063598 – Djouuuuh Nov 6 '14 at 14:40

That's pretty much the point. user_hash_password uses _password_generate_salt, which in turn uses drupal_random_bytes. This means hash returned will always contain randomization and is designed not to return the same result twice. Or at least do it as rarely as possible, because there is 2^(6*8) possible outputs of 6 random bytes.

To test passwords, Drupal uses user_check_password. This function extracts salt from stored password and applies it to the one user provided.

Note that by design Drupal does not have any function that would allow to recreate hashes perfectly, and it's not possible to write one (without using original hash to extract salt, that is). You can get pretty close if you need to, copying logic from user_check_password, but it is actually better to leave salt changing as much as possible.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.