不同操作系统上的PHP crypt功能 [英] PHP crypt function on different OS

查看:67
本文介绍了不同操作系统上的PHP crypt功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的php应用程序中,我使用php crypt()函数,其中盐值是用户名中的前两个字符.我注意到该函数在Windows和Linux上返回不同的结果.我还在w3cschools上读到过,该功能在不同的操作系统上的行为不同.可以配置php环境以便在两个操作系统上获得相同的结果吗? (修改加密模式不是一种选择.)

In my php application I use the php crypt() function where my salt value is the first two characters from the username. I noticed that the function returns a different result on windows and linux. I also read on w3cschools that this function behaves different on different operating systems. It is possible to configure the php environment in order to obtain the same result on both operating systems? (Modifying the encryption mode is not an option.)

推荐答案

crypt()使用操作系统使用的任何基础哈希函数,因此,如果要获得可靠的(恒定的)结果,可以使用以下方法之一其他哈希函数,例如 md5() sha256(),sha512().

crypt() uses whatever underlying hash function the OS uses, so if you want reliable (constant) results you could use one of the other hash functions like md5() or sha256(),sha512().

如果希望 crypt()使用特定的哈希函数,则必须相应地指定hash参数,并检查主机操作系统是否支持该算法.例如(取自 crypt()的PHP手册页):

If you want a particular hashing function to be used by crypt() you have to specify the hash parameter accordingly and check if the algorithm is supported on the host OS. For example (taken from PHP Manual page of crypt()):

if (CRYPT_STD_DES == 1) {
        echo 'Standard DES: ' . crypt('rasmuslerdorf', 'rl') . "\n";
} 

但是它非常依赖于操作系统,因此我建议您使用独立的哈希函数.或 hash().

But its very OS dependent, so I recommend you use a standalone hash function. Or hash().

追加:

使用 hash(),您将首先使用 hash_algos()来检查哪个散列是受支持的最佳散列,然后将其用作第一个参数,如下所示:

With hash() you would first use hash_algos() to check which hash is the best hash supported and then use that as the first argument, like this:

<?php
    $algos = hash_algos();
    if (in_array("sha256", $algos)) {
        $pass = hash ("sha256", "userpassword" . "salt");
    }
?>

希望这会有所帮助.

这篇关于不同操作系统上的PHP crypt功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆