本文整理汇总了C#中this.ComputeHash方法的典型用法代码示例。如果您正苦于以下问题:C# this.ComputeHash方法的具体用法?C# this.ComputeHash怎么用?C# this.ComputeHash使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类this
的用法示例。
在下文中一共展示了this.ComputeHash方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FromFile
/// <summary>
/// Get the MD5 hash value as a HEX-string
/// </summary>
/// <param name="md5">Instance of MD5</param>
/// <param name="filepath">The input filepath for the hash code is to be calculated. </param>
/// <returns>The calculated hash code as Hex-string</returns>
public static string FromFile(this MD5 md5, string filepath)
{
using (var stream = File.OpenRead(filepath))
{
return md5.ComputeHash(stream).ToHexString();
}
}
示例2: ComputeFileHash
public static byte[] ComputeFileHash(this MD5CryptoServiceProvider md5, string filename)
{
using (var fileStream = new FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
{
return md5.ComputeHash(fileStream);
}
}
示例3: ComputeHashString
/// <summary>Computes the hash value for the specified <see cref="String" /> <paramref name="input"/>.</summary>
/// <param name="md5">The <see cref="MD5" /> instance that will perform the hash computation.</param>
/// <param name="input">A <see cref="String" /> value.</param>
/// <param name="encoding">An <see cref="Encoding" /> object that will be used to encode <paramref name="input" /> to bytes.</param>
/// <returns>A <see cref="String" /> containing the computed hash of <paramref name="input"/>.</returns>
public static String ComputeHashString(this MD5 md5, String input, Encoding encoding)
{
Byte[] inputBytes = encoding.GetBytes(input);
var hashBuilder = new StringBuilder();
List<Byte> hashBytes = md5.ComputeHash(inputBytes).ToList();
hashBytes.ForEach(hashByte => hashBuilder.AppendFormat("{0:x2}", hashByte));
return hashBuilder.ToString();
}
示例4: ComputeHash
public static string ComputeHash(this ICryptoService cryptoService, string data, string salt)
{
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
byte[] saltBytes = Convert.FromBase64String(salt);
byte[] hashBytes = cryptoService.ComputeHash(dataBytes, saltBytes);
return Convert.ToBase64String(hashBytes);
}
示例5: ComputeHash
public static string ComputeHash(this HashAlgorithm Hash, string Buffer)
{
StringBuilder digestString = new StringBuilder();
foreach (byte b in Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(Buffer)))
digestString.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
return digestString.ToString();
}
示例6: ComputeHashAsString
public static string ComputeHashAsString(this HashAlgorithm hashAlgorithm, byte[] buffer)
{
var hashBytes = hashAlgorithm.ComputeHash(buffer);
var builder = new StringBuilder();
foreach (var @byte in hashBytes) {
builder.Append(@byte.ToString("x"));
}
return builder.ToString();
}
示例7: ComputeStringHash
public static string ComputeStringHash(this HashAlgorithm algorithm, byte[] content)
{
var computedHash = algorithm.ComputeHash(content);
var sb = new StringBuilder();
for (var i = 0; i < computedHash.Length; i++)
{
sb.Append(computedHash[i].ToString("x2"));
}
return sb.ToString();
}
示例8: ComputeHash
/// <summary>
/// Computes the hash value for the specified string.
/// </summary>
/// <param name="hashType">The type of algorithm to use when computing the hash.</param>
/// <param name="value">The input to compute the hash code for.</param>
/// <param name="encoding">
/// The encoding to use when retrieving the binary representation of <paramref name="value"/>.
/// If null or not supplied, then <see cref="Encoding.UTF8"/> is used instead.
/// </param>
/// <returns>The computed hash code.</returns>
public static string ComputeHash(this HashType hashType, string value, Encoding encoding = null)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
encoding = encoding ?? Encoding.UTF8;
return hashType.ComputeHash(encoding.GetBytes(value));
}
示例9: GetHash
// MD5 Hash object to string conversion
public static string GetHash(this MD5 str, string pass)
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(pass);
byte[] hash = str.ComputeHash(inputBytes);
// Convert byte array to hex string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
示例10: ComputeHash
public static string ComputeHash(this SHA1 sha1, string input, Encoding encoding)
{
// step 1, calculate SHA1 hash from input
var inputBytes = encoding.GetBytes(input);
var hash = sha1.ComputeHash(inputBytes);
// step 2, convert byte array to hex string
var sb = new StringBuilder();
foreach (var t in hash)
{
sb.Append(t.ToString("X2"));
}
return sb.ToString();
}
示例11: GetHashedString
public static string GetHashedString(this HashAlgorithm algorithm, string value)
{
if (string.IsNullOrEmpty(value)) return null;
var hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(value));
var sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
示例12: GetHash
public static string GetHash(this HashAlgorithm alg, string text)
{
string result = null;
// Convert plain text into a byte array.
byte[] textBytes = Encoding.UTF8.GetBytes(text);
byte[] hashBytes = alg.ComputeHash(textBytes);
// Convert result into a base64-encoded string.
result = Convert.ToBase64String(hashBytes);
return result;
}
示例13: HashString
public static string HashString(this System.Security.Cryptography.HashAlgorithm algo, string value, System.Text.Encoding encoding = null)
{
if (encoding == null) encoding = System.Text.Encoding.Default;
algo.Initialize();
byte[] hashBytes = algo.ComputeHash(encoding.GetBytes(value));
StringBuilder sbHash = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sbHash.Append(hashBytes[i].ToString("x").ToLower().PadLeft(2, '0'));
}
return sbHash.ToString();
}
示例14: ComputeIntHash
public static unsafe int ComputeIntHash(this HashAlgorithm alg, byte[] dat)
{
int h;
byte[] hash = alg.ComputeHash(dat);
fixed(byte* barr = hash)
{
int* iarr = (int*)barr;
h = iarr[0];
for (int i = 1; i < hash.Length >> 2; i++)
{
h ^= iarr[i];
}
}
return h;
}
示例15: ComputeHash
public static byte[] ComputeHash(this HashAlgorithm hashAlgorithm, string fileName)
{
if (hashAlgorithm == null)
throw new ArgumentNullException("hashAlgorithm");
if (fileName == null)
throw new ArgumentNullException("filename");
if (!File.Exists(fileName))
throw new FileNotFoundException("Unable to locate file", fileName);
using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
return hashAlgorithm.ComputeHash(stream);
}
}