Ejemplo n.º 1
0
        /// <summary>
        /// Deserializes a JSON string into a WindowsKey.
        /// This uses a Google specific JSON object containing the
        /// following properties.
        ///
        /// email: the email of the account creator.
        /// expireOn: the date and time in UTC we should stop accepting the key.
        /// exponent: the exponent for encryption.
        /// modulus: a 2048 bit RSA public key modulus.
        /// userName: the name of the user account.
        ///
        /// This format is still subject to change.
        /// Reliance on it in any way is at your own risk.
        /// </summary>
        /// <param name="windowsKey">The serialized WindowsKey.</param>
        /// <returns>
        /// A WindowsKey object or null if deserialization fails.
        /// </returns>
        public static WindowsKey DeserializeWindowsKey(string windowsKey)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(WindowsKeyJson));

            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(windowsKey)))
            {
                try
                {
                    WindowsKeyJson windowsKeyJson = (WindowsKeyJson)serializer.ReadObject(stream);
                    string         expireOn       = windowsKeyJson.ExpireOn;
                    string         exponent       = windowsKeyJson.Exponent;
                    string         modulus        = windowsKeyJson.Modulus;
                    string         userName       = windowsKeyJson.UserName;

                    return(new WindowsKey(expireOn: expireOn, exponent: exponent, modulus: modulus, userName: userName));
                }
                catch (Exception)
                {
                    Logger.Warning("Windows key [{0}] could not be deserialized.", windowsKey);
                    return(null);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Converts a WindowsKey into a serialized JSON string.
        /// </summary>
        /// <returns>The serialized JSON object as a string.</returns>
        public override string ToString()
        {
            WindowsKeyJson windowsKeyJson = new WindowsKeyJson
            {
                Email    = this.Email,
                ExpireOn = this.ExpireOn,
                Exponent = this.Exponent,
                Modulus  = this.Modulus,
                UserName = this.UserName
            };

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(WindowsKeyJson));

            using (MemoryStream stream = new MemoryStream())
            {
                serializer.WriteObject(stream, windowsKeyJson);
                stream.Position = 0;
                using (StreamReader reader = new StreamReader(stream))
                {
                    return(reader.ReadToEnd());
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Converts a WindowsKey into a serialized JSON string.
        /// </summary>
        /// <returns>The serialized JSON object as a string.</returns>
        public override string ToString()
        {
            WindowsKeyJson windowsKeyJson = new WindowsKeyJson
            {
                Email = this.Email,
                ExpireOn = this.ExpireOn,
                Exponent = this.Exponent,
                Modulus = this.Modulus,
                UserName = this.UserName
            };

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(WindowsKeyJson));
            using (MemoryStream stream = new MemoryStream())
            {
                serializer.WriteObject(stream, windowsKeyJson);
                stream.Position = 0;
                using (StreamReader reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }