将整数八位字节添加到字节数组

时间:2011-02-15 20:37:38

标签: c# arrays integer byte rfc

我正在尝试实施Salted Challenge Response Authentication Mechanism(RFC 5802),我遇到了一些问题。

Hi(str, salt, i):

U1   := HMAC(str, salt + INT(1))
U2   := HMAC(str, U1)
...
Ui-1 := HMAC(str, Ui-2)
Ui   := HMAC(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.

我不确定如何添加INT(1)。我有一个盐的字节数组。我需要做的就是将1移位并将其添加到数组的末尾吗?

1 个答案:

答案 0 :(得分:1)

您无法向数组添加任何内容。由于数组是固定大小,因此您需要为结果创建一个新数组。使用BitConverter类来获取整数的二进制表示形式:

// 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);