/// <remarks> /// Encrypts the key package buffer /// </remarks> private void TransformBuffer(byte[] KeyData, byte[] Salt) { byte[] kvm = new byte[48]; // use salt to derive key and counter vector using (Keccak512 digest = new Keccak512(384)) kvm = digest.ComputeHash(Salt); byte[] key = new byte[32]; byte[] iv = new byte[16]; Buffer.BlockCopy(kvm, 0, key, 0, key.Length); Buffer.BlockCopy(kvm, key.Length, iv, 0, iv.Length); using (KeyParams keyparam = new KeyParams(key, iv)) { // 40 rounds of serpent using (CTR cipher = new CTR(new SPX(40))) { cipher.Initialize(true, keyparam); cipher.Transform(KeyData, KeyData); } } }
/// <summary> /// Test the StreamDigest class implementation /// <para>Throws an Exception on failure</</para> /// </summary> public static void StreamDigestTest() { byte[] data; MemoryStream instrm; MemoryStream outstrm = new MemoryStream(); using (KeyGenerator kg = new KeyGenerator()) data = kg.GetBytes(512); // data to digest instrm = new MemoryStream(data); byte[] code1; byte[] code2; using (StreamDigest sd = new StreamDigest(Digests.Keccak512)) { sd.Initialize(instrm); code1 = sd.ComputeHash(); } using (Keccak512 kc = new Keccak512()) code2 = kc.ComputeHash(data); // compare the hash codes if (!Compare.AreEqual(code1, code2)) throw new Exception(); }