I am developing on a mac and deploying to Linux. I am writing a Rails API on top of an existing PHP app, so I cannot change the passwords or the crypt, but I need authentication to work. I need to be able to auth against the existing password and then ultimately change the password to still be able to be read by the original PHP app. I have access to the exact crypt function. I need a solution in Ruby (can use a gem).
The code (salt changed to protect the innocent) in PHP is this:
$passwordSHA = crypt($_POST['pass'],'$6$rounds=21000$abcdefghijklmnopqrstuv$');
I'm trying to write a function to verify that the user supplied password matches the encrypted password in the database. I tried doing what this suggests with the correct salt, but it didn't work (which may just have something to do with rounds not being included correctly).
The password stored in the database looks (sorta, changed for security) like this. Note that the beginning is always the $6$rounds=21000$zxyabcdefghijklm$
:
$6$rounds=21000$zxyabcdefghijklm$F/L4T80nbaaaaLMb7nPJ2OV5H/aaa.00v900000/Z5jlTLSa.XXXXXX./444444/p8b61UBz9z2Bj4qsABC4.
I've tried a few different things even with OpenSSL and BCrypt but I can't figure out how to even get something starting with $6
.
UPDATE: Trying the first answer, I am not getting anything near the correct length. Maybe this information is helpful in figuring out why this doesn't work:
2.5.3 :001 > ruby_crypt = "foo".crypt('$6$rounds=21000$salt$')
=> "$6A86JNndVTdM"
UPDATE 2: Please note the salt length. It's longer than the max length of 16 that UnixCrypt allows.
UPDATE 3/ANSWER: The problem here was that the salt should be truncated past 16 characters. Then additionally PHP inserts the round count afterwards. The final answer is:
hashed_password = UnixCrypt::SHA512.build(password, salt, 5000).gsub('$6', '$6$rounds=21000')