Ejemplo n.º 1
0
        private void RandomTest(SecureRandom random)
        {
            byte[] key = new byte[16];
            random.NextBytes(key);

            int length = 1 + random.Next(1024);

            byte[] input = new byte[length];
            random.NextBytes(input);

            SipHash mac = new SipHash();

            mac.Init(new KeyParameter(key));

            UpdateMac(mac, input, UPDATE_BYTES);
            long result1 = mac.DoFinal();

            UpdateMac(mac, input, UPDATE_FULL);
            long result2 = mac.DoFinal();

            UpdateMac(mac, input, UPDATE_MIX);
            long result3 = mac.DoFinal();

            if (result1 != result2 || result1 != result3)
            {
                Fail("Inconsistent results in random test");
            }
        }
Ejemplo n.º 2
0
        private void RunMac(byte[] key, byte[] input, int updateType)
        {
            long expected = unchecked ((long)0xa129ca6149be45e5);

            SipHash mac = new SipHash();

            mac.Init(new KeyParameter(key));

            UpdateMac(mac, input, updateType);

            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");

            UpdateMac(mac, input, updateType);

            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)");
            }
        }
Ejemplo n.º 3
0
        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)");
            }
        }
Ejemplo n.º 4
0
        /// <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);
        }