Unofficial Content

GeneXus Security API is on the way to substitute Cryptography data types.

This article shows some code using the Cryptography data type and the corresponding code using the Security API as a sample on how to convert code from one to another.

Sample: Signing an XML

Using DataType

&File.Source = 'File.xml"
&xml = &File.ReadAllText(Encoding.ISO_8859_1)

//Load  certificate
&pathPFX = "certprueba.pfx"
&clavePFX = "Password1"

&error = &CryptoCertificate.Load(&pathPFX,&clavePFX)

if &error = 0
    if not &CryptoCertificate.HasPrivateKey()
    msg("No se pudo acceder al certificado en la ruta " + &pathPFX + ".¿Tiene clave privada? " + &CryptoCertificate.ErrDescription, status)
    &mal = "S"
    endif
else
    msg("Error al cargar el certificado: " + &CryptoCertificate.ErrDescription + ". Ruta: " + &pathPFX, status)
    &mal = "S"
endif

if &mal = "N"
    &CryptoSignXml.Certificate = &CryptoCertificate
    &CryptoSignXml.KeyInfoClauses.Clear()
    &CryptoHash.Algorithm = CryptoHashAlgorithm.SHA1 

    &CryptoSignXml.KeyInfoClauses.Add('RSAKeyValue')
    &CryptoSignXml.KeyInfoClauses.Add('X509Certificate')
    &signedXml = &CryptoSignXml.Sign(&xml)
    msg(&signedXml.ToString(), status)
endif

Using Security API

&DSigOptions = new()
&alias = "1"
&password = "dummy"
&path = "certprueba.pfx"
&xmlFilePath  = 'File.xml"
&PrivateKey.LoadPKCS12(&path, &alias, &password)
&Certificate.LoadPKCS12(&path, &alias, &password)
&outputPath = "C:\Temp\signedXml.xml"
&result = &XmlDSigSigner.DoSignFile(&xmlFilePath, &PrivateKey, &Certificate, &outputPath, &DSigOptions)
if &result  =  false
    if &XmlDSigSigner.HasError() = true
        msg("Error Code: " + &XmlDSigSigner.GetErrorCode() + "Description: " + &XmlDSigSigner.GetErrorDescription(), status)    
    endif
endif



Last update: February 2024 | © GeneXus. All rights reserved. GeneXus Powered by Globant