Exemple #1
0
        public static string GenerateAuthenticatedMessage(string message, long timestamp, string password)
        {
            if (message.Contains(":[:BR:]:"))
            {
                throw new MessageUnallowedCharsException("Message can't contain :[:BR:]:");
            }
            var prepare = message + ":[:BR:]:" + Convert.ToString(timestamp);

            return(prepare + ":[:BR:]:" + Sha.HashToString(Sha.Sha512Hmac(Sha.Sha512(password), new UTF8Encoding().GetBytes(prepare))));
        }
Exemple #2
0
        public static AuthenticatedMessage AuthenticateMessage(string message, long timestamp, string password)
        {
            if (!message.Contains(":[:BR:]:"))
            {
                throw new MessageAuthenticationFailureException("Malformed message.");
            }
            var msg = message.Split(new string[] { ":[:BR:]:" }, StringSplitOptions.None);

            if (msg.Length < 2 || msg.Length > 3)
            {
                throw new MessageAuthenticationFailureException("Malformed message.");
            }
            if (msg[1] == "Guest")
            {
                return(new AuthenticatedMessage(msg[0], false));
            }
            try
            {
                if (!Time.ValidateTimestamp(timestamp, Convert.ToInt64(msg[1]), 5000))
                {
                    throw new MessageExpiredException();
                }
            }
            catch (MessageExpiredException)
            {
                throw new MessageAuthenticationFailureException();
            }
            catch
            {
                throw new MessageAuthenticationFailureException("Malformed message - timestamp can't be converted to long.");
            }
            if (Sha.HashToString(Sha.Sha512Hmac(Sha.Sha512(password), new UTF8Encoding().GetBytes(msg[0] + ":[:BR:]:" + msg[1]))) != msg[2])
            {
                throw new MessageAuthenticationFailureException("Invalid authentication code.");
            }
            return(string.IsNullOrEmpty(password) || password == "none" ? new AuthenticatedMessage(msg[0], false) : new AuthenticatedMessage(msg[0], true));
        }