我需要检查 php/Laravel 中 asp 生成的密码。 您能帮我翻译这个用于生成这些密码的 asp 代码吗?
password = "xxx"
salt = "yyy"
saltedPassword = salt & password
Set objUnicode = CreateObject("System.Text.UnicodeEncoding")
arrByte = objUnicode.GetBytes_4(saltedPassword)
Set objSHA512 = Server.CreateObject("System.Security.Cryptography.SHA512Managed")
strHash = objSHA512.ComputeHash_2((arrByte))
'Ref: http://stackoverflow.com/questions/1118947/converting-binary-file-to-base64-string
Dim xml: Set xml = CreateObject("MSXML2.DOMDocument.3.0")
xml.LoadXml "<root />"
xml.documentElement.dataType = "bin.base64"
xml.documentElement.nodeTypedValue = strHash
ToBase64String = Replace(xml.documentElement.Text,VbLf, "")
Response.Write "<p>" & ToBase64String & "</p>"
我尝试过这个:
$password = 'xxx';
$salt = 'yyy';
$saltedpass = $salt.$password;
dd( base64_encode( hash('sha512', $saltedpass, true) ) );
但我得到了不同的字符串。
最佳答案
这是我的 ASP 哈希函数,适合任何希望在经典 ASP 中进行快速而简单的哈希处理的人。它允许您散列为 MD5、SHA1、SHA256、SHA384 和 SHA512,以及编码为 Hex 或 Base64。
更新 我还提供了一个选项来指定字符集(Unicode 或 UTF8)。
Function Hash(ByVal Input, HashAlgorithm, CharSet, Encoding)
' Select the System.Security.Cryptography value.
Select Case uCase(HashAlgorithm)
Case "MD5"
HashAlgorithm = "MD5CryptoServiceProvider"
Case "SHA1"
HashAlgorithm = "SHA1CryptoServiceProvider"
Case "SHA2","SHA256"
HashAlgorithm = "SHA256Managed"
Case "SHA384"
HashAlgorithm = "SHA384Managed"
Case "SHA5","SHA512"
HashAlgorithm = "SHA512Managed"
Case Else
HashAlgorithm = "SHA1CryptoServiceProvider"
End Select
' Convert the input to bytes if not already.
If NOT VarType(Input) = 8209 Then
Dim CS : Set CS = Server.CreateObject("System.Text." & CharSet & "Encoding")
Input = CS.GetBytes_4(Input)
Set CS = Nothing
End If
' Perform the hash.
Dim hAlg : Set hAlg = Server.CreateObject("System.Security.Cryptography." & HashAlgorithm)
Dim hEnc : Set hEnc = Server.CreateObject("MSXML2.DomDocument").CreateElement("encode")
Encoding = lCase(Encoding)
If Encoding = "base64" OR Encoding = "b64" Then
hEnc.dataType = "bin.base64"
Else
hEnc.dataType = "bin.hex"
End If
hEnc.nodeTypedValue = hAlg.ComputeHash_2((Input))
Hash = hEnc.Text
Hash = Replace(Hash,VBlf,"")
Set hEnc = Nothing
Set hAlg = Nothing
End Function
Dim password, salt, saltedPassword
password = "xxx"
salt = "yyy"
saltedPassword = salt & password
Response.Write(Hash(saltedPassword,"SHA512","Unicode","Base64"))
在此示例中,我将其设置为匹配您的代码,因此它使用 System.Text.UnicodeEncoding
来获取字节(尽管默认情况下应使用 UTF8,这就是为什么您的 PHP 代码返回不同的 Base64 字符串),并且需要 Hash = Replace(Hash,VBlf,"")
,因为 bin.base64
几乎总是包含换行符,但 PHP 从不做。这是 Base64 输出:
RLW8OiWU7AN3zhc3Avo7u7OOMjUybf8p8R98dafTPJJPCwfKbxd7soEEZlpXU4CmJ2a4HpGhnLPQFf7at1+yxA==
...它与 ASP 代码生成的 Base64 输出相匹配。
现在要在 PHP 中实现相同的效果,只需在加入盐和密码时使用 mb_convert_encoding
和 UTF-16LE
即可:
$password = 'xxx';
$salt = 'yyy';
$saltedpass = mb_convert_encoding($salt.$password,'UTF-16LE');
echo(base64_encode(hash('sha512',$saltedpass,true)));
PHP hash
函数的行为与在经典 ASP 中使用 System.Text.UnicodeEncoding
相同。我没有安装 Laravel,所以只能使用 echo
、print
或 var_dump
进行测试,但不能使用 dd
>,这是 PHP 中的 Base64 输出:
RLW8OiWU7AN3zhc3Avo7u7OOMjUybf8p8R98dafTPJJPCwfKbxd7soEEZlpXU4CmJ2a4HpGhnLPQFf7at1+yxA==
它们完全匹配。
关于php - 将 SHA512 哈希函数从经典 Asp 转换为 php/Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72059249/