本文整理汇总了C#中System.Security.Cryptography.MD5CryptoServiceProvider.TransformBlock方法的典型用法代码示例。如果您正苦于以下问题:C# System.Security.Cryptography.MD5CryptoServiceProvider.TransformBlock方法的具体用法?C# System.Security.Cryptography.MD5CryptoServiceProvider.TransformBlock怎么用?C# System.Security.Cryptography.MD5CryptoServiceProvider.TransformBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.MD5CryptoServiceProvider
的用法示例。
在下文中一共展示了System.Security.Cryptography.MD5CryptoServiceProvider.TransformBlock方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: _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;
}
示例2: saveChunks
public int saveChunks()
{
if ( _saved )
throw new Exception( "already saved!" );
byte[] b = new byte[GridFS.DEFAULT_CHUNKSIZE];
long total = 0;
int cn = 0;
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
BinaryReader reader = new BinaryReader( _in);
while ( true )
{
int start =0;
while ( start < b.Length )
{
int r = reader.Read( b , start , b.Length - start );
if ( r == 0 )
throw new Exception( "i'm doing something wrong" );
if ( r < 0 )
break;
start += r;
}
total += start;
byte[] mine = b;
if ( start != b.Length )
{
mine = new byte[start];
Array.ConstrainedCopy( b , 0 , mine , 0 , start );
}
DBObject chunk = BasicDBObjectBuilder.start()
.Add( "files_id" , _id )
.Add( "n" , cn++ )
.Add( "data" , mine )
.Value;
_fs._chunkCollection.save( chunk );
if ( start < b.Length )
{
x.TransformFinalBlock(b, 0, b.Length);
break;
}
else
{
x.TransformBlock(b, 0, b.Length, b, 0)
}
}
_md5 = Util.toHex(x.Hash);
_length = total;
_saved = true;
return cn;
}