PHP密码保护:md5到sha512

时间:2022-05-14 05:02:02

How do I use the sha512 function for PHP?

如何在PHP中使用sha512函数?

Can I replace all my md5 functions with the sha512 function?

我可以用sha512功能替换所有md5功能吗?

Do I have to download something if so what?

如果是这样的话,我是否必须下载什么?

Can anyone provide examples?

任何人都可以举例吗?

3 个解决方案

#1


6  

The hash() function, provided with PHP >= 5.1, should be able to generate sha512 hashes -- you can verify this calling the hash_algos() function, that lists the supported hashing algorithms.

用PHP> = 5.1提供的hash()函数应该能够生成sha512哈希 - 你可以验证这个调用hash_algos()函数,它列出了支持的哈希算法。


For example, you could use :

例如,您可以使用:

$sha512 = hash('sha512', "Hello, World!");
var_dump($sha512);

And you'd get :

你会得到:

string '374d794a95cdcfd8b35993185fef9ba368f160d8daf432d08ba9f1ed1e5abe6cc69291e0fa2fe0006a52570ef18c19def4e617c33ce52ef0a6e5fbe318cb0387' (length=128)


And, on my system, the following portion of code :

并且,在我的系统上,代码的以下部分:

$supported = hash_algos();
var_dump($supported);

Indicates that 42 hashing algorithms are supported :

表示支持42种哈希算法:

array
  0 => string 'md2' (length=3)
  ...
  6 => string 'sha384' (length=6)
  7 => string 'sha512' (length=6)
  8 => string 'ripemd128' (length=9)
  9 => string 'ripemd160' (length=9)
  ...
  40 => string 'haval224,5' (length=10)
  41 => string 'haval256,5' (length=10)



Also, with PHP >= 5.3, you should be able to use the openssl_digest() function :

此外,使用PHP> = 5.3,您应该能够使用openssl_digest()函数:

$sha512 = openssl_digest("Hello, World!", 'sha512');
var_dump($sha512);

(Yep, the parameters are not in the same order as with hash() -- the magic of PHP, here...)

(是的,参数与hash()的顺序不同 - PHP的神奇之处,这里......)

And, to get the list of supported algorithms, you could use openssl_get_md_methods().

并且,要获取支持的算法列表,可以使用openssl_get_md_methods()。

On my system, this one gives me 22 supported algorithms.

在我的系统上,这个给了我22个支持的算法。

#2


1  

Just out of curiosity, why do you want to replace the MD5 function?

出于好奇,你为什么要更换MD5功能?

It is relatively efficient. If you add a salt, it is really annoying to reverse engineer. Someone would have to perform a brute force encoding of all passwords looking for a match. Without a salt, common short strings lower case all letter strings have been cracked and stored in a database.

它相对有效。如果添加盐,逆向工程真的很烦人。有人必须对寻找匹配的所有密码执行强制编码。没有盐,常见的短字符串小写字母所有字母字符串都已被破解并存储在数据库中。

I would just add a salt and call it good.

我只想添加盐并称之为好。

Jacob

#3


0  

Checksums are for generating checksums, HMAC is perhaps the preferred way for generating salted hashes of strings requiring securing hashing.

校验和用于生成校验和,HMAC可能是生成需要保护散列的字符串哈希值的首选方法。

hash_hmac('sha512', 'important string', 'salt');

#1


6  

The hash() function, provided with PHP >= 5.1, should be able to generate sha512 hashes -- you can verify this calling the hash_algos() function, that lists the supported hashing algorithms.

用PHP> = 5.1提供的hash()函数应该能够生成sha512哈希 - 你可以验证这个调用hash_algos()函数,它列出了支持的哈希算法。


For example, you could use :

例如,您可以使用:

$sha512 = hash('sha512', "Hello, World!");
var_dump($sha512);

And you'd get :

你会得到:

string '374d794a95cdcfd8b35993185fef9ba368f160d8daf432d08ba9f1ed1e5abe6cc69291e0fa2fe0006a52570ef18c19def4e617c33ce52ef0a6e5fbe318cb0387' (length=128)


And, on my system, the following portion of code :

并且,在我的系统上,代码的以下部分:

$supported = hash_algos();
var_dump($supported);

Indicates that 42 hashing algorithms are supported :

表示支持42种哈希算法:

array
  0 => string 'md2' (length=3)
  ...
  6 => string 'sha384' (length=6)
  7 => string 'sha512' (length=6)
  8 => string 'ripemd128' (length=9)
  9 => string 'ripemd160' (length=9)
  ...
  40 => string 'haval224,5' (length=10)
  41 => string 'haval256,5' (length=10)



Also, with PHP >= 5.3, you should be able to use the openssl_digest() function :

此外,使用PHP> = 5.3,您应该能够使用openssl_digest()函数:

$sha512 = openssl_digest("Hello, World!", 'sha512');
var_dump($sha512);

(Yep, the parameters are not in the same order as with hash() -- the magic of PHP, here...)

(是的,参数与hash()的顺序不同 - PHP的神奇之处,这里......)

And, to get the list of supported algorithms, you could use openssl_get_md_methods().

并且,要获取支持的算法列表,可以使用openssl_get_md_methods()。

On my system, this one gives me 22 supported algorithms.

在我的系统上,这个给了我22个支持的算法。

#2


1  

Just out of curiosity, why do you want to replace the MD5 function?

出于好奇,你为什么要更换MD5功能?

It is relatively efficient. If you add a salt, it is really annoying to reverse engineer. Someone would have to perform a brute force encoding of all passwords looking for a match. Without a salt, common short strings lower case all letter strings have been cracked and stored in a database.

它相对有效。如果添加盐,逆向工程真的很烦人。有人必须对寻找匹配的所有密码执行强制编码。没有盐,常见的短字符串小写字母所有字母字符串都已被破解并存储在数据库中。

I would just add a salt and call it good.

我只想添加盐并称之为好。

Jacob

#3


0  

Checksums are for generating checksums, HMAC is perhaps the preferred way for generating salted hashes of strings requiring securing hashing.

校验和用于生成校验和,HMAC可能是生成需要保护散列的字符串哈希值的首选方法。

hash_hmac('sha512', 'important string', 'salt');