传统加密方式:

md5(密码+盐值);

$passwordString='yourpassword';//你的密码$salt="yoursaltvalue";//盐值,增加复杂度(随机字串)$md5Password=md5($passwordString.$salt);

从理论上来说,md5不可逆,算是一种比较安全的加密方式。但是我要提醒的是,md5早在04年的时候就被中国人破解(请自行搜索山东大学王小云)。一旦被人***的化,密码泄漏的可能性极大。


现在推荐一种新的处理方式:密码散列算法函数

password_get_info — 返回指定哈希(hash)的相关信息

password_hash — 创建密码的哈希(hash)

password_needs_rehash — Checks if the given hash matches the given options

password_verify — 验证密码是否和哈希匹配



PHP5.5引入了Password Hashing函数,内核自带无需安装扩展。在PHP5.4下测试了下也可是可以的,使用前最好确认一下你当前的环境是否支持这些函数。


Password Hashing主要提供了4个函数

//查看哈希值的相关信息arraypassword_get_info(string$hash)//创建hash密码stringpassword_hash(string$password,integer$algo[,array$options])//判断hash密码是否特定选项、算法所创建booleanpassword_needs_rehash(string$hash,integer$algo[,array$options]booleanpassword_verify(string$password,string$hash)//验证密码


代码演示:

$password='password123456';//原始密码//使用BCRYPT算法加密密码$hash_password=password_hash($password,PASSWORD_BCRYPT);if(password_verify($password,$hash_password)){echo"密码匹配";}else{echo"密码错误";}


重要特征:

通过password_hash加密后的密码,使用字典方式很难破解,因为每次生成的密码都是不一样的。破解这种加密只能采用暴力破解。


最后提醒:

加密方法再好,原始密码设置的过于简单都容易被破解,设置复杂的密码才是王道。