简体   繁体   English

我应该如何将我的 sha512 加盐和散列密码存储在 MySQL 中?

[英]How should I store my sha512 salted & hashed passwords in MySQL?

All,全部,

I am using the following PHP function to salt & hash user passwords for a web app:我正在使用以下 PHP function 来加盐和 hash 用户密码为 Z2567A5EC9705EB7AC829C 应用程序:

function stringHashing($password,$salt){
 $hashedString=$password.$salt;
 for ($i=0; $i<50; $i++){
  $hashedString=hash('sha512',$password.$hashedString.$salt);
  }
 return $hashedString;
}  

What is the best way to store the resulting string in MySQL?将结果字符串存储在 MySQL 中的最佳方法是什么? I suppose it is a fixed CHAR field?我想这是一个固定的 CHAR 字段? How should I go about calculating the right length?我应该如何计算正确的长度?

Thanks,谢谢,

JDelage杰拉格

Well, SHA512 will always return a 512 bit hash, the two-argument hash() method returns this as hex digits, so that's 512 bits / 8 bits per byte * 2 hex digits per byte = 128 hex digits好吧,SHA512 将始终返回一个 512 位 hash,双参数hash()方法将其返回为十六进制数字,所以这是 512 位/每字节 8 位 * 每字节 2 十六进制数字 = 128 十六进制数字

A CHAR(128) should be what you need一个CHAR(128)应该是你需要的

If someone knows your salt, they probably have your source code which guides them to repeat it 50 times.如果有人知道你的盐,他们可能有你的源代码,引导他们重复 50 次。 In that light, given the trivial security benefit to recursive re-hashing with a fixed-count, I thought I'd suggest using SHA2() from MySQL 5.5.5+ as a neater alternative:鉴于此,鉴于使用固定计数进行递归重新散列的微不足道的安全优势,我想我建议使用 MySQL 5.5.5+ 中的SHA2()作为更简洁的替代方案:

mysql_query("SELECT SHA2(CONCAT('$password','$salt'), 512) AS `hash`;");

Which will be your VARCHAR(128) ready to INSERT / UPDATE .这将是您的VARCHAR(128)准备好INSERT / UPDATE

I have always used a varchar field with a more-than-needed length.我一直使用长度超过需要的 varchar 字段。 What if, down the road, you want to change your algorithm?如果你想改变你的算法怎么办? You have to alter the table, which is annoying.您必须更改表格,这很烦人。

SHA512 will produce a 128 char string, so give the field at least that. SHA512 将产生一个 128 字符的字符串,所以至少给这个字段。

Also, I must point out that you are doing a lot of wasted processing.另外,我必须指出,您正在做很多浪费的处理。 You are not adding too much security by iterating through the same salt and hash over and over and over again.您不会通过一遍又一遍地迭代相同的盐和 hash 来增加太多的安全性。 Ultimately, you will still need a salt and password, everything else about the algorithm remains constant.最终,你仍然需要一个盐和密码,算法的其他一切都保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM