コード例 #1
0
        private byte[] GetResourceBytes(byte[] buffer)
        {
            byte flags      = 0;
            int  encryptKey = _random.Next(100, int.MaxValue);

            // Compress
            if (buffer.Length > 30)
            {
                buffer = CompressionUtils.GZipCompress(buffer);
                flags |= 1;
            }

            // Encrypt
            StrongCryptoUtils.Encrypt(buffer, encryptKey);

            int pos  = 0;
            var blob = new Blob(buffer.Length + 14);

            blob.Write(ref pos, (int)ResourceSignature);                     // Signature
            blob.Write(ref pos, (int)StrongCryptoUtils.ComputeHash(buffer)); // Hash
            blob.Write(ref pos, (int)encryptKey);                            // Encrypt Key
            blob.Write(ref pos, (byte)flags);                                // Encrypt Key
            blob.Write(ref pos, (byte)0);                                    // Unused
            blob.Write(ref pos, (byte[])buffer);                             // Data

            return(blob.ToArray());
        }
コード例 #2
0
        public int Add(byte[] buffer, int offset, int count, bool encrypt = false, bool compress = false)
        {
            int blobId = NextBlobID;

            byte flags = 0;

            // Compress
            if (compress && count > 30)
            {
                buffer       = CompressionUtils.GZipCompress(buffer, offset, count);
                offset       = 0;
                count        = buffer.Length;
                flags       |= 2;
                _hasCompress = true;
            }

            // Encrypt
            if (encrypt)
            {
                StrongCryptoUtils.Encrypt(buffer, _encryptKey, offset, count);
                flags      |= 1;
                _hasEncrypt = true;
            }

            // Write
            int pos = _blob.Length;

            _blob.Write(ref pos, (byte)flags);
            _blob.Write7BitEncodedInt(ref pos, count);
            _blob.Write(ref pos, buffer, offset, count);

            return(blobId);
        }