【问题标题】:Translate SHA512 Hash function from Classic Asp to php/Laravel将 SHA512 哈希函数从 Classic Asp 翻译成 php/Laravel
【发布时间】:2023-02-16 00:25:42
【问题描述】:

我需要在 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) ) );

但我得到不同的字符串。

【问题讨论】:

  • 嗯... ToBase64String = Replace(xml.documentElement.Text,VbLf, "") 可能是不同之处。由于某种原因生成 base64 字符串后,换行符被替换。
  • 谢谢回复,请问您建议怎么办?在哈希函数之后和 base64_encode 函数之前添加类似的替换?

标签: php laravel asp-classic base64 sha512


【解决方案1】:

这是我的 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_encodingUTF-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,所以我只能使用 echoprintvar_dump 进行测试,而不是 dd,这是 PHP 中的 Base64 输出:

RLW8OiWU7AN3zhc3Avo7u7OOMjUybf8p8R98dafTPJJPCwfKbxd7soEEZlpXU4CmJ2a4HpGhnLPQFf7at1+yxA==

他们是完全匹配的。

【讨论】:

  • 感谢您的回复,我的问题是我仍然有那些由该 asp 代码生成的密码。所以我需要我的 php 代码来获得相同的结果。
  • @Andrea,我相信我已经解决了这个问题。查看更新的答案/代码。
  • 不错的代码@Adam +1
【解决方案2】:

在过去的几天里,我对此进行了很多研究,几乎没有找到任何解释如何解决这个问题的东西,幸运的是,经过多次尝试,我找到了正确的解释,点击链接:

dev.epiloum.net/1814

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2014-11-29
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 2012-11-19
    相关资源
    最近更新 更多