本文整理汇总了C#中DigestAlgorithm类的典型用法代码示例。如果您正苦于以下问题:C# DigestAlgorithm类的具体用法?C# DigestAlgorithm怎么用?C# DigestAlgorithm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DigestAlgorithm类属于命名空间,在下文中一共展示了DigestAlgorithm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SignedEntity
/// <summary>
/// Creates an entity consisting of the content and signature.
/// </summary>
/// <param name="algorithm">The digest algorithm used in the signature, used for the <c>micalg</c> parameter</param>
/// <param name="content">The content entity that was signed.</param>
/// <param name="signature">The signature entity</param>
public SignedEntity(DigestAlgorithm algorithm, MimeEntity content, MimeEntity signature)
: base(CreateContentType(algorithm))
{
if (content == null)
{
throw new ArgumentNullException("content");
}
Content = content;
Signature = signature;
}
示例2: TestSignatureOIDs
public void TestSignatureOIDs(DigestAlgorithm algo)
{
string messageText = m_tester.ReadMessageText("simple.eml");
m_cryptographer.DigestAlgorithm = algo;
SignedCms signedData = null;
Assert.DoesNotThrow(() => signedData = m_cryptographer.CreateSignature(Encoding.ASCII.GetBytes(messageText), m_cert));
Assert.True(signedData.SignerInfos.Count == 1);
Assert.True(signedData.SignerInfos[0].DigestAlgorithm.Value == SMIMECryptographer.ToDigestAlgorithmOid(algo).Value);
}
示例3: CreateCipher
public virtual TlsCipher CreateCipher(TlsClientContext context,
EncryptionAlgorithm encryptionAlgorithm, DigestAlgorithm digestAlgorithm)
{
switch (encryptionAlgorithm)
{
case EncryptionAlgorithm.cls_3DES_EDE_CBC:
return CreateDesEdeCipher(context, 24, digestAlgorithm);
case EncryptionAlgorithm.AES_128_CBC:
return CreateAesCipher(context, 16, digestAlgorithm);
case EncryptionAlgorithm.AES_256_CBC:
return CreateAesCipher(context, 32, digestAlgorithm);
default:
throw new TlsFatalAlert(AlertDescription.internal_error);
}
}
示例4: CreateDigest
/// <exception cref="IOException"></exception>
protected virtual IDigest CreateDigest(DigestAlgorithm digestAlgorithm)
{
switch (digestAlgorithm)
{
case DigestAlgorithm.MD5:
return new MD5Digest();
case DigestAlgorithm.SHA:
return new Sha1Digest();
case DigestAlgorithm.SHA256:
return new Sha256Digest();
case DigestAlgorithm.SHA384:
return new Sha384Digest();
default:
throw new TlsFatalAlert(AlertDescription.internal_error);
}
}
示例5: Sign
/// <exception cref="Sharpen.NoSuchAlgorithmException"></exception>
/// <exception cref="System.IO.IOException"></exception>
public virtual byte[] Sign(Stream stream, DigestAlgorithm digestAlgo, IDssPrivateKeyEntry
keyEntry)
{
if (SignatureAlgorithm.RSA == keyEntry.GetSignatureAlgorithm())
{
IDigest digester = DigestUtilities.GetDigest(digestAlgo.GetName());
byte[] buffer = new byte[4096];
int count = 0;
while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
{
digester.BlockUpdate(buffer, 0, count);
}
byte[] digestValue = DigestUtilities.DoFinal(digester);
return EncryptDigest(digestValue, digestAlgo, keyEntry);
}
else
{
//jbonilla
throw new System.NotImplementedException("Implementar cuando no es RSA");
//Sharpen.Signature signature = Sharpen.Signature.GetInstance(keyEntry.GetSignatureAlgorithm
// ().GetJavaSignatureAlgorithm(digestAlgo));
//try
//{
// signature.InitSign(((KSPrivateKeyEntry)keyEntry).GetPrivateKey());
// byte[] buffer = new byte[4096];
// int count = 0;
// while ((count = stream.Read(buffer)) > 0)
// {
// signature.Update(buffer, 0, count);
// }
// byte[] signValue = signature.Sign();
// return signValue;
//}
//catch (SignatureException e)
//{
// throw new RuntimeException(e);
//}
//catch (InvalidKeyException e)
//{
// throw new RuntimeException(e);
//}
}
}
示例6: Create
/// <summary>
/// Creates a new <see cref="MultipartSigned"/>.
/// </summary>
/// <remarks>
/// Cryptographically signs the entity using the supplied signer and digest algorithm in
/// order to generate a detached signature and then adds the entity along with the
/// detached signature data to a new multipart/signed part.
/// </remarks>
/// <returns>A new <see cref="MultipartSigned"/> instance.</returns>
/// <param name="signer">The signer.</param>
/// <param name="digestAlgo">The digest algorithm to use for signing.</param>
/// <param name="entity">The entity to sign.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="signer"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="entity"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="System.ArgumentException">
/// <paramref name="signer"/> cannot be used for signing.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// The <paramref name="digestAlgo"/> was out of range.
/// </exception>
/// <exception cref="System.NotSupportedException">
/// <para>A cryptography context suitable for signing could not be found.</para>
/// <para>-or-</para>
/// <para>The <paramref name="digestAlgo"/> is not supported.</para>
/// </exception>
/// <exception cref="Org.BouncyCastle.Bcpg.OpenPgp.PgpException">
/// An error occurred in the OpenPGP subsystem.
/// </exception>
public static MultipartSigned Create (PgpSecretKey signer, DigestAlgorithm digestAlgo, MimeEntity entity)
{
using (var ctx = (OpenPgpContext) CryptographyContext.Create ("application/pgp-signature")) {
return Create (ctx, signer, digestAlgo, entity);
}
}
示例7: EncryptDigest
/// <summary>The encryption of a digest it the atomic operation done by the SSCD.</summary>
/// <remarks>
/// The encryption of a digest it the atomic operation done by the SSCD. This encryption (RSA, DSA, ...) create the
/// signature value.
/// </remarks>
/// <param name="digestValue"></param>
/// <param name="digestAlgo"></param>
/// <param name="keyEntry"></param>
/// <returns></returns>
/// <exception cref="Sharpen.NoSuchAlgorithmException"></exception>
public abstract byte[] EncryptDigest(byte[] digestValue, DigestAlgorithm digestAlgo
, IDssPrivateKeyEntry keyEntry);
示例8: SignAndEncrypt
/// <summary>
/// Cryptographically signs and encrypts the specified content for the specified recipients.
/// </summary>
/// <remarks>
/// Cryptographically signs and encrypts the specified content for the specified recipients.
/// </remarks>
/// <returns>A new <see cref="MimeKit.MimePart"/> instance
/// containing the encrypted data.</returns>
/// <param name="signer">The signer.</param>
/// <param name="digestAlgo">The digest algorithm to use for signing.</param>
/// <param name="recipients">The recipients.</param>
/// <param name="content">The content.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="signer"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="recipients"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="content"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="digestAlgo"/> is out of range.
/// </exception>
/// <exception cref="System.ArgumentException">
/// <para>One or more of the recipient keys cannot be used for encrypting.</para>
/// <para>-or-</para>
/// <para>No recipients were specified.</para>
/// </exception>
/// <exception cref="System.NotSupportedException">
/// The specified <see cref="DigestAlgorithm"/> is not supported by this context.
/// </exception>
/// <exception cref="PrivateKeyNotFoundException">
/// The private key could not be found for <paramref name="signer"/>.
/// </exception>
/// <exception cref="PublicKeyNotFoundException">
/// A public key could not be found for one or more of the <paramref name="recipients"/>.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The user chose to cancel the password prompt.
/// </exception>
/// <exception cref="System.UnauthorizedAccessException">
/// 3 bad attempts were made to unlock the secret key.
/// </exception>
public MimePart SignAndEncrypt (MailboxAddress signer, DigestAlgorithm digestAlgo, IEnumerable<MailboxAddress> recipients, Stream content)
{
if (signer == null)
throw new ArgumentNullException ("signer");
if (recipients == null)
throw new ArgumentNullException ("recipients");
if (content == null)
throw new ArgumentNullException ("content");
var key = GetSigningKey (signer);
return SignAndEncrypt (key, digestAlgo, GetPublicKeys (recipients), content);
}
示例9: SignAndEncrypt
/// <summary>
/// Sign and encrypt the message to the sender and all of the recipients using
/// the specified cryptography context and the specified digest algorithm.
/// </summary>
/// <remarks>
/// <para>If either of the Resent-Sender or Resent-From headers are set, then the message
/// will be signed using the Resent-Sender (or first mailbox in the Resent-From)
/// address as the signer address, otherwise the Sender or From address will be
/// used instead.</para>
/// <para>Likewise, if either of the Resent-Sender or Resent-From headers are set, then the
/// message will be encrypted to all of the addresses specified in the Resent headers
/// (Resent-Sender, Resent-From, Resent-To, Resent-Cc, and Resent-Bcc),
/// otherwise the message will be encrypted to all of the addresses specified in
/// the standard address headers (Sender, From, To, Cc, and Bcc).</para>
/// </remarks>
/// <param name="ctx">The cryptography context.</param>
/// <param name="digestAlgo">The digest algorithm.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="ctx"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ArgumentException">
/// An unknown type of cryptography context was used.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// The <paramref name="digestAlgo"/> was out of range.
/// </exception>
/// <exception cref="System.InvalidOperationException">
/// <para>The <see cref="Body"/> has not been set.</para>
/// <para>-or-</para>
/// <para>The sender has been specified.</para>
/// <para>-or-</para>
/// <para>No recipients have been specified.</para>
/// </exception>
/// <exception cref="System.NotSupportedException">
/// The <paramref name="digestAlgo"/> is not supported.
/// </exception>
/// <exception cref="CertificateNotFoundException">
/// A certificate could not be found for the signer or one or more of the recipients.
/// </exception>
/// <exception cref="PrivateKeyNotFoundException">
/// The private key could not be found for the sender.
/// </exception>
/// <exception cref="PublicKeyNotFoundException">
/// The public key could not be found for one or more of the recipients.
/// </exception>
public void SignAndEncrypt (CryptographyContext ctx, DigestAlgorithm digestAlgo)
{
if (ctx == null)
throw new ArgumentNullException ("ctx");
if (Body == null)
throw new InvalidOperationException ("No message body has been set.");
var signer = GetMessageSigner ();
if (signer == null)
throw new InvalidOperationException ("The sender has not been set.");
var recipients = GetMessageRecipients (true);
if (recipients.Count == 0)
throw new InvalidOperationException ("No recipients have been set.");
if (ctx is SecureMimeContext) {
Body = ApplicationPkcs7Mime.SignAndEncrypt ((SecureMimeContext) ctx, signer, digestAlgo, recipients, Body);
} else if (ctx is OpenPgpContext) {
Body = MultipartEncrypted.SignAndEncrypt ((OpenPgpContext) ctx, signer, digestAlgo, recipients, Body);
} else {
throw new ArgumentException ("Unknown type of cryptography context.", "ctx");
}
}
示例10: GetHashAlgorithm
/// <summary>
/// Gets the equivalent <see cref="Org.BouncyCastle.Bcpg.HashAlgorithmTag"/> for the
/// specified <see cref="DigestAlgorithm"/>.
/// </summary>
/// <remarks>
/// Maps a <see cref="DigestAlgorithm"/> to the equivalent <see cref="Org.BouncyCastle.Bcpg.HashAlgorithmTag"/>.
/// </remarks>
/// <returns>The hash algorithm.</returns>
/// <param name="digestAlgo">The digest algorithm.</param>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="digestAlgo"/> is out of range.
/// </exception>
/// <exception cref="System.NotSupportedException">
/// <paramref name="digestAlgo"/> does not have an equivalent
/// <see cref="Org.BouncyCastle.Bcpg.HashAlgorithmTag"/> value.
/// </exception>
public static HashAlgorithmTag GetHashAlgorithm (DigestAlgorithm digestAlgo)
{
switch (digestAlgo) {
case DigestAlgorithm.MD5: return HashAlgorithmTag.MD5;
case DigestAlgorithm.Sha1: return HashAlgorithmTag.Sha1;
case DigestAlgorithm.RipeMD160: return HashAlgorithmTag.RipeMD160;
case DigestAlgorithm.DoubleSha: return HashAlgorithmTag.DoubleSha;
case DigestAlgorithm.MD2: return HashAlgorithmTag.MD2;
case DigestAlgorithm.Tiger192: return HashAlgorithmTag.Tiger192;
case DigestAlgorithm.Haval5160: return HashAlgorithmTag.Haval5pass160;
case DigestAlgorithm.Sha256: return HashAlgorithmTag.Sha256;
case DigestAlgorithm.Sha384: return HashAlgorithmTag.Sha384;
case DigestAlgorithm.Sha512: return HashAlgorithmTag.Sha512;
case DigestAlgorithm.Sha224: return HashAlgorithmTag.Sha224;
case DigestAlgorithm.MD4: throw new NotSupportedException ("The MD4 digest algorithm is not supported.");
default: throw new ArgumentOutOfRangeException ("digestAlgo");
}
}
示例11: CreateDesEdeCipher
/// <exception cref="IOException"></exception>
protected virtual TlsCipher CreateDesEdeCipher(TlsClientContext context, int cipherKeySize,
DigestAlgorithm digestAlgorithm)
{
return new TlsBlockCipher(context, CreateDesEdeBlockCipher(), CreateDesEdeBlockCipher(),
CreateDigest(digestAlgorithm), CreateDigest(digestAlgorithm), cipherKeySize);
}
示例12: SetAlgorithm
/// <param name="algorithm">the algorithm to set</param>
public virtual void SetAlgorithm(DigestAlgorithm algorithm)
{
this.algorithm = algorithm;
}
示例13: Create
/// <summary>
/// Creates a new <see cref="MultipartEncrypted"/>.
/// </summary>
/// <remarks>
/// Signs the entity using the supplied signer and digest algorithm and then encrypts to
/// the specified recipients, encapsulating the result in a new multipart/encrypted part.
/// </remarks>
/// <returns>A new <see cref="MimeKit.Cryptography.MultipartEncrypted"/> instance containing
/// the signed and encrypted version of the specified entity.</returns>
/// <param name="signer">The signer to use to sign the entity.</param>
/// <param name="digestAlgo">The digest algorithm to use for signing.</param>
/// <param name="recipients">The recipients for the encrypted entity.</param>
/// <param name="entity">The entity to sign and encrypt.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="signer"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="recipients"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="entity"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="System.ArgumentException">
/// <para><paramref name="signer"/> cannot be used for signing.</para>
/// <para>-or-</para>
/// <para>One or more of the recipient keys cannot be used for encrypting.</para>
/// <para>-or-</para>
/// <para>No recipients were specified.</para>
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// The <paramref name="digestAlgo"/> was out of range.
/// </exception>
/// <exception cref="System.NotSupportedException">
/// <para>A default <see cref="OpenPgpContext"/> has not been registered.</para>
/// <para>-or-</para>
/// <para>The <paramref name="digestAlgo"/> is not supported.</para>
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The user chose to cancel the password prompt.
/// </exception>
/// <exception cref="System.UnauthorizedAccessException">
/// 3 bad attempts were made to unlock the secret key.
/// </exception>
public static MultipartEncrypted Create (PgpSecretKey signer, DigestAlgorithm digestAlgo, IEnumerable<PgpPublicKey> recipients, MimeEntity entity)
{
if (signer == null)
throw new ArgumentNullException ("signer");
if (recipients == null)
throw new ArgumentNullException ("recipients");
if (entity == null)
throw new ArgumentNullException ("entity");
using (var ctx = (OpenPgpContext) CryptographyContext.Create ("application/pgp-encrypted")) {
return Create (ctx, signer, digestAlgo, recipients, entity);
}
}
示例14: CreateRC4Cipher
/// <exception cref="IOException"></exception>
protected virtual TlsCipher CreateRC4Cipher(TlsClientContext context, int cipherKeySize, DigestAlgorithm digestAlgorithm)
{
return new TlsStreamCipher(context, CreateRC4StreamCipher(), CreateRC4StreamCipher(), CreateDigest(digestAlgorithm), CreateDigest(digestAlgorithm), cipherKeySize);
}
示例15: Sign
/// <summary>
/// Cryptographically signs the content.
/// </summary>
/// <remarks>
/// Cryptographically signs the content using the specified signer and digest algorithm.
/// </remarks>
/// <returns>A new <see cref="MimeKit.MimePart"/> instance
/// containing the detached signature data.</returns>
/// <param name="signer">The signer.</param>
/// <param name="digestAlgo">The digest algorithm to use for signing.</param>
/// <param name="content">The content.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="signer"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="content"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="System.ArgumentException">
/// <paramref name="signer"/> cannot be used for signing.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// The <paramref name="digestAlgo"/> was out of range.
/// </exception>
/// <exception cref="System.NotSupportedException">
/// The <paramref name="digestAlgo"/> is not supported.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The user chose to cancel the password prompt.
/// </exception>
/// <exception cref="System.UnauthorizedAccessException">
/// 3 bad attempts were made to unlock the secret key.
/// </exception>
public ApplicationPgpSignature Sign (PgpSecretKey signer, DigestAlgorithm digestAlgo, Stream content)
{
if (signer == null)
throw new ArgumentNullException ("signer");
if (!signer.IsSigningKey)
throw new ArgumentException ("The specified secret key cannot be used for signing.", "signer");
if (content == null)
throw new ArgumentNullException ("content");
var hashAlgorithm = GetHashAlgorithm (digestAlgo);
var memory = new MemoryBlockStream ();
using (var armored = new ArmoredOutputStream (memory)) {
var compresser = new PgpCompressedDataGenerator (CompressionAlgorithmTag.ZLib);
using (var compressed = compresser.Open (armored)) {
var signatureGenerator = new PgpSignatureGenerator (signer.PublicKey.Algorithm, hashAlgorithm);
var buf = new byte[4096];
int nread;
signatureGenerator.InitSign (PgpSignature.CanonicalTextDocument, GetPrivateKey (signer));
while ((nread = content.Read (buf, 0, buf.Length)) > 0)
signatureGenerator.Update (buf, 0, nread);
var signature = signatureGenerator.Generate ();
signature.Encode (compressed);
compressed.Flush ();
}
armored.Flush ();
}
memory.Position = 0;
return new ApplicationPgpSignature (memory);
}