Example #1
0
        /// <summary>
        /// Decrypts the array block.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public byte[] DecryptArrayBlock(byte[] data)
        {
            var bBloblSize = new byte[Marshal.SizeOf(typeof(int))];

            byte[] bEncryptedNonce;
            byte[] bEncryptedData;

            using (var ms = new MemoryStream(data))
            {
                ms.Read(bBloblSize, 0, bBloblSize.Length);
                bEncryptedNonce = new byte[BitConverter.ToInt32(bBloblSize, 0)];
                ms.Read(bEncryptedNonce, 0, bEncryptedNonce.Length);

                ms.Read(bBloblSize, 0, bBloblSize.Length);
                bEncryptedData = new byte[BitConverter.ToInt32(bBloblSize, 0)];
                ms.Read(bEncryptedData, 0, bEncryptedData.Length);
            }

            NonceContainer nonceContainer;

            using (var ff = new FastBinaryFormatter())
            {
                nonceContainer = ff.Deserialize <NonceContainer>(DecryptBytes(bEncryptedNonce));
            }

            return(CryptoServices.AesDecrypt(bEncryptedData, nonceContainer.Nonce));
        }
Example #2
0
        public void SerializaAndDeserializeAllObjectTypes()
        {
            INamedEntity[] serializedObject = BuildAlbumArray();

            var formatter = new FastBinaryFormatter();

            var surrogateSelector = new SurrogateSelector();

            surrogateSelector.AddSurrogate(typeof(Genre), new StreamingContext(), new GenreSerializationSurrogate());

            formatter.SurrogateSelector.ChainSelector(surrogateSelector);

            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, serializedObject);

                stream.Seek(0, SeekOrigin.Begin);

                var deserializedObject = (INamedEntity[])formatter.Deserialize(stream);
                Assert.AreEqual(stream.Position, stream.Length);

                foreach (Album album in deserializedObject)
                {
                    var albumGenre = album.Genre;

                    if (albumGenre != null)
                    {
                        Assert.AreSame(albumGenre, Genre.Get(albumGenre.Key));
                    }

                    Assert.AreEqual(album.Edition.MajorNumber, 1);
                    Assert.AreEqual(album.Edition.ReleaseDate, new DateTime(2017, 11, 06));
                }
            }
        }
Example #3
0
        public void TestDateTime()
        {
            var now       = DateTime.Now;
            var formatter = new FastBinaryFormatter();

            using (var stream = new MemoryStream()) {
                formatter.Serialize(stream, now);
                stream.Seek(0, SeekOrigin.Begin);
                var deserializedObject = (DateTime)formatter.Deserialize(stream);

                Assert.AreEqual(deserializedObject, now);
                Assert.AreEqual(stream.Position, stream.Length);
            }
        }
        private void RewriteFileIgnoreRequestor(RIFileHeader newHeader, FastBinaryFormatter ff, UInt32 sessionID, UInt32 requestID)
        {
            RIFileHeader tmpHeader = null;
            String       tmpFile   = String.Format("{0}.tmp", FFilePath);

            using (FileStream tmpStream = FileStreamAccess.OpenFileStreamForWriting(tmpFile, FileMode.Create))
            {
                // original file: set file pointer to top
                FFileStream.Seek(0, SeekOrigin.Begin);
                ff.Serialize(FFileStream, FFileHeader);

                // original file: move file pointer to the end of the file header
                FFileStream.Seek(0, SeekOrigin.Begin);
                ff.Deserialize <RIFileHeader>(FFileStream);

                // temp file: move file pointer to the end of the file header with new header info
                tmpHeader = newHeader;
                tmpHeader.FInitDateTime   = DateTime.MinValue;
                tmpHeader.FFirstDateTime  = DateTime.MinValue;
                tmpHeader.FLastDateTime   = DateTime.MinValue;
                tmpHeader.FNextSequenceId = FFileHeader.FNextSequenceId;
                tmpHeader.FMessageCount   = 0;
                ff.Serialize(tmpStream, tmpHeader);

                // start copying original messages to temp file
                for (Int32 i = 0; i < FFileHeader.FMessageCount; i++)
                {
                    ReflectInsightPackage message = ff.Deserialize <ReflectInsightPackage>(FFileStream);
                    if (message.FRequestID == requestID && message.FSessionID == sessionID)
                    {
                        continue;
                    }

                    if (tmpHeader.FInitDateTime == DateTime.MinValue)
                    {
                        tmpHeader.FInitDateTime  = message.FDateTime;
                        tmpHeader.FFirstDateTime = message.FDateTime;
                        tmpHeader.FLastDateTime  = message.FDateTime;
                    }

                    tmpHeader.FMessageCount++;
                    tmpHeader.FLastDateTime = message.FDateTime;
                    ff.Serialize(tmpStream, message);
                }

                // update the FileHeader with the temp file header and write it out
                // with latest info.
                FFileHeader = tmpHeader;
                tmpStream.Seek(0, SeekOrigin.Begin);
                ff.Serialize(tmpStream, FFileHeader);
            }

            if (tmpHeader != null)
            {
                // delete the original file first, then copy temp to original
                DeleteFile(false);
                try
                {
                    File.Move(tmpFile, FFilePath);
                }
                finally
                {
                    OpenFileStream();
                }
            }
        }