예제 #1
0
        public string Serialize(T message)
        {
            Requires.NotNull(message, "message");

            message.UtcCreationDate = DateTime.UtcNow;

            if (this.decodeOnceOnly != null)
            {
                message.Nonce = MessagingUtilities.GetNonCryptoRandomData(NonceLength);
            }

            byte[] encoded = this.SerializeCore(message);

            if (this.compressed)
            {
                encoded = MessagingUtilities.Compress(encoded);
            }

            string symmetricSecretHandle = null;

            if (this.encrypted)
            {
                encoded = this.Encrypt(encoded, out symmetricSecretHandle);
            }

            if (this.signed)
            {
                message.Signature = this.CalculateSignature(encoded, symmetricSecretHandle);
            }

            int capacity = this.signed ? 4 + message.Signature.Length + 4 + encoded.Length : encoded.Length;

            using (var finalStream = new MemoryStream(capacity)) {
                var writer = new BinaryWriter(finalStream);
                if (this.signed)
                {
                    writer.WriteBuffer(message.Signature);
                }

                writer.WriteBuffer(encoded);
                writer.Flush();

                string payload = MessagingUtilities.ConvertToBase64WebSafeString(finalStream.ToArray());
                string result  = payload;
                if (symmetricSecretHandle != null && (this.signed || this.encrypted))
                {
                    result = MessagingUtilities.CombineKeyHandleAndPayload(symmetricSecretHandle, payload);
                }

                return(result);
            }
        }
예제 #2
0
 /// <summary>
 /// Encodes the specified value.
 /// </summary>
 /// <param name="value">The value.  Guaranteed to never be null.</param>
 /// <returns>The <paramref name="value"/> in string form, ready for message transport.</returns>
 public string Encode(object value)
 {
     return(MessagingUtilities.ConvertToBase64WebSafeString((byte[])value));
 }