Aditi.SignatureAuth.Signature.Parse C# (CSharp) Method

Parse() public static method

public static Parse ( string text ) : Signature
text string
return Signature
        public static Signature Parse(string text)
        {
            if (text.Length < 4 || text.Substring(0, 4) != "MAC ")
            {
                throw new ArgumentException(@"Not a valid signature string. Text doesn't start with the string ""MAC "".");
            }

            var components = new NameValueCollection();
            foreach (var pair in text.Substring(4).Split(',').Select(kv => kv.Trim().Split(new char[] { '=' }, 2)))
            {
                if (pair.Length != 2)
                {
                    throw new ArgumentException(@"Invalid Signature String.  Malformed '=' pairs");
                }
                components[pair[0].Trim()] = pair[1].Trim(' ', '"');
            }

            var requiredParameters = new string[] { "id", "nonce", "mac" };
            foreach (var parameter in requiredParameters)
            {
                if (components[parameter] == null)
                {
                    throw new ArgumentException(string.Format(@"Signature string is missing the required parameter ""{0}"".", parameter));
                }
            }

            if (components.Count != requiredParameters.Length)
            {
                throw new ArgumentException(string.Format(@"Signature string is invalid. It contains extra parameters. Expected parameters are: {0}.", string.Join(", ", requiredParameters)));
            }

            var split = components["nonce"].Split(new char [] { ':' }, 2);
            long ticks;

            if (split.Length != 2 || !long.TryParse(split[0], out ticks))
            {
                throw new ArgumentException(@"Nonce is in an invalid format. Nonce should be in the form <timestamp expressed as 100-nanoseconds intervals since 12:00 January 1st, 0001 UTC>:<random string>.");
            }

            return new Signature(components["id"]) { Timestamp = new DateTimeOffset(ticks, TimeSpan.Zero), Nonce = split[1], Mac = components["mac"] };
        }