将Java MessageDigest代码段转换为C#

时间:2016-05-02 08:51:31

标签: java c#

下面的java代码使用生成输入 msg 的SHA-256哈希值。但是,在C#中编写执行相同操作的代码的所有尝试都没有在给定相同输入的情况下产生相同的结果。我需要帮助获得C#等价物,因为我已经尝试了很多但收效甚微。

我认为除了更新摘要的部分( m.update())之外,我已经能够正确地将大部分代码转换为C#,首先使用密钥,然后是哈希之前的消息。

JAVA CODE

public static String generateHash256Value(String msg, String key) {
    MessageDigest m = null;
    String hashText = null;
    System.out.println("Value to hash::::::::::" + msg);
    byte[] actualKeyBytes = HexToByte(secret_key);

    try {
        m = MessageDigest.getInstance("SHA-256");
        m.update(actualKeyBytes, 0, actualKeyBytes.length);

        try {
            m.update(msg.getBytes("UTF-8"), 0, msg.length());
        } 

        catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }

        hashText = new BigInteger(1, m.digest()).toString(16);

        if (hashText.length() < 64) { //must be 64 in length
            int numberOfZeroes = 64 - hashText.length();
            String zeroes = "";

            for (int i = 0; i < numberOfZeroes; i++) {
                zeroes = zeroes + "0";
            }
            hashText = zeroes + hashText;
        }
    } 

    catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    }

    hashText = hashText.toUpperCase();
    return hashText;
}

public static byte[] hex2Byte(String str) {
    byte[] bytes = new byte[str.length() / 2];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = (byte) Integer
                .parseInt(str.substring(2 * i, 2 * i + 2), 16);
    }
    return bytes;
}

C#CODE (尝试)

    private static string DoSpecialSha256Hash(string message, string key)
    {
        String hashText = null;
        Console.WriteLine("Value to hash::::::::::" + message);
        byte[] keyByte = hex2Byte(key);

        Encoding encoder = new System.Text.UTF8Encoding();

        var hashAlgo = new SHA256Managed();

        var messageBytes = encoder.GetBytes(message);
        var toDigest = Combine(keyByte, messageBytes);

        hashText = ByteToString(hashAlgo.ComputeHash(toDigest, 0, message.Length));

        if (hashText.Length < 64)
        { //must be 64 in length
            int numberOfZeroes = 64 - hashText.Length;
            String zeroes = "";

            for (int i = 0; i < numberOfZeroes; i++)
            {
                zeroes = zeroes + "0";
            }
            hashText = zeroes + hashText;
        }

        hashText = hashText.ToUpper();
        return hashText;
    }

    public static byte[] HexToByte(String hex)
    {
        return Enumerable.Range(0, hex.Length)
                .Where(x => x % 2 == 0)
                .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                .ToArray();
    }

    private static string ByteToString(byte[] buff)
    {
        string sbinary = "";

        for (int i = 0; i < buff.Length; i++)
        {
            sbinary += buff[i].ToString("X2"); // hex format
        }
        return (sbinary);
    }

    private static byte[] Combine(params byte[][] arrays)
    {
        byte[] rv = new byte[arrays.Sum(a => a.Length)];
        int offset = 0;
        foreach (byte[] array in arrays)
        {
            System.Buffer.BlockCopy(array, 0, rv, offset, array.Length);
            offset += array.Length;
        }
        return rv;
    }

谢谢,

0 个答案:

没有答案