C# equivalent to hash_hmac in PHP

前端 未结 3 1028
盖世英雄少女心
盖世英雄少女心 2020-12-01 06:38

using .NET and C# i need to provide an integrity string using HMAC SHA512 to a PHP server . Using in C# :

Encoding encoding = Encoding.UTF8;
byte[] keyByte          


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 07:15

    As said upper, the problem was with PHP Pack(H* function used to convert key to byte array. C# Getbytes doesn't give the same result (utf8, asci, unicode...). The solution found here : http://www.nuronconsulting.com/c-pack-h.aspx was ok for me. now HMAC from C# match with PHP !

    public static byte[] PackH(string hex)
    {
           if ((hex.Length % 2) == 1) hex += '0';
           byte[] bytes = new byte[hex.Length / 2];
           for (int i = 0; i < hex.Length; i += 2)
           {
                 bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
           }
     return bytes;
    }
    

    Manu thanks to all for your help.

提交回复
热议问题