本文整理汇总了C#中HashAlgorithm.Initialize方法的典型用法代码示例。如果您正苦于以下问题:C# HashAlgorithm.Initialize方法的具体用法?C# HashAlgorithm.Initialize怎么用?C# HashAlgorithm.Initialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashAlgorithm
的用法示例。
在下文中一共展示了HashAlgorithm.Initialize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDigestedBytes
internal byte[] GetDigestedBytes(HashAlgorithm hash) {
m_c14nDoc.WriteHash(hash, DocPosition.BeforeRootElement, m_ancMgr);
hash.TransformFinalBlock(new byte[0], 0, 0);
byte[] res = (byte[]) hash.Hash.Clone();
// reinitialize the hash so it is still usable after the call
hash.Initialize();
return res;
}
示例2: VerifySignature
//private bool VerifySignature (ASN1 cs, byte[] calculatedMessageDigest, string hashName)
private bool VerifySignature (PKCS7.SignedData sd, byte[] calculatedMessageDigest, HashAlgorithm ha)
{
string contentType = null;
ASN1 messageDigest = null;
// string spcStatementType = null;
// string spcSpOpusInfo = null;
for (int i=0; i < sd.SignerInfo.AuthenticatedAttributes.Count; i++) {
ASN1 attr = (ASN1) sd.SignerInfo.AuthenticatedAttributes [i];
string oid = ASN1Convert.ToOid (attr[0]);
switch (oid) {
case "1.2.840.113549.1.9.3":
// contentType
contentType = ASN1Convert.ToOid (attr[1][0]);
break;
case "1.2.840.113549.1.9.4":
// messageDigest
messageDigest = attr[1][0];
break;
case "1.3.6.1.4.1.311.2.1.11":
// spcStatementType (Microsoft code signing)
// possible values
// - individualCodeSigning (1 3 6 1 4 1 311 2 1 21)
// - commercialCodeSigning (1 3 6 1 4 1 311 2 1 22)
// spcStatementType = ASN1Convert.ToOid (attr[1][0][0]);
break;
case "1.3.6.1.4.1.311.2.1.12":
// spcSpOpusInfo (Microsoft code signing)
/* try {
spcSpOpusInfo = System.Text.Encoding.UTF8.GetString (attr[1][0][0][0].Value);
}
catch (NullReferenceException) {
spcSpOpusInfo = null;
}*/
break;
default:
break;
}
}
if (contentType != spcIndirectDataContext)
return false;
// verify message digest
if (messageDigest == null)
return false;
if (!messageDigest.CompareValue (calculatedMessageDigest))
return false;
// verify signature
string hashOID = CryptoConfig.MapNameToOID (ha.ToString ());
// change to SET OF (not [0]) as per PKCS #7 1.5
ASN1 aa = new ASN1 (0x31);
foreach (ASN1 a in sd.SignerInfo.AuthenticatedAttributes)
aa.Add (a);
ha.Initialize ();
byte[] p7hash = ha.ComputeHash (aa.GetBytes ());
byte[] signature = sd.SignerInfo.Signature;
// we need to find the specified certificate
string issuer = sd.SignerInfo.IssuerName;
byte[] serial = sd.SignerInfo.SerialNumber;
foreach (X509Certificate x509 in coll) {
if (CompareIssuerSerial (issuer, serial, x509)) {
// don't verify is key size don't match
if (x509.PublicKey.Length > (signature.Length >> 3)) {
// return the signing certificate even if the signature isn't correct
// (required behaviour for 2.0 support)
signingCertificate = x509;
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider) x509.RSA;
if (rsa.VerifyHash (p7hash, hashOID, signature)) {
signerChain.LoadCertificates (coll);
trustedRoot = signerChain.Build (x509);
break;
}
}
}
}
// timestamp signature is optional
if (sd.SignerInfo.UnauthenticatedAttributes.Count == 0) {
trustedTimestampRoot = true;
} else {
for (int i = 0; i < sd.SignerInfo.UnauthenticatedAttributes.Count; i++) {
ASN1 attr = (ASN1) sd.SignerInfo.UnauthenticatedAttributes[i];
string oid = ASN1Convert.ToOid (attr[0]);
switch (oid) {
case PKCS7.Oid.countersignature:
// SEQUENCE {
// OBJECT IDENTIFIER
// countersignature (1 2 840 113549 1 9 6)
// SET {
PKCS7.SignerInfo cs = new PKCS7.SignerInfo (attr[1]);
trustedTimestampRoot = VerifyCounterSignature (cs, signature);
break;
default:
// we don't support other unauthenticated attributes
break;
}
//.........这里部分代码省略.........
示例3: ExerciseHash
public static bool ExerciseHash(HashAlgorithm hash, int size)
{
// Exercise the properties
//
if (hash.CanReuseTransform != true)
{
Console.WriteLine("CanReuseTransform != true");
return false;
}
if (hash.CanTransformMultipleBlocks != true)
{
Console.WriteLine("CanTransformMultipleBlocks != true");
return false;
}
if (hash.HashSize != size)
{
Console.WriteLine("HashSize, expected={0} actual={1}", size, hash.HashSize);
return false;
}
if (hash.InputBlockSize != 1)
{
Console.WriteLine("InputBlockSize != 1");
return false;
}
if (hash.OutputBlockSize != 1)
{
Console.WriteLine("OutputBlockSize != 1");
return false;
}
// Exercise the Initialize method. Test proper behavior both when it is and is not called.
//
byte[] bytesHalf1 = {0x00, 0x01, 0x02, 0x03};
byte[] bytesHalf2 = {0xfc, 0xfd, 0xfe, 0xff};
byte[] bytesFull = {0x00, 0x01, 0x02, 0x03, 0xfc, 0xfd, 0xfe, 0xff};
byte[] bytesExpected;
byte[] bytesActual;
// Initialize is called between partial hashes
//
hash.Initialize();
bytesExpected = hash.ComputeHash(bytesHalf1);
hash.Initialize();
hash.TransformBlock(bytesHalf2, 0, bytesHalf2.Length, bytesHalf2, 0);
hash.Initialize();
hash.TransformFinalBlock(bytesHalf1, 0, bytesHalf1.Length);
bytesActual = hash.Hash;
if (!CompareBytes(bytesExpected, bytesActual))
{
Console.WriteLine("\nInitialize test failed");
return false;
}
// Initialize is not called between partial hashes
//
hash.Initialize();
bytesExpected = hash.ComputeHash(bytesFull);
hash.Initialize();
hash.TransformBlock(bytesHalf1, 0, bytesHalf1.Length, bytesHalf1, 0);
hash.TransformFinalBlock(bytesHalf2, 0, bytesHalf2.Length);
bytesActual = hash.Hash;
if (!CompareBytes(bytesExpected, bytesActual))
{
Console.WriteLine("\nNo Initialize test failed");
return false;
}
// Exercise the Clear method -- ensure object disposed
//
hash.Initialize();
hash.Clear();
try
{
hash.ComputeHash(bytesFull);
Console.WriteLine("Clear test failed -- no exception thrown");
return false;
}
catch (ObjectDisposedException)
{
}
return true;
}