3

I need write php function calculating hash from password string. In MSSQL hash is calculated like:

lower(substring(convert(varchar(200), hashbytes('md5', 'my_password_string' + '|' + cast(cast(getdate() as date) as varchar(max))), 1), 3, 32))

What is php analogue of this crazy algorithm?

I tried this:

$password = hash('md5' , 'my_password_string'.'|'.date('YYYY-mm-dd'), false);

but result is not same

0

1 Answer 1

1

You need to use hash(), but the important parts are 1) the result of the date conversion, 2) the character encoding of the password and 3) the varchar collation of the data in the database. But the following basic example is an option:

T-SQL:

SELECT 
   LOWER(SUBSTRING(
      CONVERT(
         varchar(200),
         HASHBYTES(
            'md5', 
            'my_password_string' + '|' + CONVERT(varchar(10), CONVERT(date, GETDATE(), 23))
         ),
         1
      ),
      3,
      32
   ))      

PHP:

<?
$password = strtolower(substr(hash('md5' , 'my_password_string'.'|'.date('Y-m-d'), false), 0, 200));
echo $password;
?>

Result:

b2b2c0a81c42f802d98200daa504f197
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Zhorow ! I used other format for datetime. Your format is ok!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.