示例#1
0
        /// <summary>
        /// Searches the specified text for a signature block.
        /// If found it is verified that is belongs to the text,
        /// which indicates that the text has not been modified.
        /// </summary>
        /// <param name="signer">The <see cref="ISigner"/>
        /// instance that is used to generate the signature.</param>
        /// <param name="text">The text which contains an embedded signature.</param>
        /// <param name="publicKey">The public key for verifying the signature.</param>
        /// <returns>A <see cref="TextVerificationResult"/> indicating
        /// whether the signature is valid or not. If the text does
        /// not contain an embedded signature, <c>SignatureVerified</c>
        /// is set to <c>false</c>. The <c>TextVerificationResult</c>
        /// object also contains the text without the signature.</returns>
        /// <exception cref="ArgumentNullException">
        /// Occurs if any of the arguments are null.</exception>
        public async Task <TextVerificationResult> VerifySignatureAsync(
            ISigner signer, string text, string publicKey)
        {
            AssertArg.IsNotNull(signer, nameof(signer));
            AssertArg.IsNotNull(text, nameof(text));
            AssertArg.IsNotNull(publicKey, nameof(publicKey));

            var regex = GetSignatureRegex();
            var match = regex.Match(text);

            if (!match.Success)
            {
                return(new TextVerificationResult(false, null));
            }

            var    signatureText    = match.Groups["Signature"].Value;
            var    allText          = match.Groups["All"].Value;
            string withoutSignature = text.Replace(allText, string.Empty);

            bool result = await signer.VerifyBase64SignatureAsync(
                withoutSignature, signatureText, publicKey, Encoding);

            return(new TextVerificationResult(result, withoutSignature));
        }