public void VerifySignedXml_ShouldVerifySignedXmlString()
        {
            var input = new SignXmlInput
            {
                CertificatePath    = _certificatePath,
                PrivateKeyPassword = _privateKeyPassword,
                SigningStrategy    = SigningStrategyType.PrivateKeyCertificate,
                XmlInputType       = XmlParamType.XmlString,
                XmlEnvelopingType  = XmlEnvelopingType.XmlEnvelopedSignature,
                Xml = "<root><foo>bar</foo></root>"
            };
            var output = new SignXmlOutput
            {
                OutputType = XmlParamType.XmlString
            };
            var options = new SignXmlOptions
            {
                DigestMethod       = DigestMethod.SHA256,
                TransformMethods   = new [] { TransformMethod.DsigExcC14 },
                XmlSignatureMethod = XmlSignatureMethod.RSASHA256
            };
            string signedXml   = SigningTask.SignXml(input, output, options).Result;
            var    verifyInput = new VerifySignatureInput
            {
                XmlInputType = XmlParamType.XmlString,
                Xml          = signedXml
            };

            var result = VerifyTask.VerifySignedXml(verifyInput, new VerifySignatureOptions());

            Assert.IsTrue(result.IsValid);
        }
        public void SignXml_ShouldSignXmlStringWithPrivateKeyCertificate()
        {
            var input = new SignXmlInput
            {
                CertificatePath    = _certificatePath,
                PrivateKeyPassword = _privateKeyPassword,
                SigningStrategy    = SigningStrategyType.PrivateKeyCertificate,
                XmlInputType       = XmlParamType.XmlString,
                XmlEnvelopingType  = XmlEnvelopingType.XmlEnvelopedSignature,
                Xml = "<root><value>foo</value></root>"
            };
            var output = new SignXmlOutput
            {
                OutputType = XmlParamType.XmlString
            };
            var options = new SignXmlOptions
            {
                DigestMethod       = DigestMethod.SHA256,
                TransformMethods   = new [] { TransformMethod.DsigExcC14 },
                XmlSignatureMethod = XmlSignatureMethod.RSASHA256
            };

            SigningResult result = SigningTask.SignXml(input, output, options);

            StringAssert.Contains("<Signature", result.Result);
        }
        public void SignXml_ShouldSignXmlFileWithPrivateKeyCertificate()
        {
            // create file
            string xmlFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestItems", Guid.NewGuid().ToString() + ".xml");

            File.WriteAllText(xmlFilePath, @"<root>
    <value>foo</value>
</root>");
            var input = new SignXmlInput
            {
                CertificatePath    = _certificatePath,
                PrivateKeyPassword = _privateKeyPassword,
                SigningStrategy    = SigningStrategyType.PrivateKeyCertificate,
                XmlEnvelopingType  = XmlEnvelopingType.XmlEnvelopedSignature,
                XmlInputType       = XmlParamType.File,
                XmlFilePath        = xmlFilePath
            };
            var output = new SignXmlOutput
            {
                OutputType     = XmlParamType.File,
                OutputFilePath = xmlFilePath.Replace(".xml", "_signed.xml"),
                OutputEncoding = "utf-8"
            };
            var options = new SignXmlOptions
            {
                DigestMethod       = DigestMethod.SHA256,
                TransformMethods   = new [] { TransformMethod.DsigExcC14 },
                XmlSignatureMethod = XmlSignatureMethod.RSASHA256,
                PreserveWhitespace = true
            };

            SigningResult result    = SigningTask.SignXml(input, output, options);
            var           signedXml = File.ReadAllText(result.Result);

            StringAssert.Contains("<Signature", signedXml);
            StringAssert.DoesNotContain("<Signature", File.ReadAllText(xmlFilePath));

            // cleanup
            File.Delete(xmlFilePath);
            File.Delete(result.Result);
        }