PHP analogue of MSSQL

Viewed 61

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

1 Answers

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
Related