Beispiel #1
0
        public string Serialize(object stateGraph)
        {
            string       str          = null;
            MemoryStream memoryStream = GetMemoryStream();

            try
            {
                this.Serialize(memoryStream, stateGraph);
                memoryStream.SetLength(memoryStream.Position);
                byte[] buf    = memoryStream.GetBuffer();
                int    length = (int)memoryStream.Length;
                if ((this._page != null) && this._page.RequiresViewStateEncryptionInternal)
                {
                    buf    = MachineKeySection.EncryptOrDecryptData(true, buf, this.GetMacKeyModifier(), 0, length);
                    length = buf.Length;
                }
                else if (((this._page != null) && this._page.EnableViewStateMac) || (this._macKeyBytes != null))
                {
                    buf = MachineKeySection.GetEncodedData(buf, this.GetMacKeyModifier(), 0, ref length);
                }
                str = Convert.ToBase64String(buf, 0, length);
            }
            finally
            {
                ReleaseMemoryStream(memoryStream);
            }
            return(str);
        }
Beispiel #2
0
        internal /*public*/ void CompleteTransforms(TextWriter output, bool enableMac, byte[] macKey)
        {
            int len = 0;

            // convert to bytes
            if (_recyclable)
            {
                // still using the original recyclable char buffer
                // -- can use recyclable byte buffer
                _byteBuffer = (byte[])_byteBufferAllocator.GetBuffer();

                if (_freePos > 0)
                {
                    len = Encoding.UTF8.GetBytes(_charBuffer, 0, _freePos, _byteBuffer, 0);
                }

                // do the mac encoding if requested
                if (enableMac)
                {
                    // the size of the output array depends on the key length and encryption type
                    // so we can't use the recyclable buffers after this
                    byte[] data       = MachineKeySection.GetEncodedData(_byteBuffer, macKey, 0, ref len);
                    string serialized = Convert.ToBase64String(data, 0, len);
                    output.Write(serialized);
                }
                else
                {
                    char[] base64chars = (char[])_charBufferAllocatorBase64.GetBuffer();
                    len = Convert.ToBase64CharArray(_byteBuffer, 0, len, base64chars, 0);
                    output.Write(base64chars, 0, len);
                    _charBufferAllocatorBase64.ReuseBuffer(base64chars);
                }
            }
            else
            {
                _byteBuffer = Encoding.UTF8.GetBytes(_charBuffer, 0, _freePos);


                len = _byteBuffer.Length;
                if (enableMac)
                {
                    _byteBuffer = MachineKeySection.GetEncodedData(_byteBuffer, macKey, 0, ref len);
                }

                string serialized = Convert.ToBase64String(_byteBuffer);
                output.Write(serialized);
            }
        }