public static byte[] PseudorandomFunction(byte[] secret, byte[] seed, int length) { byte[] result = new byte[length]; System.Security.Cryptography.HMACSHA256 hmac = new System.Security.Cryptography.HMACSHA256(secret); int iterations = (int)Math.Ceiling(length / (double)hmac.HashSize); byte[] dataToHash = seed; int offset = 0; for (int index = 0; index < iterations; index++) { dataToHash = hmac.ComputeHash(dataToHash); hmac.TransformBlock(dataToHash, 0, dataToHash.Length, dataToHash, 0); byte[] hash = hmac.TransformFinalBlock(seed, 0, seed.Length); Buffer.BlockCopy(hash, 0, result, offset, Math.Min(hash.Length, length - offset)); offset += hash.Length; } return(result); }
public static byte[] PseudorandomFunction(byte[] secret, byte[] seed, int length) { byte[] result = new byte[length]; System.Security.Cryptography.HMACSHA256 hmac = new System.Security.Cryptography.HMACSHA256(secret); int iterations = (int)Math.Ceiling(length / (double)hmac.HashSize); byte[] dataToHash = seed; int offset = 0; for (int index = 0; index < iterations; index++) { dataToHash = hmac.ComputeHash(dataToHash); hmac.TransformBlock(dataToHash, 0, dataToHash.Length, dataToHash, 0); byte[] hash = hmac.TransformFinalBlock(seed, 0, seed.Length); Buffer.BlockCopy(hash, 0, result, offset, Math.Min(hash.Length, length - offset)); offset += hash.Length; } return result; }