private bool IsValidFile(Stream stream, ref byte[] contentBytes)
        {
            bool valid = false;

            string md5Hash       = null;
            int    headerLength  = 0;
            int    contentLength = 0;
            bool   validHeader   = SaveUtilities.DeserializeHeader(stream, ref headerLength, ref m_modifiedTime, ref m_deviceName, ref md5Hash, ref contentLength);

            if (validHeader)
            {
                contentBytes = new byte[contentLength];
                int contentRead = stream.Read(contentBytes, 0, contentLength);

                if (contentRead == contentLength)
                {
                    string contentHash = SaveUtilities.CalculateMD5Hash(contentBytes);

                    if (md5Hash == contentHash)
                    {
                        valid = true;
                    }
                    else
                    {
                        Debug.LogError(string.Format("SaveData :: MD5 Checksum Failed (Got {0} expected {1})", md5Hash, contentHash));
                    }
                }
                else
                {
                    Debug.LogError(string.Format("SaveData :: Content length different to expected (Got {0} expected {1})", contentRead, contentLength));
                }
            }
            else
            {
                Debug.LogError("SaveData :: Invalid header detected!");
            }

            return(valid);
        }
        public MemoryStream SaveToStream()
        {
            MemoryStream stream = null;
            string       json   = m_saveData.ToJSON();

            //Compress the data using LZF compression
            byte[] compressed = null;
            using (MemoryStream compMemStream = new MemoryStream())
            {
                using (StreamWriter writer = new StreamWriter(compMemStream, Encoding.UTF8))
                {
                    writer.Write(json);
                    writer.Close();
                    compressed = CLZF2.Compress(compMemStream.ToArray());
                }
            }
            if (compressed != null)
            {
                byte[] encrypted = AESEncryptor.Encrypt(m_cryptoKey, m_cryptoIV, compressed);

                if (encrypted != null)
                {
                    stream = new MemoryStream();
                    //Write version and headers!
                    byte[] version = SaveUtilities.SerializeVersion(HeaderVersion);
                    stream.Write(version, 0, version.Length);
                    byte[] header = SaveUtilities.SerializeHeader(m_modifiedTime, m_deviceName, SaveUtilities.CalculateMD5Hash(encrypted), encrypted.Length);
                    stream.Write(header, 0, header.Length);
                    //Write encrypted and compressed data to stream
                    stream.Write(encrypted, 0, encrypted.Length);
                }
            }
            return(stream);
        }