将命令从C#转换为PowerShell

时间:2018-12-14 12:59:35

标签: powershell

能帮我将下面的C#转换为PowerShell

        byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];

下面是我要在此处执行的完整代码。 我正在尝试在此处生成哈希码

 Function CalculateHashWithSalt($input = "Password@123", $salt="qLTf99m__JGu", $algorithmName = "SHA512")
    {


       $pass = [System.Text.Encoding]::UTF8


       $input = "Password123"
       $data1 = $pass.GetBytes($input)

       $saltbytes = [System.Text.Encoding]::UTF8
       $saltbytes=$saltbytes.GetBytes($salt)

        $plainTextWithSaltBytes=@()
        $plainTextWithSaltBytes = $data1.Length + $saltbytes.Length



        for ($i = 0; $i -lt $data1.Length; $i++)
        {
            $plainTextWithSaltBytes[$i] = $data1[$i]
        }

        for ($i = 0; $i -lt $saltbytes.Length; $i++)
        {
            $plainTextWithSaltBytes[$pass.Length + $i] = $saltbytes[$i];
        }

        [System.Byte[]]::new($hashBytes)
        $hashBytes = $algorithmName.ComputeHash($plainTextWithSaltBytes);


        $Encrypted = [System.Convert]::FromBase64String($hashBytes)

    }

尝试转换以下c#代码: C# code link

1 个答案:

答案 0 :(得分:0)

来自https://blogs.msdn.microsoft.com/luc/2011/01/21/powershell-getting-the-hash-value-for-a-string/

我的个人脚本中具有此功能,并带有要使用的算法参数。

function Hash($textToHash)
{
    $hasher = new-object System.Security.Cryptography.SHA256Managed
    $toHash = [System.Text.Encoding]::UTF8.GetBytes($textToHash)
    $hashByteArray = $hasher.ComputeHash($toHash)
    foreach($byte in $hashByteArray)
    {
         $res += $byte.ToString()
    }
    return $res;
}
相关问题