System.Security.Cryptography.Xml namespace allows you to digitally sign an XML document or part of it. The following example was converted into OO Cobol using a MSDN C# sample. The original C# code has been commented out so you can compare how far from C# logic OO Cobol is...see for yourself!
1: program-id. main as "ConsoleApplication5.Main".
2:
3: environment division.
4: configuration section.
5: repository.
6: class ClassCspParameters as "System.Security.Cryptography.CspParameters"
7: class ClassXmlDocument as "System.Xml.XmlDocument"
8: class ClassXmlNode as "System.Xml.XmlNode"
9: class ClassRSACryptoServiceProvider as "System.Security.Cryptography.RSACryptoServiceProvider"
10: class ClassSignedXml as "System.Security.Cryptography.Xml.SignedXml"
11: class ClassXmlReference as "System.Security.Cryptography.Xml.Reference"
12: class ClassXmlDsigEnvSignatureTrans
13: as "System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform"
14: class ClassXmlElement as "System.Xml.XmlElement"
15: class SystemString as "System.String"
16: property StringEmpty as "Empty"
17: property KeyContainer as "KeyContainerName"
18: property PreserveWhitespace as "PreserveWhitespace"
19: property SigningKey as "SigningKey"
20: property Uri as "Uri"
21: property DocumentElement as "DocumentElement".
22:
23: data division.
24: working-storage section.
25: 01 cspParams usage object reference ClassCspParameters.
26: 01 xmlDoc usage object reference ClassXmlDocument.
27: 01 xmlDocChild usage object reference ClassXmlNode.
28: 01 xmlNode usage object reference ClassXmlNode.
29: 01 rsaKey usage object reference ClassRSACryptoServiceProvider.
30: 01 signedXml usage object reference ClassSignedXml.
31: 01 xmlReference usage object reference ClassXmlReference.
32: 01 emptyString usage object reference SystemString.
33: 01 env usage object reference ClassXmlDsigEnvSignatureTrans.
34: 01 xmlDigitalSignature usage object reference ClassXmlElement.
35: 01 xmlElement �usage object reference ClassXmlElement.
36: procedure division.
37: *> Cobol does not support empty strings, so this a trick
38: set emptyString to StringEmpty of SystemString
39:
40: *> C# CspParameters cspParams = new CspParameters();
41: invoke ClassCspParameters "NEW" returning cspParams
42:
43: *> C# cspParams.KeyContainerName = "XML_DSIG_RSA_KEY";
44: set KeyContainer OF cspParams to "XML_DSIG_RSA_KEY"
45:
46: *> C# RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);
47: invoke ClassRSACryptoServiceProvider "NEW" using cspParams returning rsaKey
48: *> C# XmlDocument xmlDoc = new XmlDocument();
49: invoke ClassXmlDocument "NEW" returning xmlDoc
50:
51: *> C# xmlDoc.PreserveWhitespace = true;
52: set PreserveWhitespace of xmlDoc to b"1"
53:
54: *> C# xmlDoc.Load("test.xml");
55: invoke xmlDoc "Load" using "c:\test.xml"
56:
57: *> C# SignedXml signedXml = new SignedXml(Doc);
58: invoke ClassSignedXml "NEW" using xmlDoc returning signedXml
59:
60: *> C# signedXml.SigningKey = Key; - it should be rsaKey instead of just "Key"
61: set SigningKey of signedXml to rsaKey
62: *> C# Reference reference = new Reference();
63: invoke ClassXmlReference "NEW" returning xmlReference
64:
65: *> reference.Uri = ""; // !! means all document
66: set uri of xmlReference to emptyString
67:
68: *> C$ XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
69: invoke ClassXmlDsigEnvSignatureTrans "NEW" returning env
70:
71: *> C# reference.AddTransform(env);
72: invoke xmlReference "AddTransform" using env
73: *> C# signedXml.AddReference(reference);
74: invoke signedXml "AddReference" using xmlReference
75:
76: *> signedXml.ComputeSignature()
77: invoke signedXml "ComputeSignature"
78:
79: *> C# XmlElement xmlDigitalSignature = signedXml.GetXml();
80: invoke signedXml "GetXml" returning xmlDigitalSignature
81:
82: *> C# Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature, true));
83: invoke xmlDoc "ImportNode" using xmlDigitalSignature, b"1" returning xmlDocChild
84:
85: set xmlElement to DocumentElement of xmlDoc
86:
87: invoke xmlElement "AppendChild" using xmlDocChild
88:
89: *> C# xmlDoc.Save("SignedTest.xml");
90: invoke xmlDoc "Save" using "c:\SignedTest.xml"
91:
92: stop run.
Testing our app
1: ?xml version="1.0" encoding="UTF-8" ?
2: Cliente version="1.0"
3: NomeElis/Nome
4: Contatos
5: �Telefone99-9999-9999/Telefone
6: �eMailelis@elis.com.br/eMail
7: /Contatos
8: /Cliente
After run our app:
1: ?xml version="1.0" encoding="UTF-8"?
8: Signature xmlns=http://www.w3.org/2000/09/xmldsig#SignedInfoCanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /Reference URI=""TransformsTransform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" //TransformsDigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /DigestValuey4V1upGm/awXUcAKWFAG/PSRDAU=/DigestValue/Reference/SignedInfoSignatureValueCNVyQxiYBVN C0o3szSkz 65mHFZ JV7rGgTkV62NH/i8fDN1yUJyd7Zo88TH4qdcU Vc qPLSIwaHO411TgI5N vmitbodhYI4O7n8HwbDa1nTEsTeGnpsXlGXTJsTvQVUe1FpD HTsITfFmEzs9HiuN5sY3glag4EZUCEyDIk=/SignatureValue/Signature/Cliente