用PHP和MySQL解析我的哈希

像大多数用户一样,我只是试图找出一种安全的方式来存储密码.我在这里找不到的东西(或者也许是我的缺乏理解)是如何在我的数据库中检索一个盐渍哈希,并将盐与散列密码分开,特别是在每个口令中使用独特的盐,同时将盐密码保存在一个柱.

我正在找到所有这些很酷的方法加密密码(SHA-256,但是MySQL支持SHA / 1和MD5?)以及PHP手册中的其他内容,但不知道如何存储和检索密码.

所以,这是我所了解的全部:

SHA('$salt'.'$password') // My query sends the password and salt 
                         // (Should the $salt be a hash itself?)

之后,我失去了盐.

检索密码没有盐很容易,但盐混淆了我.我从哪里获得$salt的价值,特别是如果它是独一无二的和安全的?我将其隐藏在另一个数据库中吗?恒(似乎不安全)?

编辑:HMAC中的关键变量是盐还是其他?

首先,您的DBMS(MysqL)不需要对加密散列进行任何支持.你可以在PHP方面做所有这些,这也是你应该做的.

如果要将salt和hash存储在同一列中,则需要连接它们.

// the plaintext password
$password = (string) $_GET['password'];

// you'll want better RNG in reality
// make sure number is 4 chars long
$salt = str_pad((string) rand(1,1000),4,'0',STR_PAD_LEFT);

// you may want to use more measures here too
// concatenate hash with salt
$user_password = sha512($password . $salt) . $salt;

现在,如果要验证您所做的密码:

// the plaintext password
$password = (string) $_GET['password'];

// the hash from the db
$user_password = $row['user_password'];

// extract the salt
// just cut off the last 4 chars
$salt = substr($user_password,-4);
$hash = substr($user_password,-4);

// verify
if (sha512($password . $salt) == $hash) {
  echo 'match';
}

您可能想看看phpass,它也使用这种技术.这是一个PHP散列解决方案,其中使用盐析.

你一定要看看WolfOdrade链接到的问题的答案.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...