Пример #1
0
 public static bool EqualHash(byte[] dataToCompare, byte[] hash, HashAlg hashAlg)
 {
     return String.Equals(
         ComputeHash64(dataToCompare, hashAlg),
         B2S64(hash)
         );
 }
        /// <summary>
        /// Computes the computer id hash.
        /// </summary>
        /// <param name="baseString">Base string built by appending one or more identifier elements.</param>
        /// <param name="hashAlg">The <see cref="HashAlg"/> to use.</param>
        /// <returns>The computer identifier hash as a URL safe string.</returns>
        /// <exception cref="NotSupportedException">Thrown if the given <paramref name="hashAlg"/> is not supported.</exception>
        private static string ComputeHash(string baseString, HashAlg hashAlg)
        {
            byte[] hash;
            if (hashAlg == HashAlg.SHA1)
            {
                using (var sha1 = new SHA1Managed())
                {
                    hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(baseString));
                }
            }
            else if (hashAlg == HashAlg.SHA256)
            {
                using (var sha256 = SHA256.Create())
                {
                    hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(baseString));
                }
            }
            else
            {
                var message = string.Format("Hash algorithm {0} not supported", hashAlg);
                throw new NotSupportedException(message);
            }

            return(Base64Url.Encode(hash));
        }
Пример #3
0
 public static bool EqualHash(byte[] dataToCompare, byte[] hash, HashAlg hashAlg)
 {
     return(String.Equals(
                ComputeHash64(dataToCompare, hashAlg),
                B2S64(hash)
                ));
 }
Пример #4
0
 public override int GetHashCode()
 {
     // Since the digest is already a randomized sequence of bytes, we can
     // use only the initial four bytes with the same probability of collision
     // as if we used all bytes of digest to compute 4-byte integer.
     return(Globs.Mix(HashAlg.GetHashCode(), BitConverter.ToInt32(HashData, 0)));
 }
Пример #5
0
 private static HashAlgorithm GetAlg(HashAlg hashAlg)
 {
     return(hashAlg switch
     {
         HashAlg.MD5 => MD5.Create(),
         HashAlg.SHA1 => SHA1.Create(),
         HashAlg.SHA256 => SHA256.Create(),
         HashAlg.SHA512 => SHA512.Create(),
         _ => SHA256.Create(),
     });
Пример #6
0
        static string GetHash(PairwiseSettings settings, string input)
        {
            if (!settings.CanCache)
            {
                return(Guid.NewGuid().ToString());
            }

            string both = settings.GetPictArgs() + input;

            return(Convert.ToBase64String(HashAlg.ComputeHash(EncodingForHashAlgorithm.GetBytes(both))));
        }
Пример #7
0
 private static HashAlgorithm GetAlg(HashAlg hashAlg)
 {
     switch (hashAlg)
     {
         case HashAlg.MD5:
             return MD5.Create();
         case HashAlg.SHA1:
             return SHA1.Create();
         case HashAlg.SHA256:
             return SHA256.Create();
         case HashAlg.SHA512:
             return SHA512.Create();
         default:
             return SHA256.Create();
     }
 }
Пример #8
0
        public string?CreateHash(string value, HashAlg alg)
        {
            string?result = null;

            switch (alg)
            {
            case HashAlg.Sha256:
                result = value.ToSha256();
                break;

            case HashAlg.Sha512:
                result = value.ToSha512();
                break;
            }

            return(result);
        }
        /// <summary>
        /// Builds a hash of selected computer identifiers.
        /// </summary>
        /// <param name="customId">You own custom value to as a component of the computer identifier, may represent a single
        /// identifier or multiple identifiers concatenated. <c>null</c> for no custom / caller-specified computer id.</param>
        /// <param name="salt">Application specific salt for computing the hash, or <c>null</c> for no application-specific salt.</param>
        /// <param name="identifyBy"><see cref="ComputerIdentifier"/> values to use as components for the computer identifier.</param>
        /// <returns>The computer identifier hash as a URL safe string.</returns>
        public static string BuildComputerId(string customId, string salt, HashAlg hashAlg, params ComputerIdentifier[] identifyBy)
        {
            var baseString = new StringBuilder();

            baseString.Append(ComputerIdentity.ComputerIdSalt);

            if (!string.IsNullOrEmpty(customId))
            {
                baseString.Append(customId);
            }

            foreach (var requestedIdentifier in identifyBy)
            {
                switch (requestedIdentifier)
                {
                case ComputerIdentifier.BaseboardSerialNumber:
                    baseString.Append(GetBaseboardSerialNumber());
                    break;

                case ComputerIdentifier.ComputerSystemProductUuid:
                    baseString.Append(GetComputerSystemProductUuid());
                    break;

                case ComputerIdentifier.WindowsDigitalProductId:
                    baseString.Append(GetWindowsDigitalProductId());
                    break;

                case ComputerIdentifier.WindowsProductId:
                    baseString.Append(GetWindowsProductId());
                    break;

                default:
                    throw new NotSupportedException(string.Format("Computer identity value {0} not supported", requestedIdentifier.ToString()));
                }
            }

            if (!string.IsNullOrEmpty(salt))
            {
                baseString.Append(salt);
            }

            return(ComputeHash(baseString.ToString(), hashAlg));
        }
Пример #10
0
        private static HashAlgorithm GetAlg(HashAlg hashAlg)
        {
            switch (hashAlg)
            {
            case HashAlg.MD5:
                return(MD5.Create());

            case HashAlg.SHA1:
                return(SHA1.Create());

            case HashAlg.SHA256:
                return(SHA256.Create());

            case HashAlg.SHA512:
                return(SHA512.Create());

            default:
                return(SHA256.Create());
            }
        }
Пример #11
0
 /// <summary>
 /// Creates a new crypto service provider instance of algo.
 /// 
 /// Exception: RIPEMD160, that is managed.
 /// </summary>
 /// <param name="algo">The algorithm to create an equivalent instance of.</param>
 /// <returns>The new instance.</returns>
 public static HashAlgorithm GetInstance(HashAlg algo)
 {
     switch(algo)
     {
         case HashAlg.SHA1:
             return new SHA1CryptoServiceProvider();
         case HashAlg.SHA256:
             return new SHA256CryptoServiceProvider();
         case HashAlg.SHA384:
             return new SHA384CryptoServiceProvider();
         case HashAlg.SHA512:
             return new SHA512CryptoServiceProvider();
         case HashAlg.MD5:
             return new MD5CryptoServiceProvider();
         case HashAlg.RIPEMD160:
             return new RIPEMD160Managed();
         default:
             throw new UnknownAlgorithmException("uncatched algorithm " + algo);
     }
 }
Пример #12
0
 private static byte[] ComputeHash(string data, HashAlg hashAlg)
 {
     return(ComputeHash(S2B(data), hashAlg));
 }
Пример #13
0
 public static bool EqualHash(string dataToCompare, string hash, HashAlg hashAlg)
 {
     return(EqualHash(S2B(dataToCompare), S642B(hash), hashAlg));
 }
Пример #14
0
 public static string Get(HashAlg algo)
 {
     return Enum.GetName(typeof(HashAlg), algo);
 }
Пример #15
0
 public static byte[] Hash(byte[] data, HashAlg hashAlg)
 {
     return ComputeHash(data, hashAlg);
 }
Пример #16
0
 private static string ComputeHash64(byte[] data, HashAlg hashAlg)
 {
     return B2S64(ComputeHash(data, hashAlg));
 }
Пример #17
0
 private static byte[] ComputeHash(byte[] data, HashAlg hashAlg)
 {
     return GetAlg(hashAlg).ComputeHash(data);
 }
Пример #18
0
 private static byte[] ComputeHash(byte[] data, HashAlg hashAlg)
 {
     return(GetAlg(hashAlg).ComputeHash(data));
 }
Пример #19
0
 private static byte[] ComputeHash(string data, HashAlg hashAlg)
 {
     return ComputeHash(S2B(data), hashAlg);
 }
Пример #20
0
 private static string ComputeHash64(byte[] data, HashAlg hashAlg)
 {
     return(B2S64(ComputeHash(data, hashAlg)));
 }
Пример #21
0
 private static string ComputeHash64(string data, HashAlg hashAlg)
 {
     return ComputeHash64(S2B(data), hashAlg);
 }
Пример #22
0
 private static string ComputeHash64(string data, HashAlg hashAlg)
 {
     return(ComputeHash64(S2B(data), hashAlg));
 }
Пример #23
0
 public static string Base64Hash(byte[] data, HashAlg hashAlg)
 {
     return ComputeHash64(data, hashAlg);
 }
Пример #24
0
 public static byte[] Hash(byte[] data, HashAlg hashAlg)
 {
     return(ComputeHash(data, hashAlg));
 }
Пример #25
0
 public static bool EqualHash(string dataToCompare, string hash, HashAlg hashAlg)
 {
     return EqualHash(S2B(dataToCompare), S642B(hash), hashAlg);
 }
Пример #26
0
 public static string Base64Hash(byte[] data, HashAlg hashAlg)
 {
     return(ComputeHash64(data, hashAlg));
 }