private void UpdateMac(SipHash mac, byte[] input, int updateType) { switch (updateType) { case UPDATE_BYTES: { for (int i = 0; i < input.Length; ++i) { mac.Update(input[i]); } break; } case UPDATE_FULL: { mac.BlockUpdate(input, 0, input.Length); break; } case UPDATE_MIX: { int step = System.Math.Max(1, input.Length / 3); int pos = 0; while (pos < input.Length) { mac.Update(input[pos++]); int len = System.Math.Min(input.Length - pos, step); mac.BlockUpdate(input, pos, len); pos += len; } break; } default: throw new InvalidOperationException(); } }
public override void PerformTest() { byte[] key = Hex.Decode("000102030405060708090a0b0c0d0e0f"); byte[] input = Hex.Decode("000102030405060708090a0b0c0d0e"); long expected = unchecked ((long)0xa129ca6149be45e5); SipHash mac = new SipHash(); mac.Init(new KeyParameter(key)); mac.BlockUpdate(input, 0, input.Length); long result = mac.DoFinal(); if (expected != result) { Fail("Result does not match expected value for DoFinal()"); } // NOTE: Little-endian representation of 0xa129ca6149be45e5 byte[] expectedBytes = Hex.Decode("e545be4961ca29a1"); mac.BlockUpdate(input, 0, input.Length); byte[] output = new byte[mac.GetMacSize()]; int len = mac.DoFinal(output, 0); if (len != output.Length) { Fail("Result length does not equal GetMacSize() for DoFinal(byte[],int)"); } if (!AreEqual(expectedBytes, output)) { Fail("Result does not match expected value for DoFinal(byte[],int)"); } }
/// <summary> /// テキストのハッシュを64ビット値で返す。 /// </summary> /// <param name="text">テキスト</param> /// <returns>テキストのハッシュ(64ビット)</returns> public static long ComputeSipHash(string text) { //// 16バイトの鍵を生成する。 byte[] key = Encoding.ASCII.GetBytes("dWV9#LeZGa8eu7Hx"); SipHash mac = new SipHash(); mac.Init(new KeyParameter(key)); //// テキストのハッシュを算出する。 byte[] arrayOfText = Encoding.UTF8.GetBytes(text); mac.BlockUpdate(arrayOfText, 0, arrayOfText.Length); var result = mac.DoFinal(); return(result); }