Ver Mensaje Individual
  #1
Antiguo 23 de febrero de 2017, 12:47
Kuk
Administrador
Última Actividad 19.09.2026 20:11
Posts Posts: 2.500
Likes enviados Enviados: 1037
Likes recibidos Recibidos: 1207
Arrow Firma digital sobre XML
1 Inactivo Inactivo

Citación del post de Eslopes
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!
Código COBOL:
  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.  
  37.   procedure division.
  38.  
  39.   *> Cobol does not support empty strings, so this a trick
  40.     set  emptyString   to  StringEmpty of SystemString
  41.  
  42.   *> C#  CspParameters cspParams = new CspParameters();
  43.       invoke ClassCspParameters "NEW" returning cspParams
  44.  
  45.   *> C# cspParams.KeyContainerName = "XML_DSIG_RSA_KEY";
  46.      set   KeyContainer OF cspParams   to  "XML_DSIG_RSA_KEY"
  47.  
  48.   *> C# RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);
  49.  
  50.      invoke ClassRSACryptoServiceProvider "NEW" using cspParams returning rsaKey
  51.  
  52.   *> C# XmlDocument xmlDoc = new XmlDocument();
  53.  
  54.      invoke ClassXmlDocument "NEW" returning xmlDoc
  55.  
  56.   *> C# xmlDoc.PreserveWhitespace = true;
  57.      set  PreserveWhitespace of xmlDoc  to b"1"  
  58.  
  59.   *> C# xmlDoc.Load("test.xml");
  60.      invoke xmlDoc "Load" using "c:\test.xml"  
  61.  
  62.   *> C# SignedXml signedXml = new SignedXml(Doc);
  63.      invoke ClassSignedXml "NEW" using  xmlDoc returning signedXml  
  64.  
  65.   *> C# signedXml.SigningKey = Key; - it should be rsaKey instead of just "Key"
  66.      set  SigningKey of signedXml   to  rsaKey
  67.  
  68.   *> C# Reference reference = new Reference();
  69.      invoke ClassXmlReference "NEW" returning xmlReference  
  70.  
  71.   *> reference.Uri = "";   // !! means all document
  72.     set  uri of xmlReference    to  emptyString  
  73.  
  74.   *> C$ XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
  75.      invoke ClassXmlDsigEnvSignatureTrans "NEW" returning env  
  76.  
  77.   *> C# reference.AddTransform(env);
  78.      invoke xmlReference "AddTransform" using env
  79.  
  80.   *> C# signedXml.AddReference(reference);
  81.      invoke signedXml "AddReference" using xmlReference  
  82.  
  83.   *> signedXml.ComputeSignature()
  84.      invoke signedXml "ComputeSignature"  
  85.  
  86.   *> C# XmlElement xmlDigitalSignature = signedXml.GetXml();
  87.      invoke signedXml "GetXml" returning xmlDigitalSignature  
  88.  
  89.   *> C# Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature, true));
  90.      invoke xmlDoc "ImportNode" using xmlDigitalSignature, b"1" returning xmlDocChild  
  91.  
  92.     set    xmlElement   to  DocumentElement of xmlDoc  
  93.  
  94.     invoke xmlElement "AppendChild" using xmlDocChild  
  95.  
  96.   *> C# xmlDoc.Save("SignedTest.xml");
  97.      invoke xmlDoc "Save" using "c:\SignedTest.xml"    
  98.  
  99.     stop run.

Testing our app:

Código XML:
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <Cliente version="1.0">
  3.     <Nome>Elis</Nome>
  4.     <Contatos>
  5.          <Telefone>99-9999-9999</Telefone>
  6.          <eMail>[email protected]</eMail>
  7.         </Contatos>
  8. </Cliente>

After run our app:

Código XML:
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <Cliente version="1.0">
  3.     <Nome>Elis</Nome>
  4.     <Contatos>
  5.          <Telefone>99-9999-9999</Telefone>
  6.          <eMail>[email protected]</eMail>
  7.         </Contatos>
  8.         <Signature xmlns=http://www.w3.org/2000/09/xmldsig#><SignedInfo>
  9. <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
  10. <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /><Reference URI="">
  11. <Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
  12. </Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
  13. <DigestValue>y4V1upGm/awXUcAKWFAG/PSRDAU=</DigestValue></Reference></SignedInfo>
  14. <SignatureValue>CNVyQxiYBVN C0o3szSkz 65mHFZ JV7rGgTkV62NH/i8fDN1yUJyd7Zo88TH4qdc
  15. U Vc qPLSIwaHO411TgI5N vmitbodhYI4O7n8HwbDa1nTEsTeGnpsXlGXTJsTvQVUe1FpD HTsITfFmE
  16. zs9HiuN5sY3glag4EZUCEyDIk=</SignatureValue>
  17. </Signature></Cliente>



NORMAS DEL FORO - para garantizar el buen funcionamiento del Foro.
¿Te han ayudado? NO TE OLVIDES de darle a
¿Quieres dirigirte a alguien en tu post? Notifícale haciendo clic en su Nick
Kuk is offline   Responder Con Cita