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)); }
public static bool EqualHash(byte[] dataToCompare, byte[] hash, HashAlg hashAlg) { return(String.Equals( ComputeHash64(dataToCompare, hashAlg), B2S64(hash) )); }
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))); }
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(), });
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)))); }
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(); } }
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)); }
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()); } }
/// <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); } }
private static byte[] ComputeHash(string data, HashAlg hashAlg) { return(ComputeHash(S2B(data), hashAlg)); }
public static bool EqualHash(string dataToCompare, string hash, HashAlg hashAlg) { return(EqualHash(S2B(dataToCompare), S642B(hash), hashAlg)); }
public static string Get(HashAlg algo) { return Enum.GetName(typeof(HashAlg), algo); }
public static byte[] Hash(byte[] data, HashAlg hashAlg) { return ComputeHash(data, hashAlg); }
private static string ComputeHash64(byte[] data, HashAlg hashAlg) { return B2S64(ComputeHash(data, hashAlg)); }
private static byte[] ComputeHash(byte[] data, HashAlg hashAlg) { return GetAlg(hashAlg).ComputeHash(data); }
private static byte[] ComputeHash(byte[] data, HashAlg hashAlg) { return(GetAlg(hashAlg).ComputeHash(data)); }
private static byte[] ComputeHash(string data, HashAlg hashAlg) { return ComputeHash(S2B(data), hashAlg); }
private static string ComputeHash64(byte[] data, HashAlg hashAlg) { return(B2S64(ComputeHash(data, hashAlg))); }
private static string ComputeHash64(string data, HashAlg hashAlg) { return ComputeHash64(S2B(data), hashAlg); }
private static string ComputeHash64(string data, HashAlg hashAlg) { return(ComputeHash64(S2B(data), hashAlg)); }
public static string Base64Hash(byte[] data, HashAlg hashAlg) { return ComputeHash64(data, hashAlg); }
public static byte[] Hash(byte[] data, HashAlg hashAlg) { return(ComputeHash(data, hashAlg)); }
public static bool EqualHash(string dataToCompare, string hash, HashAlg hashAlg) { return EqualHash(S2B(dataToCompare), S642B(hash), hashAlg); }
public static string Base64Hash(byte[] data, HashAlg hashAlg) { return(ComputeHash64(data, hashAlg)); }