[ABAP] Message Authentication Code (MAC) mit verschiedenen HASH-Algorithmen (MD5, SHA1, SHA256, SHA384, SHA512) berechnen

Variante 1 (Strings)

* MD5, SHA1, SHA256, SHA384, SHA512
DATA(lv_algo) = |SHA256|.
* Key
DATA(lv_key) = |MyKey1|.
* Key als XSTRING
DATA(lv_keyx) = cl_abap_hmac=>string_to_xstring( lv_key ).
* zu kodierende String-Daten
DATA(lv_data) = |Teststring|.

* HMAC-Wert als Hex-Encoded String
DATA: lv_hmacstring TYPE string.
* HMAC-Wert binär als XString
DATA: lv_hmacxstring TYPE xstring.
* HMAC-Wert als Base64-Encoded String
DATA: lv_hmacb64string TYPE string.

TRY.
* Generische HMAC-Funktion (Char-Input)
    cl_abap_hmac=>calculate_hmac_for_char( EXPORTING
                                             if_algorithm     = lv_algo
                                             if_key           = lv_keyx
                                             if_data          = lv_data
                                           IMPORTING
                                             ef_hmacstring    = lv_hmacstring
                                             ef_hmacxstring   = lv_hmacxstring
                                             ef_hmacb64string = lv_hmacb64string ).
    cl_demo_output=>write_data( lv_algo ).
    cl_demo_output=>write_data( lv_key ).
    cl_demo_output=>write_data( lv_keyx ).
    cl_demo_output=>write_data( lv_data ).
    cl_demo_output=>write_data( lv_hmacstring ).
    cl_demo_output=>write_data( lv_hmacxstring ).
    cl_demo_output=>write_data( lv_hmacb64string ).
    cl_demo_output=>display( ).
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 2 (Bytefolgen / RAW)

* MD5, SHA1, SHA256, SHA384, SHA512
DATA(lv_algo) = |SHA256|.
* Key
DATA(lv_key) = |MyKey1|.
* Key als XSTRING
DATA(lv_keyx) = cl_abap_hmac=>string_to_xstring( lv_key ).
* zu kodierende String-Daten
DATA(lv_data) = |Teststring|.
* zu kodierende String-Daten als XSTRING
DATA(lv_datax) = cl_abap_message_digest=>string_to_xstring( lv_data ).

* HMAC-Wert als Hex-Encoded String
DATA: lv_hmacstring TYPE string.
* HMAC-Wert binär als XString
DATA: lv_hmacxstring TYPE xstring.
* HMAC-Wert als Base64-Encoded String
DATA: lv_hmacb64string TYPE string.

TRY.
* Generische HMAC-Funktion (XString)
    cl_abap_hmac=>calculate_hmac_for_raw( EXPORTING
                                            if_algorithm     = lv_algo
                                            if_key           = lv_keyx
                                            if_data          = lv_datax
                                          IMPORTING
                                            ef_hmacstring    = lv_hmacstring
                                            ef_hmacxstring   = lv_hmacxstring
                                            ef_hmacb64string = lv_hmacb64string ).
    cl_demo_output=>write_data( lv_algo ).
    cl_demo_output=>write_data( lv_key ).
    cl_demo_output=>write_data( lv_keyx ).
    cl_demo_output=>write_data( lv_data ).
    cl_demo_output=>write_data( lv_datax ).
    cl_demo_output=>write_data( lv_hmacstring ).
    cl_demo_output=>write_data( lv_hmacxstring ).
    cl_demo_output=>write_data( lv_hmacb64string ).
    cl_demo_output=>display( ).
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Kommentare sind geschlossen.