예제 #1
0
        private void MacTest()
        {
            byte[] data = new byte[256];
            byte[] hash = new byte[20];

            for (int i = 0; i < 256; i++)
            {
                data[i] = (byte)i;
            }

            using (VMAC mac = new VMAC())
            {
                mac.Initialize(_key, _iv);
                mac.BlockUpdate(data, 0, data.Length);
                mac.DoFinal(hash, 0);
            }

            if (!Evaluate.AreEqual(_expected, hash))
            {
                throw new Exception("VMAC is not equal! Expected: " + HexConverter.ToString(_expected) + " Received: " + HexConverter.ToString(hash));
            }
        }
 /// <summary>
 /// Validates user inputs with the inputed MAC
 /// </summary>
 public void Validate(object obj = null)
 {
     try
     {
         if (VMAC != "")
         {
             byte[] userMAC = new byte[0];
             if (vAscii)
             {
                 // Converts ascii to byte array
                 userMAC = Encoding.ASCII.GetBytes(VMAC);
             }
             else if (vBase64)
             {
                 // Converts base64 to byte array
                 userMAC = Convert.FromBase64String(VMAC);
             }
             else
             {
                 // Converts hex string to byte array
                 string[] hexValuesSplit = VMAC.Split('-');
                 userMAC = new byte[hexValuesSplit.Length];
                 for (int i = 0; i < hexValuesSplit.Length; i++)
                 {
                     userMAC[i] = Convert.ToByte(hexValuesSplit[i], 16);
                 }
             }
             // Validates the inputed mac with the generated mac
             Valid = hashing.Validate(hashed, userMAC);
         }
     }
     catch
     {
         Valid = false;
     }
 }