Exemplo n.º 1
0
        public void TestProtectVerifyIntegrity()
        {
            var key = "eda8506c1fb0bbcc3f62626fef074bbf2d09a8c7c608f3fa1482c9a625d00f75".ToByteArray();

            var message = Encoding.ASCII.GetBytes("hoy, how are you?");

            var plaintextAndTag  = Symmetric.ProtectIntegrity(key, message);
            var retrievedMessage = Symmetric.VerifyIntegrity(key, plaintextAndTag);

            if (!message.SequenceEqual(retrievedMessage))
            {
                throw new Exception("Verify did not work");
            }

            // Tamper
            plaintextAndTag[plaintextAndTag.Length - 1] ^= 1;

            bool tamperDetected;

            try
            {
                Symmetric.VerifyIntegrity(key, plaintextAndTag);
                tamperDetected = false;
            }
            catch (Exception)
            {
                tamperDetected = true;
            }

            if (!tamperDetected)
            {
                throw new Exception("Verify did not work");
            }
        }