[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.

[ABAP] Passwort generieren

PARAMETERS: p_length TYPE i DEFAULT 20. " Passwortlänge
PARAMETERS: p_alpha TYPE string LOWER CASE DEFAULT 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_'. " Passwortzeichensatz
  
START-OF-SELECTION.
  DATA: lv_pw TYPE string.
  DATA(r_sel) = cl_abap_random_int=>create( seed = cl_abap_random=>seed( ) min = 0 max = strlen( p_alpha ) - 1 ).

  DO p_length TIMES.
    lv_pw = |{ lv_pw }{ substring( val = p_alpha off = r_sel->get_next( ) len = 1 ) }|.
  ENDDO.

  WRITE: / 'Passwort: ', lv_pw.

[ABAP] Base64 Encoding / Decoding

Variante 1 (cl_http_utility)

DATA: lv_bas64enc TYPE string VALUE 'Teststring'.
DATA: lv_base64dec TYPE string.

WRITE: / 'Plain:', lv_bas64enc.

cl_http_utility=>encode_base64( EXPORTING
                                  unencoded = lv_bas64enc
                                RECEIVING
                                  encoded   = lv_bas64enc ).

WRITE: / 'Base64 enc:', lv_bas64enc.

cl_http_utility=>decode_base64( EXPORTING
                                  encoded = lv_bas64enc
                                RECEIVING
                                  decoded = lv_base64dec ).

WRITE: / 'Base64 dec:', lv_base64dec.

Variante 2 (cl_hard_wired_encryptor)

DATA: text TYPE string VALUE 'TestString.'.

START-OF-SELECTION.

  WRITE: / text.

  DATA(o_base64) = NEW cl_hard_wired_encryptor( ).

  DATA(enc_string) = o_base64->encrypt_string2string( text ).
  WRITE: / 'Base64 encrypt_string2string: ', enc_string.

  DATA(dec_string) = o_base64->decrypt_string2string( enc_string ).
  WRITE: / 'Base64 decrypt_string2string: ', dec_string.

  DATA(enc_xstring) = o_base64->encrypt_string2bytes( dec_string ).
  WRITE: / 'Base64 encrypt_string2bytes:  ', enc_xstring.

  DATA(enc_bytes) = o_base64->encrypt_bytes2bytes( enc_xstring ).
  WRITE: / 'Base64 encrypt_bytes2bytes:   ', enc_bytes.

  DATA(dec_bytes) = o_base64->decrypt_bytes2bytes( enc_bytes ).
  WRITE: / 'Base64 decrypt_bytes2bytes:   ', dec_bytes.

  DATA(dec_string2) = o_base64->decrypt_bytes2string( dec_bytes ).
  WRITE: / 'Base64 decrypt_bytes2string:  ', dec_string2.

Links

[ABAP] MD5, SHA1, SHA256, SHA384, SHA512 HASH-Summen berechnen

Variante 1 (Strings)

* MD5, SHA1, SHA256, SHA384, SHA512
DATA(lv_algo) = |MD5|.
* zu kodierende String-Daten
DATA(lv_data) = |Teststring|.

* Hash-Wert als Hex-Encoded String
DATA: lv_hashstring TYPE string.
* Hash-Wert binär als XString
DATA: lv_hashxstring TYPE xstring.
* Hash-Wert als Base64-Encoded String
DATA: lv_hashb64string TYPE string.

TRY.
* Generische Hash-Funktion (Char-Input)
    cl_abap_message_digest=>calculate_hash_for_char( EXPORTING
                                                       if_algorithm     = lv_algo
                                                       if_data          = lv_data
                                                     IMPORTING
                                                       ef_hashstring    = lv_hashstring
                                                       ef_hashxstring   = lv_hashxstring
                                                       ef_hashb64string = lv_hashb64string ).
    cl_demo_output=>write_data( lv_algo ).
    cl_demo_output=>write_data( lv_data ).
    cl_demo_output=>write_data( lv_hashstring ).
    cl_demo_output=>write_data( lv_hashxstring ).
    cl_demo_output=>write_data( lv_hashb64string ).
    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) = |MD5|.
* 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 ).

* Hash-Wert als Hex-Encoded String
DATA: lv_hashstring TYPE string.
* Hash-Wert binär als XString
DATA: lv_hashxstring TYPE xstring.
* Hash-Wert als Base64-Encoded String
DATA: lv_hashb64string TYPE string.

TRY.
* Generische Hash-Funktion (XString)
    cl_abap_message_digest=>calculate_hash_for_raw( EXPORTING
                                                      if_algorithm     = lv_algo
                                                      if_data          = lv_datax
                                                    IMPORTING
                                                      ef_hashstring    = lv_hashstring
                                                      ef_hashxstring   = lv_hashxstring
                                                      ef_hashb64string = lv_hashb64string ).
    cl_demo_output=>write_data( lv_algo ).
    cl_demo_output=>write_data( lv_data ).
    cl_demo_output=>write_data( lv_datax ).
    cl_demo_output=>write_data( lv_hashstring ).
    cl_demo_output=>write_data( lv_hashxstring ).
    cl_demo_output=>write_data( lv_hashb64string ).
    cl_demo_output=>display( ).
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] Zufallszahlen erzeugen

Variante 1 (cl_abap_random)

* int - Zufallszahlen von 0...100 erzeugen
DATA(o_rand_i) = cl_abap_random_int=>create( seed = cl_abap_random=>seed( ) min = 0 max = 100 ).
DO 10 TIMES.
  WRITE: / o_rand_i->get_next( ).
ENDDO.

DATA(o_rand) = cl_abap_random=>create( cl_abap_random=>seed( ) ).
DO 10 TIMES.
  WRITE: / o_rand->intinrange( low = 0 high = 100 ).
ENDDO.

* float - Zufallszahlen im Bereich von 0...1 erzeugen
DATA(o_rand_f) = cl_abap_random_float=>create( seed = cl_abap_random=>seed( ) ).
DO 10 TIMES.
  WRITE: / |{ o_rand_f->get_next( ) NUMBER = ENVIRONMENT }|.
ENDDO.

DATA(o_rand) = cl_abap_random=>create( cl_abap_random=>seed( ) ).
DO 10 TIMES.
  WRITE: / |{ o_rand->float( ) NUMBER = ENVIRONMENT }|.
*  WRITE: / |{ o_rand->decfloat16( ) NUMBER = ENVIRONMENT }|.
*  WRITE: / |{ o_rand->decfloat34( ) NUMBER = ENVIRONMENT }|.
ENDDO.

Variante 2 (cl_random_number)

DATA(o_rnd) = NEW cl_random_number( ).
o_rnd->if_random_number~init( ).

DO 10 TIMES.
* Integer-Zahlen von 0 ... 100
  DATA(lv_i) = o_rnd->if_random_number~get_random_int( 100 ).
  WRITE: / lv_i.
ENDDO.

DO 10 TIMES.
* Float-Zahlen von 0 ... 1
  DATA(lv_f) = o_rnd->if_random_number~get_random_float( ).
  WRITE: / |{ lv_f NUMBER = ENVIRONMENT }|.
ENDDO.