MSSQL 的 PHP 模拟

标签 php sql-server

我需要编写 php 函数来计算密码字符串的哈希值。 在 MSSQL 中,哈希的计算方式如下:

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

这个疯狂算法的 php 模拟是什么?

我试过这个:

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

但结果不一样

最佳答案

您需要使用hash(),但重要的部分是 1) 日期转换的结果,2) 密码的字符编码和 3) varchar 数据库中数据的整理。但以下基本示例是一个选项:

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;
?>

结果:

b2b2c0a81c42f802d98200daa504f197

关于MSSQL 的 PHP 模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66953737/

相关文章:

php - laravel 原始查询 last_insert_id

php - 使用 php 删除所有文件、文件夹及其子文件夹

sql-server - 仅在 SQL Server 2005 Management Studio 选项卡中显示文件名

php - 使用 sys_exec 触发 mysql 到 sql server

sql-server - 在数据库中创建新行后,我应该返回该行的主键还是数据传输对象?

PHP 显示 01 而不是 1

php - MKDIR php 命令不创建文件夹

sql-server - 如何使存储过程可供特定用户访问

sql - 调整 SQL Server

php - laravel复选框插入mysql数据库 'Integrity constraint violation'