本文整理汇总了C#中System.Security.Cryptography.MD5CryptoServiceProvider.Initialize方法的典型用法代码示例。如果您正苦于以下问题:C# MD5CryptoServiceProvider.Initialize方法的具体用法?C# MD5CryptoServiceProvider.Initialize怎么用?C# MD5CryptoServiceProvider.Initialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.MD5CryptoServiceProvider
的用法示例。
在下文中一共展示了MD5CryptoServiceProvider.Initialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateKeyDigest
static byte[] CreateKeyDigest(String password, byte[] docIdData)
{
Check16Bytes(docIdData, "docId");
int nChars = Math.Min(password.Length, 16);
byte[] passwordData = new byte[nChars * 2];
for (int i = 0; i < nChars; i++)
{
char ch = password[i];
passwordData[i * 2 + 0] = (byte)((ch << 0) & 0xFF);
passwordData[i * 2 + 1] = (byte)((ch << 8) & 0xFF);
}
byte[] kd;
MD5 md5 = new MD5CryptoServiceProvider();
byte[] passwordHash = md5.ComputeHash(passwordData);
md5.Clear();
md5.Initialize();
byte[] data=new byte[passwordHash.Length*16 + docIdData.Length*16];
int offset=0;
for (int i = 0; i < 16; i++)
{
Array.Copy(passwordHash, 0, data, offset, PASSWORD_HASH_NUMBER_OF_BYTES_USED);
offset+=passwordHash.Length;
Array.Copy(docIdData,0,data,offset,docIdData.Length);
offset += docIdData.Length;
}
kd = md5.ComputeHash(data);
byte[] result = new byte[KEY_DIGEST_LENGTH];
Array.Copy(kd, 0, result, 0, KEY_DIGEST_LENGTH);
return result;
}
示例2: ToMD5
public static string ToMD5(this Stream stream)
{
HashAlgorithm hasher = new MD5CryptoServiceProvider();
hasher.Initialize();
stream.Seek(0, System.IO.SeekOrigin.Begin);
return BitConverter.ToString(hasher.ComputeHash(stream)).Replace("-", "");
}
示例3: Generate
public static string Generate(string str)
{
var md5 = new MD5CryptoServiceProvider();
md5.Initialize();
var buf = Encoding.UTF8.GetBytes(str);
var hashArray = md5.ComputeHash(buf);
string hash = "";
hashArray.ForEach(x =>
{
hash += x.ToString("x");
});
return hash.Substring(0, 6);
}
示例4: MD5File
/// <summary>
/// ���ļ�����MD5����
/// </summary>
/// <param name="filePath"></param>
public static void MD5File(string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
int bufferSize = 1048576; // ����������1MB
byte[] buff = new byte[bufferSize];
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
md5.Initialize();
long offset = 0;
while (offset < fs.Length)
{
long readSize = bufferSize;
if (offset + readSize > fs.Length)
{
readSize = fs.Length - offset;
}
fs.Read(buff, 0, Convert.ToInt32(readSize)); // ��ȡһ�����ݵ�������
if (offset + readSize < fs.Length) // �������һ��
{
md5.TransformBlock(buff, 0, Convert.ToInt32(readSize), buff, 0);
}
else // ���һ��
{
md5.TransformFinalBlock(buff, 0, Convert.ToInt32(readSize));
}
offset += bufferSize;
}
fs.Close();
byte[] result = md5.Hash;
md5.Clear();
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < result.Length; i++)
{
sb.Append(result[i].ToString("X2"));
}
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
示例5: GetFileMd5
/// <summary>
/// 获取文件的MD5值
/// </summary>
/// <param name="fileName"> 文件名 </param>
/// <returns> 32位MD5 </returns>
public static string GetFileMd5(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
const int bufferSize = 1024 * 1024;
byte[] buffer = new byte[bufferSize];
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
md5.Initialize();
long offset = 0;
while (offset < fs.Length)
{
long readSize = bufferSize;
if (offset + readSize > fs.Length)
{
readSize = fs.Length - offset;
}
fs.Read(buffer, 0, (int)readSize);
if (offset + readSize < fs.Length)
{
md5.TransformBlock(buffer, 0, (int)readSize, buffer, 0);
}
else
{
md5.TransformFinalBlock(buffer, 0, (int)readSize);
}
offset += bufferSize;
}
fs.Close();
byte[] result = md5.Hash;
md5.Clear();
StringBuilder sb = new StringBuilder(32);
foreach (byte b in result)
{
sb.Append(b.ToString("X2"));
}
return sb.ToString();
}
示例6: Md5Sum
/// <summary>
/// Constructor.
/// </summary>
public Md5Sum()
{
_md5Sum = new MD5CryptoServiceProvider();
_md5Sum.Initialize();
}
示例7: CreateKeyDigest
private static byte[] CreateKeyDigest(String password, byte[] docIdData)
{
Check16Bytes(docIdData, "docId");
var nChars = Math.Min(password.Length, 16);
var passwordData = new byte[nChars*2];
for (var i = 0; i < nChars; i++)
{
var chr = password[i];
passwordData[i*2 + 0] = (byte) ((chr << 0) & 0xFF);
passwordData[i*2 + 1] = (byte) ((chr << 8) & 0xFF);
}
using (MD5 md5 = new MD5CryptoServiceProvider())
{
var passwordHash = md5.ComputeHash(passwordData);
md5.Initialize();
var data = new byte[PasswordHashNumberOfBytesUsed*16 + docIdData.Length*16];
var offset = 0;
for (var i = 0; i < 16; i++)
{
Array.Copy(passwordHash, 0, data, offset, PasswordHashNumberOfBytesUsed);
offset += PasswordHashNumberOfBytesUsed;
Array.Copy(docIdData, 0, data, offset, docIdData.Length);
offset += docIdData.Length;
}
var kd = md5.ComputeHash(data);
var result = new byte[KeyDigestLength];
Array.Copy(kd, 0, result, 0, KeyDigestLength);
md5.Clear();
return result;
}
}
示例8: GenMD5
/// <summary>
/// 生成某个文件的MD5
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private string GenMD5(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
int bufferSize = 1048576; // 缓冲区大小,1MB
byte[] buff = new byte[bufferSize];
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
md5.Initialize();
long offset = 0;
while (offset < fs.Length)
{
long readSize = bufferSize;
if (offset + readSize > fs.Length)
{
readSize = fs.Length - offset;
}
fs.Read(buff, 0, Convert.ToInt32(readSize)); // 读取一段数据到缓冲区
if (offset + readSize < fs.Length) // 不是最后一块
{
md5.TransformBlock(buff, 0, Convert.ToInt32(readSize), buff, 0);
}
else // 最后一块
{
md5.TransformFinalBlock(buff, 0, Convert.ToInt32(readSize));
}
offset += bufferSize;
}
fs.Close();
byte[] result = md5.Hash;
md5.Clear();
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < result.Length; i++)
{
sb.Append(result[i].ToString("X2"));
}
return sb.ToString();
}
示例9: GetMD5Hash
/// <summary>
/// 获取本地文件MD5哈希值
/// </summary>
/// <param name="FilePath">本地文件完整路径</param>
/// <remarks>
/// 如果文件不存在则返回字符N/A
/// </remarks>
public static string GetMD5Hash(string FilePath)
{
if (!File.Exists(FilePath))
{
return "N/A";
}
else
{
System.IO.FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
System.Security.Cryptography.MD5 md = new MD5CryptoServiceProvider();
md.Initialize();
byte[] b = md.ComputeHash(fs);
return ByteArrayToHexStr(b, true);
}
}
示例10: Parse
// Parse the contents of a certificate data block.
private void Parse(byte[] data)
{
// Clone the data for internal storage.
rawData = (byte[])(data.Clone());
// Parse the ASN.1 data to get the field we are interested in.
ASN1Parser parser = new ASN1Parser(rawData);
ASN1Parser signed = parser.GetSequence();
ASN1Parser certInfo = signed.GetSequence();
if(certInfo.Type == ASN1Parser.ContextSpecific(0))
{
// Skip the version field.
certInfo.Skip();
}
serialNumber = certInfo.GetContentsAsArray(ASN1Type.Integer);
ASN1Parser algId = certInfo.GetSequence();
issuer = ParseName(certInfo);
ASN1Parser validity = certInfo.GetSequence();
effectiveDate = validity.GetUTCTime();
expirationDate = validity.GetUTCTime();
name = ParseName(certInfo);
ASN1Parser keyInfo = certInfo.GetSequence();
algId = keyInfo.GetSequence();
keyAlgorithm = ToHex(algId.GetObjectIdentifier());
if(algId.IsAtEnd() || algId.IsNull())
{
keyAlgorithmParameters = null;
}
else
{
keyAlgorithmParameters = algId.GetWholeAsArray();
}
publicKey = keyInfo.GetBitString();
#if CONFIG_CRYPTO
// Construct an MD5 hash of the certificate. Is this correct?
MD5 md5 = new MD5CryptoServiceProvider();
md5.InternalHashCore(rawData, 0, rawData.Length);
hash = md5.InternalHashFinal();
md5.Initialize();
#endif
}
示例11: PassphraseToKey
public static byte[] PassphraseToKey(string passphrase, int length) {
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] pp = Encoding.UTF8.GetBytes(passphrase);
int hashlen = md5.HashSize / 8;
byte[] buf = new byte[((length + hashlen) / hashlen) * hashlen];
int offset = 0;
while (offset < length) {
MemoryStream s = new MemoryStream();
s.Write(pp, 0, pp.Length);
if (offset > 0)
s.Write(buf, 0, offset);
Array.Copy(md5.ComputeHash(s.ToArray()), 0, buf, offset, hashlen);
offset += hashlen;
md5.Initialize();
}
byte[] key = new byte[length];
Array.Copy(buf, 0, key, 0, length);
return key;
}
示例12: ComposeRequest
public string ComposeRequest()
{
string nonce = ipAddress + ":" + DateTime.Now.ToUniversalTime().ToString("R");
MD5 md5 = new MD5CryptoServiceProvider();
md5.Initialize();
byte[] h = md5.ComputeHash(Encoding.ASCII.GetBytes(nonce));
nonce = BitConverter.ToString(h).Replace("-", "").ToLower();
string opaque = "nfoiwero8ur0 ofidosfuoewrf oieufo sedoif ";
h = md5.ComputeHash(Encoding.ASCII.GetBytes(opaque));
opaque = BitConverter.ToString(h).Replace("-", "").ToLower();
return string.Format("Digest qop=auth, nonce=\"{0}\", realm=\"{1}\", opaque=\"{2}\"",
nonce, Realm, opaque);
}
示例13: Encode
public byte[] Encode(byte[] data)
{
byte[] buffer = new byte[data.Length + 128];
using (MemoryStream stream = new MemoryStream(data))
{
int num;
MemoryStream stream2 = new MemoryStream(buffer);
RSACryptoServiceProvider key = new RSACryptoServiceProvider(1024);
byte[] buffer2 = new byte[86];
byte[] outputBuffer = new byte[86];
HashAlgorithm algorithm = new MD5CryptoServiceProvider();
algorithm.Initialize();
while ((num = stream.Read(buffer2, 0, 86)) == 86)
{
algorithm.TransformBlock(buffer2, 0, 86, outputBuffer, 0);
stream2.Write(buffer2, 0, buffer2.Length);
}
buffer2 = algorithm.TransformFinalBlock(buffer2, 0, num);
stream2.Write(buffer2, 0, buffer2.Length);
RSAParameters parameters = new RSAParameters();
parameters.D = (byte[])this.rsaParameters.D.Clone();
parameters.DP = (byte[])this.rsaParameters.DP.Clone();
parameters.DQ = (byte[])this.rsaParameters.DQ.Clone();
parameters.Exponent = (byte[])this.rsaParameters.Exponent.Clone();
parameters.InverseQ = (byte[])this.rsaParameters.InverseQ.Clone();
parameters.Modulus = (byte[])this.rsaParameters.Modulus.Clone();
parameters.P = (byte[])this.rsaParameters.P.Clone();
parameters.Q = (byte[])this.rsaParameters.Q.Clone();
key.ImportParameters(parameters);
AsymmetricSignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);
formatter.SetHashAlgorithm("MD5");
outputBuffer = formatter.CreateSignature(algorithm.Hash);
stream2.Write(outputBuffer, 0, outputBuffer.Length);
stream2.Close();
stream.Close();
}
return buffer;
}
示例14: ImgJBIG2
/**
* Actual constructor for ImgJBIG2 images.
* @param width the width of the image
* @param height the height of the image
* @param data the raw image data
* @param globals JBIG2 globals
*/
public ImgJBIG2(int width, int height, byte[] data, byte[] globals)
: base((Uri)null)
{
type = Element.JBIG2;
originalType = ORIGINAL_JBIG2;
scaledHeight = height;
this.Top = scaledHeight;
scaledWidth = width;
this.Right = scaledWidth;
bpc = 1;
colorspace = 1;
rawData = data;
plainWidth = this.Width;
plainHeight = this.Height;
if ( globals != null ) {
this.global = globals;
try {
MD5 md5 = new MD5CryptoServiceProvider();
md5.Initialize();
this.globalHash = md5.ComputeHash(this.global);
} catch {
//ignore
}
}
}
示例15: CryptDeriveKey
// Derive a key for a specific cryptographic algorithm.
public byte[] CryptDeriveKey(String algname, String alghashname,
int keySize, byte[] rgbIV)
{
if((algname == "DES" || algname == "RC2") &&
alghashname == "MD5" && keySize == 8)
{
// Use the older PKCS #5 password generation routine.
MD5 md5 = new MD5CryptoServiceProvider();
if(strPassword != null)
{
byte[] pwd = Encoding.UTF8.GetBytes(strPassword);
md5.InternalHashCore(pwd, 0, pwd.Length);
Array.Clear(pwd, 0, pwd.Length);
}
if(rgbSalt != null)
{
md5.InternalHashCore(rgbSalt, 0, rgbSalt.Length);
}
byte[] tempHash = md5.InternalHashFinal();
md5.Initialize();
int count = iterations;
while(count > 1)
{
md5.InternalHashCore(tempHash, 0, tempHash.Length);
Array.Clear(tempHash, 0, tempHash.Length);
tempHash = md5.InternalHashFinal();
md5.Initialize();
--count;
}
byte[] key = new byte [8];
Array.Copy(tempHash, 0, key, 0, 8);
if(rgbIV != null)
{
Array.Copy(tempHash, 8, rgbIV, 0, 8);
}
Array.Clear(tempHash, 0, tempHash.Length);
return key;
}
else
{
// Use the newer PKCS #5 password generation routine.
Reset();
if(alghashname != null)
{
strHashName = alghashname;
}
byte[] result = GetBytes(keySize);
if(rgbIV != null)
{
byte[] iv = GetBytes(rgbIV.Length);
Array.Copy(iv, 0, rgbIV, 0, rgbIV.Length);
Array.Clear(iv, 0, iv.Length);
}
return result;
}
}