Skip to Content
Technical Articles
Author's profile photo Birzhan Moldabayev

Nested data hash calculating

ZCL_EUI_CRC64 class

Source code in github

Why do we need another class?
ABAP has a lot of methods to calutae md5, sha1 or sha256 hashes. But there are some limitation with:

  • Data references
  • Objects
  • Strings & xStrings
  • Tables with complex data types

Main methods

Usage

DATA(lo_crc64) = NEW zcl_eui_crc64( ).
lo_crc64->add_to_hash( zsaqo3_general_info ).
lo_crc64->add_to_hash( mt_fld_value ).

rv_hash = lo_crc64->get_hash( ).

Data references

By default mc_dref-data_value, you can calculate and add referenced data to the resulting hash.
Also you can skip them mc_dref-no_info or just compare types mc_dref-type_info.


Objects
By default public attributes values are added to the resulting hash.
If you also want to calculate private attributes hashes just add ZCL_EUI_CRC64 to friends.


Strings & xStrings

Processing these data types a little bit tricky. First of all SHA1 hashes are calculating via

CALL FUNCTION 'CALCULATE_HASH_FOR_CHAR'

CALL FUNCTION 'CALCULATE_HASH_FOR_RAW'

respectively. And then these hashes are added to the final result.


Tables with complex data types

Since structures fields and table rows can contain all above mentioned data types, they are processed separately and the resulting hashes are added to the final result.


Show logs

If you want to compare very complex data structures you can pass iv_log = abap_true parameter to the ZCL_EUI_CRC64 constructor, and see which fielde are differ.

Example
SE38 -> ZEUI_TEST_CRC64

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Marco Wahler
      Marco Wahler

      Thanks for sharing...

      I was wondering if/how this could be enhanced in order to calculate Hash sum for a file (AL11) out of ABAP?

       

      Cheers Marco

      Author's profile photo Birzhan Moldabayev
      Birzhan Moldabayev
      Blog Post Author

      Hi Marco

      Yes sure

      for "ordinary" files in user file system you can use the following code

      " From presentation server
      TRY.
          DATA(lo_file) = NEW zcl_eui_file(
          )->import_from_file( iv_full_path = 'C:\Users\moldab\Desktop\TEST.txt' ).
        CATCH zcx_eui_exception INTO DATA(lo_error).
          MESSAGE lo_error TYPE 'S' DISPLAY LIKE 'E'.
          RETURN.
      ENDTRY.
      
      WRITE NEW zcl_eui_crc64(
            )->add_to_hash( lo_file->mv_xstring
            )->get_hash( ).

       

      Unfortunately ZCL_EUI_FILE class can only download file via to_app_server( ) method

      You can use OPEN, READ, CLOSE DATASET for this purpose

      " For application server
      DATA(lv_server_path) = |/tmp/.sapstartsrv98_sapstartsrv.log|.
      OPEN DATASET lv_server_path FOR INPUT IN BINARY MODE.
      
      DATA(lv_file_data) = VALUE xstring( ).
      READ DATASET lv_server_path INTO lv_file_data LENGTH DATA(lv_len).
      
      CLOSE DATASET lv_server_path.
      CHECK lv_len IS NOT INITIAL.
      
      WRITE / NEW zcl_eui_crc64(
            )->add_to_hash( lv_file_data
            )->get_hash( ).

       

      ---------------------------------------------------------------

      Recently I used zcl_eui_crc64 class to detect if something was changed in the state of an app to activate SAVE button in PBO event

      https://github.com/bizhuka/aqo/blob/master/src/zaqo_editor3_editor.prog.abap#L700