Exemplo n.º 1
0
        private static string GetVersionNumber(byte[] byteStream)
        {
            var versionNumber = ByteHelpers.CreateSpecialByteArray(2);

            Buffer.BlockCopy(byteStream, 0, versionNumber, 0, 2);

            var version = $"{versionNumber[0]}.{versionNumber[1]}";

            return(version);
        }
Exemplo n.º 2
0
        public byte[] Load(byte[] byteStream)
        {
            if (byteStream == null)
            {
                throw new ArgumentNullException(nameof(byteStream));
            }

            var versionNumber = ByteHelpers.CreateSpecialByteArray(2);
            var hash          = ByteHelpers.CreateSpecialByteArray(32);
            var encrypted     = ByteHelpers.CreateSpecialByteArray((byteStream.Length - 34));

            SplitFileIntoChunks(byteStream, versionNumber, hash, encrypted);
            CheckFileIntegrity(hash, encrypted);

            return(DecryptData(encrypted));
        }
Exemplo n.º 3
0
        private static byte[] CheckHmac(byte[] dataToDecrypt, byte[] salt, AesCryptoServiceProvider aes)
        {
            var hmac          = ByteHelpers.CreateSpecialByteArray(32);
            var encryptedData = ByteHelpers.CreateSpecialByteArray(dataToDecrypt.Length - 32);

            Buffer.BlockCopy(dataToDecrypt, 0, hmac, 0, 32);
            Buffer.BlockCopy(dataToDecrypt, 32, encryptedData, 0, dataToDecrypt.Length - 32);

            var newHmac = CreateHmac(salt, aes, encryptedData);

            if (!ByteHelpers.ByteArrayCompare(hmac, newHmac))
            {
                throw new CryptographicException("The authenticated message code doesn't match. \n\nThe message may have been corrupted or tampered with.");
            }

            return(encryptedData);
        }
Exemplo n.º 4
0
 public void CreateSpecialByteArrayCreateArayOfSize10()
 {
     byte[] array = ByteHelpers.CreateSpecialByteArray(10);
     Assert.AreEqual(10, array.Length);
 }
Exemplo n.º 5
0
 public void CreateSpecialByteArrayThrowsInvalidOperationExceptionIfLengthIsZero()
 {
     ByteHelpers.CreateSpecialByteArray(0);
 }