📄 cryptohelper.cs
字号:
namespace NCindy.Util
{
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public static class CryptoHelper
{
private const byte FOUR_BITS = 4;
private const byte HIGH_BITS_MASK = 240;
private const byte LOW_BITS_MASK = 15;
private const byte MD5_LENGTH = 0x20;
public static string ByteArrayToHexadecimalString(byte[] array)
{
LangHelper.CheckNullArgument("array", array);
StringBuilder builder = new StringBuilder(0x20);
for (int i = 0; i < array.Length; i++)
{
builder.Append(NibbleToHex((array[i] & 240) >> 4));
builder.Append(NibbleToHex(array[i] & 15));
}
return builder.ToString();
}
public static string GetFileMD5(string filePath)
{
LangHelper.CheckNullArgument("filePath", filePath);
using (FileStream s = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
return GetMD5(s);
}
}
public static string GetFileMD5(string filePath, long offset, int length)
{
LangHelper.CheckNullArgument("filePath", filePath);
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
PartStream s = new PartStream(stream, offset, length);
return GetMD5(s);
}
}
public static string GetMD5(byte[] array)
{
LangHelper.CheckNullArgument("array", array);
if (array.Length < 1)
{
throw new ArgumentException("array should not be empty.");
}
return GetMD5(array, 0, array.Length);
}
private static string GetMD5(Stream s)
{
MD5 md = null;
string text;
try
{
md = new MD5CryptoServiceProvider();
md.Initialize();
text = ByteArrayToHexadecimalString(md.ComputeHash(s));
}
finally
{
if (md != null)
{
md.Clear();
}
}
return text;
}
public static string GetMD5(byte[] array, int offset, int count)
{
string text;
LangHelper.CheckNullArgument("array", array);
MD5 md = null;
try
{
md = new MD5CryptoServiceProvider();
md.Initialize();
text = ByteArrayToHexadecimalString(md.ComputeHash(array, offset, count));
}
finally
{
if (md != null)
{
md.Clear();
}
}
return text;
}
public static string GetMD5(Stream s, long offset, int length)
{
PartStream stream = new PartStream(s, offset, length);
return GetMD5(stream);
}
private static char NibbleToHex(int n)
{
if (n < 10)
{
return (char) (0x30 + n);
}
return (char) (0x61 + (n - 10));
}
private class PartStream : Stream
{
private readonly Stream _innerStream;
private readonly int _lenght;
private readonly long _offset;
public PartStream(Stream stream, long offset, int length)
{
this._innerStream = stream;
this._offset = offset;
this._lenght = length;
this._innerStream.Position = this._offset;
}
public override void Flush()
{
this._innerStream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
if (this.Position == (this._lenght - 1))
{
return 0;
}
int num = count;
if ((this.Position + count) > this._lenght)
{
num = this._lenght - ((int) this.Position);
}
return this._innerStream.Read(buffer, offset, num);
}
public override int ReadByte()
{
if (this.Position == (this._lenght - 1))
{
return -1;
}
return this._innerStream.ReadByte();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override bool CanRead
{
get
{
return this._innerStream.CanRead;
}
}
public override bool CanSeek
{
get
{
return false;
}
}
public override bool CanWrite
{
get
{
return false;
}
}
public override long Length
{
get
{
return (long) this._lenght;
}
}
public override long Position
{
get
{
return (this._innerStream.Position - this._offset);
}
set
{
this._innerStream.Position = this._offset + value;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -