开发者

Adding an integer octet to a byte array

开发者 https://www.devze.com 2023-02-11 19:42 出处:网络
I am trying to implement Salted Challenge Response Authentication Mechanism (RFC 5802) and I am running into a bit of a problem.

I am trying to implement Salted Challenge Response Authentication Mechanism (RFC 5802) and I am running into a bit of a problem.

Hi(str, salt, i):

U1   := HMAC(str, salt + INT(1))
U2   := HMAC(str, U1)
...
Ui-1 := HMAC(str, Ui-2)
Ui   := H开发者_Python百科MAC(str, Ui-1)

Hi := U1 XOR U2 XOR ... XOR Ui

where "i" is the iteration count, "+" is the string concatenation
operator, and INT(g) is a 4-octet encoding of the integer g, most
significant octet first.

I am unsure of how to add the INT(1). I have a byte array for salt. Do all I need to do is bit shift the 1 and add it to the end of the array?


You can't add anything to an array. As arrays are fixed size you need to create a new array for the result. Use the BitConverter class to get the binary representation of the integer:

// create new array
byte[] key = new byte[salt.Length + 4];
// copy salt
Array.Copy(salt, key, salt.Length);
// create array from integer
byte[] g = BitConverter.GetBytes(1);
if (BitConverter.IsLittleEndian) {
  Array.Reverse(g);
}
// copy integer array
Array.Copy(g, 0, key, salt.Length, 4);
0

精彩评论

暂无评论...
验证码 换一张
取 消