Beispiel #1
0
        /// <summary>
        /// Returns hash characters based on digits passed to it.
        /// </summary>
        /// <param name="digits">Digits to checksum.</param>
        /// <param name="returnAllDigits">If true, all digits are to be returned instead of only hash.</param>
        /// <exception cref="System.ArgumentNullException">Digits cannot be null.</exception>
        public static string ComputeHash(string digits, bool returnAllDigits)
        {
            if (digits == null)
            {
                throw new ArgumentNullException(nameof(digits), "Digits cannot be null.");
            }
            digits = digits.Replace(" ", "").Replace("-", ""); //ignore dashes and spaces

            using (var checksum = new Damm()) {
                checksum.ComputeHash(Encoding.ASCII.GetBytes(digits));
                if (returnAllDigits)
                {
                    return(digits + checksum.HashAsChar.ToString());
                }
                else
                {
                    return(checksum.HashAsChar.ToString());
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Returns if hash matches digits.
        /// </summary>
        /// <param name="digitsWithHash">Digits with appended hash.</param>
        /// <exception cref="System.ArgumentNullException">Digits cannot be null.</exception>
        public static bool ValidateHash(string digitsWithHash)
        {
            if (digitsWithHash == null)
            {
                throw new ArgumentNullException(nameof(digitsWithHash), "Digits cannot be null.");
            }
            digitsWithHash = digitsWithHash.Replace(" ", "").Replace("-", ""); //ignore dashes and spaces
            if (digitsWithHash.Length < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(digitsWithHash), "Digits cannot be less than one character long.");
            }

            var digits = digitsWithHash.Substring(0, digitsWithHash.Length - 1);
            var hash   = digitsWithHash[digitsWithHash.Length - 1];

            using (var checksum = new Damm()) {
                checksum.ComputeHash(Encoding.ASCII.GetBytes(digits));
                return(hash == checksum.HashAsChar);
            }
        }
Beispiel #3
0
 /// <summary>
 /// Returns hash characters based on digits passed to it.
 /// </summary>
 /// <param name="digits">Digits to checksum.</param>
 /// <exception cref="System.ArgumentNullException">Digits cannot be null.</exception>
 public static string ComputeHash(string digits)
 {
     return(Damm.ComputeHash(digits, returnAllDigits: false));
 }