Billy Moon Billy Moon - 4 months ago 20
Linux Question

How can I calculate shadow password for sha-512 using php/shell

I created account sysadmin with password delme. I then want to calculate the sha-512 of the password using php.

I expected this should produce the same hash...

billy@iserve:~$ sudo cat /etc/shadow | grep sysadmin
billy@iserve:~$ php -r "echo crypt('delme','$6$rounds=5000$q5HxMEDr$').\"\n"\";"

I can not replicate the password in the shadow file... can you tell me how?


I ended up doing this to solve my problem...

/*    Need to add www-data to group shadow (and restart apache)
        $ sudo adduser www-data shadow
        $ sudo /etc/init.d/apache2 restart
      Needs whois to be installed to run mkpasswd
        $ sudo apt-get install whois
      Assumes that sha-512 is used in shadow file

function authenticate($user, $pass){
  // run shell command to output shadow file, and extract line for $user
  // then split the shadow line by $ or : to get component parts
  // store in $shad as array
  $shad =  preg_split("/[$:]/",`cat /etc/shadow | grep "^$user\:"`);
  // use mkpasswd command to generate shadow line passing $pass and $shad[3] (salt)
  // split the result into component parts and store in array $mkps
  $mkps = preg_split("/[$:]/",trim(`mkpasswd -m sha-512 $pass $shad[3]`));
  // compare the shadow file hashed password with generated hashed password and return
  return ($shad[4] == $mkps[3]);

// usage...
  // logged in   
} else {
  // not valid user

I am not entirely confident about the security of this method, so will be asking a question about that.