Example #1
0
        public async Task WriteChecksAndEmbeddedDataAsync()
        {
            OutData.Position = 0;
            await OutData.WriteAsync(BitConverter.GetBytes(MagicNumber), 0, 4);

            await OutData.WriteAsync(BitConverter.GetBytes(DataVersionNumber), 0, 2);

            await OutData.WriteAsync(BitConverter.GetBytes(MinCompatibleDataVersionNumber), 0, 2);

            await OutData.WriteAsync(IV, 0, 16);

            await OutData.WriteAsync(Salt, 0, 32);

            var kcv = KeyCheckValueValidator.GenerateKeyCheckValue(Key);

            OutData.Write(kcv, 0, 19);

            long currentPosition = OutData.Position;
            var  mac             = MessageAuthenticationCodeValidator.CalculateMessageAuthenticationCode(Key, OutData, HeaderSize);

            Debug.Assert(currentPosition == OutData.Position, "Stream position is changed after generating MAC");
            await OutData.WriteAsync(mac, 0, 48);

            OutData.Position = 0;
        }
        public void AlteredKeyCheckValueIsNotValid()
        {
            byte[] key = CryptoRandom.NextBytesStatic(32);
            byte[] kcv = KeyCheckValueValidator.GenerateKeyCheckValue(key);
            if (key[0] > 120)
            {
                key[0]--;
            }
            else
            {
                key[0]++;
            }

            bool exceptionWasThrown = false;

            try
            {
                KeyCheckValueValidator.ValidateKeyCheckValue(key, kcv);
            }
            catch (KeyCheckValueValidationException)
            {
                exceptionWasThrown = true;
            }

            Assert.True(exceptionWasThrown, "Validation of KCV should have failed, but it didn't");
        }
        public void UnalteredKeyCheckValueIsValid()
        {
            byte[] key = CryptoRandom.NextBytesStatic(32);
            byte[] kcv = KeyCheckValueValidator.GenerateKeyCheckValue(key);

            bool exceptionWasThrown = false;

            try
            {
                KeyCheckValueValidator.ValidateKeyCheckValue(key, kcv);
            }
            catch (KeyCheckValueValidationException)
            {
                exceptionWasThrown = true;
            }

            Assert.False(exceptionWasThrown, "Validation of KCV has failed");
        }
 public void KeyCheckValidIs19BytesLong()
 {
     byte[] key = CryptoRandom.NextBytesStatic(32);
     byte[] kcv = KeyCheckValueValidator.GenerateKeyCheckValue(key);
     Assert.True(kcv.Length == 19, "KCV is not 19 bytes long");
 }