private static (XDocument document, byte[] signature) GetSignatureToTimestamp(OpcPart signaturePart)
        {
            XNamespace xmlDSigNamespace = OpcKnownUris.XmlDSig.AbsoluteUri;

            using (var signatureStream = signaturePart.Open())
            {
                var doc       = XDocument.Load(signatureStream);
                var signature = doc.Element(xmlDSigNamespace + "Signature")?.Element(xmlDSigNamespace + "SignatureValue")?.Value?.Trim();
                return(doc, Convert.FromBase64String(signature));
            }
        }
 public static (byte[] digest, Uri identifier) Digest(OpcPart part, HashAlgorithmName algorithmName)
 {
     using (var hashAlgorithm = HashAlgorithmTranslator.TranslateFromNameToxmlDSigUri(algorithmName, out var identifier))
     {
         using (var partStream = part.Open())
         {
             var digest = hashAlgorithm.ComputeHash(partStream);
             return(digest, identifier);
         }
     }
 }
Esempio n. 3
0
        public static (byte[] digest, Uri identifier) Digest(OpcPart part, HashAlgorithmName algorithmName)
        {
            var info = new HashAlgorithmInfo(algorithmName);

            using (var hashAlgorithm = info.Create())
            {
                using (var partStream = part.Open())
                {
                    var digest = hashAlgorithm.ComputeHash(partStream);
                    return(digest, info.XmlDSigIdentifier);
                }
            }
        }
 private static void PublishSignature(XmlDocument document, OpcPart signatureFile)
 {
     using (var copySignatureStream = signatureFile.Open())
     {
         copySignatureStream.SetLength(0L);
         using (var xmlWriter = new XmlTextWriter(copySignatureStream, System.Text.Encoding.UTF8))
         {
             //The .NET implementation of OPC used by Visual Studio does not tollerate "white space" nodes.
             xmlWriter.Formatting = Formatting.None;
             document.Save(xmlWriter);
         }
     }
 }
        private static void ApplyTimestamp(XDocument originalSignatureDocument, OpcPart signaturePart, byte[] timestampSignature)
        {
            XNamespace xmlDSigNamespace      = OpcKnownUris.XmlDSig.AbsoluteUri;
            XNamespace xmlSignatureNamespace = OpcKnownUris.XmlDigitalSignature.AbsoluteUri;
            var        document  = new XDocument(originalSignatureDocument);
            var        signature = new XElement(xmlDSigNamespace + "Object",
                                                new XElement(xmlSignatureNamespace + "TimeStamp", new XAttribute("Id", "idSignatureTimestamp"),
                                                             new XElement(xmlSignatureNamespace + "Comment", ""),
                                                             new XElement(xmlSignatureNamespace + "EncodedTime", Convert.ToBase64String(timestampSignature))
                                                             )
                                                );

            document.Element(xmlDSigNamespace + "Signature").Add(signature);
            using (var copySignatureStream = signaturePart.Open())
            {
                using (var xmlWriter = new XmlTextWriter(copySignatureStream, System.Text.Encoding.UTF8))
                {
                    //The .NET implementation of OPC used by Visual Studio does not tollerate "white space" nodes.
                    xmlWriter.Formatting = Formatting.None;
                    document.Save(xmlWriter);
                }
            }
        }