本文整理汇总了C#中System.Security.Cryptography.MD5CryptoServiceProvider.Initialize方法的典型用法代码示例。如果您正苦于以下问题:C# System.Security.Cryptography.MD5CryptoServiceProvider.Initialize方法的具体用法?C# System.Security.Cryptography.MD5CryptoServiceProvider.Initialize怎么用?C# System.Security.Cryptography.MD5CryptoServiceProvider.Initialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.MD5CryptoServiceProvider
的用法示例。
在下文中一共展示了System.Security.Cryptography.MD5CryptoServiceProvider.Initialize方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Hash
//MD5によるハッシュ作成
public static string Hash(string passStr,string timestampStr)
{
const int range = 64;
var pass = Encoding.ASCII.GetBytes(passStr);
var timestamp = Encoding.ASCII.GetBytes(timestampStr);
var h = new System.Security.Cryptography.MD5CryptoServiceProvider();
var k = new byte[range];
if (range < pass.Length)
throw new InvalidOperationException("key length is too long");
var ipad = new byte[range];
var opad = new byte[range];
pass.CopyTo(k,0);
for (var i = pass.Length; i < range; i++) {
k[i] = 0x00;
}
for (var i = 0; i < range; i++) {
ipad[i] = (byte)(k[i] ^ 0x36);
opad[i] = (byte)(k[i] ^ 0x5c);
}
var hi = new byte[ipad.Length + timestamp.Length];
ipad.CopyTo(hi,0);
timestamp.CopyTo(hi,ipad.Length);
var hash = h.ComputeHash(hi);
var ho = new byte[opad.Length + hash.Length];
opad.CopyTo(ho,0);
hash.CopyTo(ho,opad.Length);
h.Initialize();
var tmp = h.ComputeHash(ho);
var sb = new StringBuilder();
foreach (var b in tmp) {
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
示例2: _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;
}
示例3: 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();
}
}
示例4: CalculateLayer1ChecksumMD5
private byte[] CalculateLayer1ChecksumMD5(string filename, int OffsetLayer1, bool forceSilent)
{
/*
1. calculate checksum pointer
2. Checksum is 2 level based on Message Digest 5 algorithm
3. Pointer = @ 0x20140 and is a 4 byte pointer
4. Use MD5 to make 16 bytes digest from any string
5. name checksum pointer CHPTR
6. checksum area 1 ranges from 20000h to CHPTR – 20000h- 1
7. Create an MD5 hash from this string (20000h – (CHPTR – 20000h – 1))
a. MD5Init(Context)
b. MD5Update(Context, buffer, size)
c. MD5Final(Context, Md5Seed)
d. sMd5Seed = MD5Print(Md5Seed)
e. sMd5Seed = 16 bytes hex, so 32 bytes.
f. Now crypt sMd5Seed: xor every byte with 21h, then substract D6h (minus)
g. These 16 bytes are from CHPTR + 2 in the bin!!!! This is checksum level 1 !!
* */
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
int len = OffsetLayer1 - 0x20000;//- 1;
md5.Initialize();
int end = 0x20000 + len;
logger.Debug("Calculating from 0x20000 upto " + end.ToString("X8"));
byte[] data = readdatafromfile(filename, 0x20000, len);
byte[] hash = md5.ComputeHash(data);
/* foreach (byte b in hash)
{
byte bcalc = b;
Console.Write(" " + b.ToString("X2"));
}
logger.Debug("");*/
byte[] finalhash = new byte[hash.Length];
for (int i = 0; i < hash.Length; i++)
{
byte bcalc = hash[i];
bcalc ^= 0x21;
bcalc -= 0xD6;
finalhash[i] = bcalc;
}
/*foreach (byte b in finalhash)
{
Console.Write(" " + b.ToString("X2"));
}*/
return finalhash;
}