/// <summary> /// Hash+Mac string data based on the supported digest types, also, deviates the key data based on supported divination types. /// </summary> /// <param name="data">Data of any encoding type</param> /// <param name="key">Hash password. Will be deviated using the supplied divination type.</param> /// <returns>byte[]</returns> public static byte[] ToHmac(this string data, string key) { var dataBytes = new UTF8Encoding().GetBytes(data); var derivedKey = new UTF8Encoding().GetBytes(key); var digest = new HMac(new Sha256Digest()); digest.Init(new KeyParameter(derivedKey)); digest.BlockUpdate(dataBytes, 0, dataBytes.Length); var output = new byte[digest.GetMacSize()]; digest.DoFinal(output, 0); digest.Reset(); return output; }
/// <summary> /// Hash+Mac string data based on the supported digest types, also, deviates the key data based on supported divination types. /// </summary> /// <param name="data">Data of any encoding type</param> /// <param name="key">Hash password. Will be deviated using the supplied divination type.</param> /// <param name="salt">Optional, supplied 8 byte salt, one will be auto-generated if not supplied</param> /// <returns>SaltedData</returns> public static SaltedData ToHmac(this string data, string key, byte[] salt) { var salting = salt ?? 16.ToRandomBytes(); var dataBytes = new UTF8Encoding().GetBytes(data); var derivedKey = key.ToKeyDevination(salting).Data; var digest = new HMac(new Sha256Digest()); digest.Init(new KeyParameter(derivedKey)); digest.BlockUpdate(dataBytes, 0, dataBytes.Length); var output = new byte[digest.GetMacSize()]; digest.DoFinal(output, 0); digest.Reset(); return new SaltedData() {Data = output, Salt = salting}; }
public virtual ITestResult Perform() { HMac hmac = new HMac(new Sha1Digest()); byte[] resBuf = new byte[hmac.GetMacSize()]; for (int i = 0; i < messages.Length; i++) { byte[] m = Encoding.ASCII.GetBytes(messages[i]); if (messages[i].StartsWith("0x")) { m = Hex.Decode(messages[i].Substring(2)); } hmac.Init(new KeyParameter(Hex.Decode(keys[i]))); hmac.BlockUpdate(m, 0, m.Length); hmac.DoFinal(resBuf, 0); if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[i]))) { return new SimpleTestResult(false, Name + ": Vector " + i + " failed"); } } // // test reset // int vector = 0; // vector used for test byte[] m2 = Encoding.ASCII.GetBytes(messages[vector]); if (messages[vector].StartsWith("0x")) { m2 = Hex.Decode(messages[vector].Substring(2)); } hmac.Init(new KeyParameter(Hex.Decode(keys[vector]))); hmac.BlockUpdate(m2, 0, m2.Length); hmac.DoFinal(resBuf, 0); hmac.Reset(); hmac.BlockUpdate(m2, 0, m2.Length); hmac.DoFinal(resBuf, 0); if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[vector]))) { return new SimpleTestResult(false, Name + "Reset with vector " + vector + " failed"); } return new SimpleTestResult(true, Name + ": Okay"); }
public override void PerformTest() { HMac hmac = new HMac(new NonMemoableDigest(new Sha1Digest())); byte[] resBuf = new byte[hmac.GetMacSize()]; for (int i = 0; i < messages.Length; i++) { byte[] m = Strings.ToByteArray(messages[i]); if (messages[i].StartsWith("0x")) { m = Hex.Decode(messages[i].Substring(2)); } hmac.Init(new KeyParameter(Hex.Decode(keys[i]))); hmac.BlockUpdate(m, 0, m.Length); hmac.DoFinal(resBuf, 0); if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[i]))) { Fail(Name + ": Vector " + i + " failed"); } } // // test reset // { int vector = 0; // vector used for test byte[] m = Strings.ToByteArray(messages[vector]); if (messages[vector].StartsWith("0x")) { m = Hex.Decode(messages[vector].Substring(2)); } hmac.Init(new KeyParameter(Hex.Decode(keys[vector]))); hmac.BlockUpdate(m, 0, m.Length); hmac.DoFinal(resBuf, 0); hmac.Reset(); hmac.BlockUpdate(m, 0, m.Length); hmac.DoFinal(resBuf, 0); if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[vector]))) { Fail(Name + ": Reset with vector " + vector + " failed"); } } }
private void ComputeNextBlock(HMac hash) { BigEndian.CopyBytes(m_blockNumber, m_saltWithBlock, m_saltWithBlock.Length - 4); byte[] final = new byte[hash.GetMacSize()]; byte[] tmp = new byte[hash.GetMacSize()]; //InitialPass: U1 = PRF(Password, Salt || INT_32_BE(i)) hash.Reset(); hash.BlockUpdate(m_saltWithBlock,0,m_saltWithBlock.Length); hash.DoFinal(final, 0); final.CopyTo(tmp, 0); for (int iteration = 1; iteration < m_iterations; iteration++) { //U2 = PRF(Password, U1) //hash.Reset(); hash.BlockUpdate(tmp,0,tmp.Length); hash.DoFinal(tmp, 0); for (int x = 0; x < tmp.Length; x++) final[x] ^= tmp[x]; } m_blockNumber++; foreach (var b in final) m_results.Enqueue(b); }
/// <summary> /// Hash+Mac byte data based on the supported digest types, also, deviates the key data based on supported divination types. /// </summary> /// <param name="data">Data of any encoding type</param> /// <param name="key">Hash password. Will be deviated using the supplied divination type.</param> /// <returns>SaltedData</returns> public static byte[] ToHmac(this byte[] data, byte[] key) { var digest = new HMac(new Sha256Digest()); digest.Init(new KeyParameter(key)); digest.BlockUpdate(data, 0, data.Length); var output = new byte[digest.GetMacSize()]; digest.DoFinal(output, 0); digest.Reset(); return output; }