Ejemplo n.º 1
0
        private void longBlockTest()
        {
            byte[] data = new byte[16000];
            byte[] res  = new byte[32];

            for (int i = 0; i != data.Length; i++)
            {
                data[i] = (byte)i;
            }

            for (int i = 10000; i != data.Length; i++)
            {
                CShakeDigest cshake_ = new CShakeDigest(128, new byte[0], Arrays.CopyOfRange(data, 0, i));

                cshake_.BlockUpdate(Hex.Decode("00010203"), 0, 4);

                cshake_.DoFinal(res, 0);
            }


            CShakeDigest cshake = new CShakeDigest(256, new byte[0], new byte[200]);

            cshake.BlockUpdate(Arrays.CopyOfRange(data, 0, 200), 0, 200);

            cshake.DoFinal(res, 0);

            Assert.IsTrue(Arrays.AreEqual(Hex.Decode("4a899b5be460d85a9789215bc17f88b8f8ac049bd3b519f561e7b5d3870dafa3"), res));
        }
Ejemplo n.º 2
0
        public int DoFinal(byte[] output, int outOff)
        {
            if (firstOutput)
            {
                if (!initialised)
                {
                    throw new InvalidOperationException("KMAC not initialized");
                }

                byte[] encOut = XofUtilities.RightEncode(GetMacSize() * 8);

                cshake.BlockUpdate(encOut, 0, encOut.Length);
            }

            int rv = cshake.DoFinal(output, outOff, GetMacSize());

            Reset();

            return(rv);
        }
        private void CalculateDigest(ulong[] words)
        {
            var hasher = new CShakeDigest(256, null, Encoding.ASCII.GetBytes("ROM_CTRL"));

            foreach (var word in words)
            {
                hasher.BlockUpdate(BitConverter.GetBytes(word), 0, 8);
            }

            hasher.DoFinal(digest, 0, digest.Length);
            CheckDigest();
        }
Ejemplo n.º 4
0
        private void doFinalTest()
        {
            CShakeDigest cshake = new CShakeDigest(128, new byte[0], Strings.ToByteArray("Email Signature"));

            cshake.BlockUpdate(Hex.Decode("00010203"), 0, 4);

            byte[] res = new byte[32];

            cshake.DoOutput(res, 0, res.Length);

            Assert.IsTrue(Arrays.AreEqual(Hex.Decode("c1c36925b6409a04f1b504fcbca9d82b4017277cb5ed2b2065fc1d3814d5aaf5"), res));

            cshake.DoOutput(res, 0, res.Length);

            Assert.IsTrue(!Arrays.AreEqual(Hex.Decode("c1c36925b6409a04f1b504fcbca9d82b4017277cb5ed2b2065fc1d3814d5aaf5"), res));

            cshake.DoFinal(res, 0, res.Length);

            cshake.BlockUpdate(Hex.Decode("00010203"), 0, 4);

            cshake.DoFinal(res, 0, res.Length);

            string s = Hex.ToHexString(res);

            Console.WriteLine(s);

            Assert.IsTrue(Arrays.AreEqual(Hex.Decode("c1c36925b6409a04f1b504fcbca9d82b4017277cb5ed2b2065fc1d3814d5aaf5"), res));

            cshake.BlockUpdate(Hex.Decode("00010203"), 0, 4);

            cshake.DoOutput(res, 0, res.Length);

            Assert.IsTrue(Arrays.AreEqual(Hex.Decode("c1c36925b6409a04f1b504fcbca9d82b4017277cb5ed2b2065fc1d3814d5aaf5"), res));

            cshake.DoFinal(res, 0, res.Length);

            Assert.IsTrue(Arrays.AreEqual(Hex.Decode("9cbce830079c452abdeb875366a49ebfe75b89ef17396e34898e904830b0e136"), res));
        }
Ejemplo n.º 5
0
        private void checkSHAKE(int bitSize, CShakeDigest cshake, byte[] msg)
        {
            ShakeDigest ref_ = new ShakeDigest(bitSize);

            ref_.BlockUpdate(msg, 0, msg.Length);
            cshake.BlockUpdate(msg, 0, msg.Length);

            byte[] res1 = new byte[32];
            byte[] res2 = new byte[32];

            ref_.DoFinal(res1, 0, res1.Length);
            cshake.DoFinal(res2, 0, res2.Length);

            Assert.IsTrue(Arrays.AreEqual(res1, res2));
        }
Ejemplo n.º 6
0
        private static void Main()
        {
            Console.WriteLine("Hello World!");

            const int hashchainLength = 1000_000;

            //해시체인 시드
            var randomBytes = new byte[1000];

            using var c = new RNGCryptoServiceProvider();
            //시드 생성
            c.GetBytes(randomBytes);

            const int    outputLength  = 25;
            const string functionName  = "hello";
            const string customization = "world";

            var cShakeDigest =
                new CShakeDigest(256,
                                 Encoding.ASCII.GetBytes(functionName),
                                 Encoding.ASCII.GetBytes(customization));

            var previousHash = randomBytes;

            for (int i = 0; i < hashchainLength; i++)
            {
                cShakeDigest.Reset();
                cShakeDigest.BlockUpdate(previousHash, 0, previousHash.Length);
                var hash = new byte[outputLength];
                cShakeDigest.DoFinal(hash, 0, outputLength);
                previousHash = hash;
                // var hashString = BitConverter.ToString(hash).Replace("-", string.Empty).ToLowerInvariant();
                // Console.WriteLine($"{i} {hashString}");
            }

            Console.WriteLine("done");
        }