Create a Digital Signature in ABAP ( Sample Code)

Here is explanation of Create a Digital Signature in ABAP (Sample Code).

This is a short list of necessary operations:
Build a table for metadata using as key a field referring to data element SIGN_GUID_22 and the complete key for your to-be-signed document; in my case:
ZZAPT_SIGNATURE
FIELD DATA ELEMENT
MANDT
MANDT
SIGN_ID SIGN_GUID_22
CORDERID N1CORDID
Build a structure for operation log: ZZAPT_SIGNATURE_LOG
FIELD DATA ELEMENT
.include
SIGN_PROT_STRUC
CORDERID N1CORDID
Launch the following transactions(very intuitive):
SLG0: declare a sub-object under CDSG1
SIGNA:declare your application
SIGNO:insert the signature object
ELSIG03N: choose and link the type of signature(in my case R/3)
REPORT ZPR00004
.
DATA: lo_ds_runtime TYPE REF TO if_ds_runtime.
DATA: lo_sign TYPE REF TO if_ds_sign.
DATA: l_metadata_wa TYPE ZZAPT_SIGNATURE. lo_ds_runtime = cl_ds_runtime=>get_instance( ).
l_metadata_wa-corderid = ‘5159DB46ACFC1424E1000000010000AB’.”Doc ID call method lo_ds_runtime->create
exporting
im_appl = ‘ZTEST'”APPL
im_object = ‘ZTEST'”OBJECT
im_meta = l_metadata_wa
im_type = ‘C’ “Synch signature
receiving
result = lo_sign.
call method lo_sign->sign
exporting
im_signer = sy-uname
im_doc_txt = ‘SIGNING DOCUMENT 5159DB46ACFC1424E1000000010000AB’
im_doctype = ‘TXT’. commit work and wait.

More about Create a Digital Signature in ABAP (Sample Code) Here’s an example of how you can create a digital signature in ABAP.

REPORT z_digital_signature.

DATA: lv_private_key TYPE string VALUE ‘<your_private_key>’,
lv_certificate TYPE string VALUE ‘<your_certificate>’,
lv_data TYPE string VALUE ‘Sample data to be signed’,
lv_signature TYPE string.

DATA: lo_crypto_api TYPE REF TO if_cryptographic_services.

CREATE OBJECT lo_crypto_api.

TRY.
lo_crypto_api->sign(
EXPORTING
algorithm = if_cryptographic_services=>algorithm_sha256_rsa
private_key = lv_private_key
certificate = lv_certificate
data = lv_data
IMPORTING
signature = lv_signature
).
WRITE: ‘Digital signature:’, lv_signature.
CATCH cx_cryptographic_services INTO DATA(lx_crypto).
WRITE: ‘An error occurred:’, lx_crypto->get_text( ).
ENDTRY.

In the above code, replace <your_private_key> with the actual private key you want to use for signing, <your_certificate> with the certificate associated with the private key, and Sample data to be signed with the actual data you want to sign.

The code uses the if_cryptographic_services interface to perform the signing operation. It specifies the algorithm (SHA-256 with RSA) and provides the private key, certificate, and data to be signed. The resulting digital signature is stored in the lv_signature variable and is displayed on the screen.

Note that you’ll need to have the necessary cryptographic libraries and configurations set up in your ABAP system for this code to work properly. Additionally, make sure to handle error cases appropriately in your production code.

Leave a comment