Skip to content

Instantly share code, notes, and snippets.

@cbuckowitz
Last active May 18, 2022 02:34
Show Gist options
  • Save cbuckowitz/818a056d207eb07aa9847ed25d208eaf to your computer and use it in GitHub Desktop.
Save cbuckowitz/818a056d207eb07aa9847ed25d208eaf to your computer and use it in GitHub Desktop.
Create hash from multiple values #SAP #ABAP

The standard class CL_ABAP_MESSAGE_DIGEST can generate hashes for string or raw data.

This sample builds a hash from multiple input values.

DATA lo_digest TYPE Ref To cl_abap_message_digest.
DATA lv_timestamp TYPE timestampl.
DATA lv_int TYPE int4.
DATA lv_text TYPE string.
DATA lv_hash_string TYPE string.
DATA lv_hash_base64 TYPE string.
* prepare test data
GET TIME STAMP FIELD lv_timestamp.
lv_int = sy-uzeit+5(1).
lv_text = sy-uname.
* create a message digest object with a given hash algo
lo_digest = cl_abap_message_digest=>get_instance( 'sha1' ).
lo_digest->update( if_data = cl_abap_message_digest=>string_to_xstring( |{ lv_timestamp }| ) ).
lo_digest->update( if_data = cl_abap_message_digest=>string_to_xstring( |{ lv_int }| ) ).
lo_digest->update( if_data = cl_abap_message_digest=>string_to_xstring( |{ lv_text }| ) ).
lo_digest->digest( ).
lv_hash_string = lo_digest->to_string( ).
lv_hash_base64 = lo_digest->to_base64( ).
* output hashes
WRITE: / 'Hash string: ', lv_hash_string.
WRITE: / 'Base64 string: ', lv_hash_base64.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment