DKIM.DkimSigner.GenerateSignature C# (CSharp) Method

GenerateSignature() public method

public GenerateSignature ( [ email ) : string
email [ The email to sign.
return string
		public string GenerateSignature([NotNull] Email email)
		{
		    if (email == null)
		    {
		        throw new ArgumentNullException("email");
		    }

            if (email.Headers == null)
            {
                throw new ArgumentException("email headers property is null");
            }

		    var headers = DkimCanonicalizer.CanonicalizeHeaders(email.Headers, this.HeaderCanonicalization, true, _headersToSign);

            //if (this.Debug != null)
            //{
            //    this.Debug.WriteContent("DKIM signature", email.Headers[SignatureKey].Value);
            //    this.Debug.WriteContent("DKIM canonicalized headers", headers);

            //}

			

			// assumes signature ends with "b="
			return Convert.ToBase64String(_privateKeySigner.Sign(this.Encoding.GetBytes(headers), this.SigningAlgorithm));

			
		}

Usage Example

Ejemplo n.º 1
0
        public static MailMessage DkimSign([NotNull] this MailMessage message, DkimSigner signer)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (signer == null)
            {
                throw new ArgumentNullException("signer");
            }

            message.BodyEncoding    = signer.Encoding;
            message.SubjectEncoding = signer.Encoding;
            message.HeadersEncoding = signer.Encoding;


            // get email content and generate initial signature
            var email = Email.Parse(message.GetText());

            if (!CanSign(email))
            {
                throw new InvalidOperationException("Unable to Domain Key sign the message");
            }

            var value = signer.GenerateDkimHeaderValue(email);



            // signature value get formatted so add dummy signature value then remove it
            message.Headers.Prepend(DkimSigner.SignatureKey, value + new string('0', 70));
            email = message.Parse();
            var formattedSig = email.Headers[DkimSigner.SignatureKey].Value;

            email.Headers[DkimSigner.SignatureKey].Value = formattedSig.Substring(0, formattedSig.Length - 70);



            // sign email
            value += signer.GenerateSignature(email);
            message.Headers.Set(DkimSigner.SignatureKey, value);


            return(message);
        }
All Usage Examples Of DKIM.DkimSigner::GenerateSignature