本文整理汇总了C#中System.Security.Cryptography.MD5CryptoServiceProvider.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# System.Security.Cryptography.MD5CryptoServiceProvider.Clear方法的具体用法?C# System.Security.Cryptography.MD5CryptoServiceProvider.Clear怎么用?C# System.Security.Cryptography.MD5CryptoServiceProvider.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.MD5CryptoServiceProvider
的用法示例。
在下文中一共展示了System.Security.Cryptography.MD5CryptoServiceProvider.Clear方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetMD5Hash
public static string GetMD5Hash(string pathName)
{
string strResult = "";
string strHashData = "";
byte[] arrbytHashValue;
System.IO.FileStream oFileStream = null;
System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher =
new System.Security.Cryptography.MD5CryptoServiceProvider();
try
{
oFileStream = GetFileStream(pathName);
arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream);
oFileStream.Close();
strHashData = System.BitConverter.ToString(arrbytHashValue);
strHashData = strHashData.Replace("-", "");
strResult = strHashData;
oMD5Hasher.Clear();
}
catch (System.Exception)
{
oMD5Hasher.Clear();
}
return (strResult);
}
示例2: MD5
public string MD5(string str)
{
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
var bs = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
md5.Clear();
return bs.Aggregate("", (current, b) => current + (b.ToString("x2")));
}
示例3: MD5String
public static string MD5String(string value)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] data = Encoding.Default.GetBytes(value);
byte[] md5Data = md5.ComputeHash(data);
md5.Clear();
string result = "";
for (int i = 0; i < md5Data.Length; i++)
{
result += md5Data[i].ToString("x").PadLeft(2, '0');
}
return result;
}
示例4: GetMD5
public string GetMD5(string sDataIn, string move)
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytValue, bytHash;
bytValue = System.Text.Encoding.UTF8.GetBytes(move + sDataIn);
bytHash = md5.ComputeHash(bytValue);
md5.Clear();
string sTemp = "";
for (int i = 0; i < bytHash.Length; i++)
{
sTemp += bytHash[i].ToString("x").PadLeft(2, '0');
}
return sTemp;
}
示例5: MD5
public string MD5(string str)
{
System.Security.Cryptography.MD5CryptoServiceProvider md = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] value, hash;
value = System.Text.Encoding.UTF8.GetBytes(str);
hash = md.ComputeHash(value);
md.Clear();
string temp = "";
for (int i = 0, len = hash.Length; i < len; i++)
{
temp += hash[i].ToString("x").PadLeft(2, '0');
}
return temp;
}
示例6: GetMD5
/// <summary>
/// 返回 MD5 加密字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string GetMD5(string s)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
bytes = md5.ComputeHash(bytes);
md5.Clear();
string ret = "";
for (int i = 0; i < bytes.Length; i++)
{
ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
}
return ret.PadLeft(32, '0');
}
示例7: Hash_MD5_32
/// <summary>
/// 计算32位MD5码
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
/// <returns></returns>
public static string Hash_MD5_32(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP
= new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = MD5CSP.ComputeHash(bytValue);
MD5CSP.Clear();
//根据计算得到的Hash码翻译为MD5码
string sHash = "", sTemp = "";
for (int counter = 0; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / 16;
if (i > 9)
{
sTemp = ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % 16;
if (i > 9)
{
sTemp += ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
}
//根据大小写规则决定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
示例8: _Get_File_MD5
/// <summary>
/// 计算指定文件的MD5值
/// </summary>
/// <param name="file">文件名</param>
/// <returns>文件的MD5值(若出现异常则返回null)</returns>
private byte[] _Get_File_MD5(string file)
{
byte[] ret = null;
var csp = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] buffer = new byte[STREAM_BUFFER_SIZE];
csp.Initialize();
FileInfo fi = new FileInfo(file);
FileStream fs = null;
try
{
fs = fi.OpenRead();
}
catch (Exception)
{
return ret;
}
//构造MD5计算事件的参数
File_MD5_Calculate_Event_Arg e = new File_MD5_Calculate_Event_Arg();
e.Current_Position = 0;
e.File_Extension = fi.Extension;
e.Full_File_Name = fi.FullName;
e.File_Length = fi.Length;
e.File_Name = fi.Name;
if (File_MD5_Begin_Calculate_Event != null)
File_MD5_Begin_Calculate_Event(e);
//读取文件计算MD5
try
{
fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
int nRead = 0;
do
{
nRead = fs.Read(buffer, 0, STREAM_BUFFER_SIZE);
csp.TransformBlock(buffer, 0, nRead, buffer, 0);
e.Current_Position += nRead;
if (File_MD5_Calculating_Event != null)
File_MD5_Calculating_Event(e);
} while (nRead != 0);
csp.TransformFinalBlock(buffer, 0, 0);
ret = csp.Hash;
}
finally
{
try
{
fs.Close();
}
catch (Exception)
{
}
csp.Clear();
}
if (File_MD5_End_Calculate_Event != null)
File_MD5_End_Calculate_Event(e);
return ret;
}
示例9: GetFileMd5
/// <summary>
/// 计算文件的MD5校验
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string GetFileMd5(string fileName)
{
//try
//{
// FileStream file = new FileStream(fileName, FileMode.Open);
// System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
// byte[] retVal = md5.ComputeHash(file);
// file.Close();
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < retVal.Length; i++)
// {
// sb.Append(retVal[i].ToString("x2"));
// }
// return sb.ToString();
//}
try
{
var get_file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
var hash_byte = md5.ComputeHash(get_file);
var resule = BitConverter.ToString(hash_byte);
resule = resule.Replace("-", "");
md5.Clear(); md5.Dispose();
get_file.Close(); get_file.Dispose();
return resule;
}
catch (Exception)
{
throw;
}
}
示例10: WorkerCompute
/** Called by our worker thread, this function handles a single item in the queue. */
protected void WorkerCompute(QueueItem item)
{
// is it a directory?
if (Directory.Exists(item.fullpath))
{
string[] files=Directory.GetFileSystemEntries(item.fullpath);
foreach (string f in files)
{
//QueueItem(f,item.partialpath+"\\"+JustTheFileName(f));
Enqueue(new QueueItem(f,item.partialpath+"\\"+JustTheFileName(f)));
this.Invoke(new UpdateEnqueuedLabelDelegate(UpdateEnqueuedLabel),new object[1] { queue.Count()});
}
return;
}
// Console.WriteLine(item.partialpath);
// it's not a directory. Is it a md5 file?
if (IsMD5File(item.fullpath))
{
//Console.WriteLine("caught md5 file");
StreamReader sr=new StreamReader(item.fullpath);
string s;
// read each lkine. If the line looks like an md5 hash, add it
// to the database.
while ((s=sr.ReadLine())!=null)
{
Match m=Regex.Match(s,@"^([0-9a-fA-F]{32})\s+(.+)$",RegexOptions.None);
if (m.Success && m.Groups.Count==3)
{
string p=m.Groups[2].Value.Trim();
string path = p.Replace("/","\\");
string hash = m.Groups[1].Value.Trim().ToLower();
kb.AddRecord(path, hash);
}
}
sr.Close();
listView.Invoke(new EventHandler(this.ReverifyAllItems));
// don't return; we also compute the hash of the md5sum file. (why not?)
}
// compute the md5 hash
FileStream fsr=null;
try
{
currentlyProcessingLabel.Invoke(new SetCurrentlyProcessingDelegate(SetCurrentlyProcessing), new object[] { item.partialpath });
fsr=new FileStream(item.fullpath, FileMode.Open, FileAccess.Read);
item.size=fsr.Length;
// wrap the file system's stream in our progress stream. The progress stream
// updates the thermometer/progress bar as the file is read.
progStream=new ProgressStream(fsr,progressBar);
// compute the hash
// Is it just me, or is this MD5 routine slow?
System.Security.Cryptography.MD5 md5=new System.Security.Cryptography.MD5CryptoServiceProvider();
md5.Initialize();
byte[] hash=md5.ComputeHash(progStream);
progStream=null;
// we're done. Add the data to the screen
listView.Invoke(new AddFileToGridDelegate(AddFileToGrid),new object[] {item, ByteArrayToHexadecimalString(hash)});
md5.Clear();
}
catch (Exception e)
{
// did they click the abort button?
if (e.Message.Equals("aborted"))
{
queue.Clear();
this.Invoke(new UpdateEnqueuedLabelDelegate(UpdateEnqueuedLabel),new object[1] { queue.Count()});
}
else if (!quitting)
ReportError("Couldn't process "+item.fullpath+"\r\n\r\nIs it open by another application?");
return;
}
finally
{
currentlyProcessingLabel.Invoke(new SetCurrentlyProcessingDelegate(SetCurrentlyProcessing), new object[] { "(idle)" });
if (fsr!=null)
fsr.Close();
}
}