Hash() public static method

Creates a hash string using the specified hash methodology.
Default encoding is ISO-8859-1
public static Hash ( string text, HashAlgorithm hasher, Encoding encoder = null ) : string
text string String to be hashed
hasher System.Security.Cryptography.HashAlgorithm Hash algorithm with which to perform the hash
encoder System.Text.Encoding (Optional) Character encoding for converting the text string to bytes
return string
コード例 #1
0
        /// <summary>
        /// Decodes a data string.
        /// </summary>
        /// <param name="data">Data to be decoded</param>
        /// <param name="secret">Secret key</param>
        /// <param name="timestamp">Timestamp as an unsigned int</param>
        /// <param name="offset">Offset for local key generation</param>
        /// <returns>Decoded string</returns>
        private static string Decode(string data, string secret, uint timestamp, int offset)
        {
            string md5Key  = HashHelper.Hash(timestamp.ToString() + secret, HashHelper.HashType.MD5);
            string decoded = string.Empty;
            int    length  = CharsToEncode.Length;

            for (int index = 0; index < data.Length; index++)
            {
                int decodeIndex = CharsToEncode.IndexOf(data[index]);
                if (decodeIndex >= 0)
                {
                    int newIndex = GetDecodeIndex(
                        Int32.Parse(md5Key.Substring((offset + index) % md5Key.Length, 1), NumberStyles.AllowHexSpecifier) * 7,
                        length,
                        decodeIndex);

                    decoded += (newIndex < 0) ? '?' : CharsToEncode[newIndex];
                }
                else
                {
                    decoded += data[index];
                }
            }

            return(decoded);
        }
コード例 #2
0
        /// <summary>
        /// Creates the digest portion of the ticket from the provided data.
        /// </summary>
        /// <param name="ticketData">Data used to create the digest</param>
        /// <param name="secret">Secret key used to create the digest</param>
        /// <returns>Digest string</returns>
        /// <remarks>
        /// The algorithm for the digest is as follows:
        ///     digest = MD5(digest0 + key)
        /// where
        ///     Version 1.3: digest0 = MD5(iptstamp + key + user_id + user_data)
        ///     Version 2.0: digest0 = MD5(iptstamp + key + user_id + '\0' + token_list + '\0' + user_data)
        /// </remarks>
        private static string CreateDigest(AuthenticationTicketData ticketData, string secret)
        {
            if (ticketData == null)
            {
                return(null);
            }

            if (string.IsNullOrWhiteSpace(secret))
            {
                secret = DefaultSecret;
            }

            string iptStamp = CreateIPTimeStamp(ticketData.IPAddressAsInt, ticketData.UnixTimeStamp);
            string digest;
            const HashHelper.HashType hashType = HashHelper.HashType.MD5;

            switch (ticketData.Version)
            {
            case "1.3":
                digest = HashHelper.Hash(iptStamp + secret + ticketData.UserId + ticketData.UserData, hashType);
                digest = HashHelper.Hash(digest + secret, hashType);
                break;

            case "2.0":
                digest =
                    HashHelper.Hash(
                        iptStamp + secret + ticketData.UserId + '\0' + ticketData.TokensAsString + '\0' + ticketData.UserData, hashType);
                digest = HashHelper.Hash(digest + secret, hashType);
                break;

            default:
                throw new NotSupportedException(string.Format("Version {0} of the mod_auth_tkt algorithm is not supported",
                                                              ticketData.Version));
            }

            return(digest);
        }
コード例 #3
0
        /// <summary>
        /// Encodes a data string.
        /// </summary>
        /// <param name="data">Data to be encoded</param>
        /// <param name="secret">Secret key</param>
        /// <param name="timestamp">Timestamp as an unsigned int</param>
        /// <param name="offset">Offset for local key generation</param>
        /// <returns>Encoded string</returns>
        private static string Encode(string data, string secret, uint timestamp, int offset)
        {
            string md5Key  = HashHelper.Hash(timestamp.ToString() + secret, hashType: HashHelper.HashType.MD5);
            int    length  = CharsToEncode.Length;
            string encoded = string.Empty;

            for (int index = 0; index < data.Length; index++)
            {
                int encodeIndex = CharsToEncode.IndexOf(data[index]);
                if (encodeIndex >= 0)
                {
                    int newIndex = (encodeIndex +
                                    Int32.Parse(md5Key.Substring((offset + index) % md5Key.Length, 1), NumberStyles.AllowHexSpecifier) * 7) %
                                   length;
                    encoded += CharsToEncode[newIndex];
                }
                else
                {
                    encoded += data[index];
                }
            }

            return(encoded);
        }