本文整理汇总了C#中System.Security.Cryptography.MD5CryptoServiceProvider.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# MD5CryptoServiceProvider.Dispose方法的具体用法?C# MD5CryptoServiceProvider.Dispose怎么用?C# MD5CryptoServiceProvider.Dispose使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.MD5CryptoServiceProvider
的用法示例。
在下文中一共展示了MD5CryptoServiceProvider.Dispose方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetHash
public static string GetHash(string text)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
StringBuilder Hash = new StringBuilder();
foreach (byte b in hash)
{
Hash.AppendFormat("{0:X2}", b);
}
md5.Dispose();
return Hash.ToString();
}
示例2: Encrypt_static
/// <summary>
/// MD5CryptoServiceProvider类的哈希大小为 128 位
/// 此类型的任何公共static成员都是线程安全的。但不保证所有实例成员都是线程安全的
/// </summary>
private static string Encrypt_static(string clearText, Encoding encode)
{
MD5 md5 = null;
try
{
byte[] originalBytes = encode.GetBytes(clearText); //Encoding.UTF8.GetBytes(clearText);
md5 = new MD5CryptoServiceProvider();
byte[] data = md5.ComputeHash(originalBytes);
StringBuilder builder = new StringBuilder();
foreach (var item in data)
{
builder.Append(item.ToString("X2"));
}
return builder.ToString();
}
catch (ArgumentNullException) { return clearText; }
catch (EncoderFallbackException) { return clearText; }
//catch (TargetInvocationException) { return clearText; }
catch (ObjectDisposedException) { return clearText; }
catch (InvalidOperationException) { return clearText; }
catch { return clearText; }
finally
{
if (md5 != null)
{
md5.Clear();
md5.Dispose();
}
md5 = null;
}
//return "";
}
示例3: ComputeHash
//.........这里部分代码省略.........
/// Name of the hash algorithm. Allowed values are: "MD5", "SHA1",
/// "SHA256", "SHA384", "SHA512", "HMACMD5", "HMACSHA1", "HMACSHA256",
/// "HMACSHA512" (if any other value is specified MD5 will be used).
///
/// HMAC algorithms uses Hash-based Message Authentication Code.
/// The HMAC process mixes a secret key with the message data, hashes
/// the result with the hash function, mixes that hash value with
/// the secret key again, and then applies the hash function
/// a second time. HMAC hashes are fixed lenght and generally
/// much longer than non-HMAC hashes of the same type.
///
/// https://msdn.microsoft.com/en-us/library/system.security.cryptography.hmacsha256(v=vs.110).aspx
///
/// This value is case-insensitive.
/// </param>
/// <param name="saltBytes">
/// Optional but recommended salt bytes to apply to the hash. If not passed the
/// raw encoding is used. If salt is nullthe raw algorithm is used (useful for
/// file hashes etc.) HMAC versions REQUIRE that salt is passed.
/// </param>
/// <param name="useBinHex">if true returns the data as BinHex byte pair string. Otherwise Base64 is returned.</param>
/// <returns>
/// Hash value formatted as a base64-encoded or BinHex stringstring.
/// </returns>
public static string ComputeHash(byte[] byteData,
string hashAlgorithm,
byte[] saltBytes,
bool useBinHex = false)
{
if (byteData == null)
return null;
// Convert plain text into a byte array.
byte[] plainTextWithSaltBytes;
if (saltBytes != null)
{
// Allocate array, which will hold plain text and salt.
plainTextWithSaltBytes =
new byte[byteData.Length + saltBytes.Length];
// Copy plain text bytes into resulting array.
for (int i = 0; i < byteData.Length; i++)
plainTextWithSaltBytes[i] = byteData[i];
// Append salt bytes to the resulting array.
for (int i = 0; i < saltBytes.Length; i++)
plainTextWithSaltBytes[byteData.Length + i] = saltBytes[i];
}
else
plainTextWithSaltBytes = byteData;
HashAlgorithm hash;
// Make sure hashing algorithm name is specified.
if (hashAlgorithm == null)
hashAlgorithm = "";
// Initialize appropriate hashing algorithm class.
switch (hashAlgorithm.ToUpper())
{
case "SHA1":
hash = new SHA1Managed();
break;
case "SHA256":
hash = new SHA256Managed();
break;
case "SHA384":
hash = new SHA384Managed();
break;
case "SHA512":
hash = new SHA512Managed();
break;
case "HMACMD5":
hash = new HMACMD5(saltBytes);
break;
case "HMACSHA1":
hash = new HMACSHA1(saltBytes);
break;
case "HMACSHA256":
hash = new HMACSHA256(saltBytes);
break;
case "HMACSHA512":
hash = new HMACSHA512(saltBytes);
break;
default:
// default to MD5
hash = new MD5CryptoServiceProvider();
break;
}
byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);
hash.Dispose();
if (useBinHex)
return BinaryToBinHex(hashBytes);
return Convert.ToBase64String(hashBytes);
}
示例4: GetMD5
/// <summary>
/// 根据文件路径得到文件的MD5值
/// </summary>
/// <param name="filePath">文件的路径</param>
/// <returns>MD5值</returns>
public static string GetMD5(string filePath)
{
try
{
var get_file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
var md5 = new MD5CryptoServiceProvider();
var hash_byte = md5.ComputeHash(get_file);
var resule = System.BitConverter.ToString(hash_byte);
resule = resule.Replace("-", "");
md5.Clear();
md5.Dispose();
get_file.Close();
get_file.Dispose();
return resule;
}
catch (Exception e) {
return e.ToString();
}
}
示例5: MD5Stream
public static string MD5Stream(System.IO.Stream stream)
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = null;
string resule = string.Empty;
if (stream != null)
{
try
{
md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(stream);
resule = System.BitConverter.ToString(hash);
resule = resule.Replace("-", "");
}
finally
{
if (md5 != null)
{
md5.Dispose();
}
}
}
stream.Close();
return resule;
}
示例6: MD5File
public string MD5File(string fileName)
{
if(fileName.IsNull())
throw new ArgumentNullException("fileName");
byte[] retVal;
using(var file = new FileStream(fileName, FileMode.Open))
{
var md5 = new MD5CryptoServiceProvider();
retVal = md5.ComputeHash(file);
#if false
md5.Dispose();
#endif
}
var sb = new StringBuilder();
if(!retVal.IsNull())
{
for(var i = 0; i < retVal.Length; i++)
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
示例7: Md5
public string Md5(string value)
{
if(value.IsNull())
throw new ArgumentNullException("value");
var x = new MD5CryptoServiceProvider();
var data = Encoding.UTF8.GetBytes(value);
data = x.ComputeHash(data);
#if false
x.Dispose();
#endif
var ret = string.Empty;
for(var i = 0; i < data.Length; i++)
ret += data[i].ToString("x2").ToLower();
return ret;
}
示例8: Verify
/// <summary>
/// Computes the hash for the specified stream and compares
/// it to the value in this object. CRC hashes are not supported
/// because there is no built-in support in the .net framework and
/// a CRC implementation exceeds the scope of this project. If you
/// attempt to Verify() a CRC hash a NotImplemented() exception will
/// be thrown.
/// </summary>
/// <param name="istream">The stream to compute the hash for</param>
/// <returns>True if the computed hash matches what's stored in this object.</returns>
public bool Verify(Stream istream) {
if (IsValid) {
HashAlgorithm hashAlg = null;
switch (m_algorithm) {
case FtpHashAlgorithm.SHA1:
hashAlg = new SHA1CryptoServiceProvider();
break;
#if !NET2
case FtpHashAlgorithm.SHA256:
hashAlg = new SHA256CryptoServiceProvider();
break;
case FtpHashAlgorithm.SHA512:
hashAlg = new SHA512CryptoServiceProvider();
break;
#endif
case FtpHashAlgorithm.MD5:
hashAlg = new MD5CryptoServiceProvider();
break;
case FtpHashAlgorithm.CRC:
throw new NotImplementedException("There is no built in support for computing CRC hashes.");
default:
throw new NotImplementedException("Unknown hash algorithm: " + m_algorithm.ToString());
}
try {
byte[] data = null;
string hash = "";
data = hashAlg.ComputeHash(istream);
if (data != null) {
foreach (byte b in data) {
hash += b.ToString("x2");
}
return (hash.ToUpper() == m_value.ToUpper());
}
}
finally {
#if !NET2 // .NET 2.0 doesn't provide access to Dispose() for HashAlgorithm
if (hashAlg != null)
hashAlg.Dispose();
#endif
}
}
return false;
}
示例9: ToMD5Hash
/// <summary>
/// Returns the MD5 hash string of the input string.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>The MD5 hash string of the input string.</returns>
public static string ToMD5Hash(this string input)
{
var cryptoServiceProvider = new MD5CryptoServiceProvider();
var newdata = Encoding.Default.GetBytes(input);
var encrypted = cryptoServiceProvider.ComputeHash(newdata);
cryptoServiceProvider.Dispose();
return BitConverter.ToString(encrypted).Replace("-", string.Empty).ToLower();
}