this is code and works but when change console.writeline to data.Logger.LogObject got error.
class Hkdf
{
Func<byte[], byte[], byte[]> keyedHash;
public Hkdf()
{
var hmac = new HMACSHA256();
keyedHash = (key, message) =>
{
hmac.Key = key;
return hmac.ComputeHash(message);
};
}
public byte[] Extract(byte[] salt, byte[] inputKeyMaterial)
{
return keyedHash(salt, inputKeyMaterial);
}
public byte[] Expand(byte[] prk, byte[] info, int outputLength)
{
var resultBlock = new byte[0];
var result = new byte[outputLength];
var bytesRemaining = outputLength;
for (int i = 1; bytesRemaining > 0; i++)
{
var currentInfo = new byte[resultBlock.Length + info.Length + 1];
Array.Copy(resultBlock, 0, currentInfo, 0, resultBlock.Length);
Array.Copy(info, 0, currentInfo, resultBlock.Length, info.Length);
currentInfo[currentInfo.Length - 1] = (byte)i;
resultBlock = keyedHash(prk, currentInfo);
Array.Copy(resultBlock, 0, result, outputLength - bytesRemaining, Math.Min(resultBlock.Length, bytesRemaining));
bytesRemaining -= resultBlock.Length;
}
return result;
}
public byte[] DeriveKey(byte[] salt, byte[] inputKeyMaterial, byte[] info, int outputLength)
{
var prk = Extract(salt, inputKeyMaterial);
var result = Expand(prk, info, outputLength);
return result;
}
}
class Program
{
static void Main()
{
string password = "Test2";
string salt = "8e94ef805b93e683ff18";
int length = 16;
string infoString = "SomeInfo";
var hkdf = new Hkdf();
var inputKeyMaterial = Encoding.UTF8.GetBytes(password);
var saltBytes = FromHexString(salt);
var infoBytes = Encoding.UTF8.GetBytes(infoString);
var result = hkdf.DeriveKey(saltBytes, inputKeyMaterial, infoBytes, length);
var resultHex = BitConverter.ToString(result).Replace("-", "").ToLower();
data.Logger.LogObject("Derived Key (Hex): {0}", resultHex);
}
public static byte[] FromHexString(string hex)
{
if (hex.Length % 2 != 0)
{
throw new ArgumentException("The hex string must have an even length.", nameof(hex));
}
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;
}
}