Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Using SAP Abap class CL_ABAP_MESSAGE_DIGEST to get sha256

tuliovargas
Participant
0 Kudos
2,561

Hi experts,

I was wondering why I am getting a different result from a zip file when I try to convert it to hash SHA256.

I am using the function CALCULATE_HASH_FOR_CHAR and class CL_ABAP_MESSAGE_DIGEST to obtain the hash from a file, it is working when I test it using a simples file text.txt; however, when I zip the text.txt then the hash expected is coming with a wrong hash.

test.txt

content: test123

Expected hash: 56a7010456b474aeee111f3b7336581fb0a99129d426cf51903efbdfd629f008

Result: 56A7010456B474AEEE111F3B7336581FB0A99129D426CF51903EFBDFD629F008

TEST.zip

expected hash: da03ff10b45774ad1890d060575c48fc86842c9616410ec2c40f19227780fa63

Result: B0AEEA3B970589317B485F21BBF8AF6D0AD40DAF0758DB8F071219EFBC7108E3

*DATA itab TYPE TABLE OF string .
CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filetype = 'ASC'
    filename = 'C:\file\TEST.zip'
  TABLES
    data_tab = itab.
data lt_concat TYPE string.
concatenate lines of itab into LT_CONCAT.
TRY.
    lo_digest = cl_abap_message_digest=>get_instance( 'sha-256' ).
    "update digest with input
    converter = cl_abap_conv_out_ce=>create( ).
    converter->convert(
      EXPORTING
        data   = lt_concat
      IMPORTING
        buffer = data_as_xstring
    ).
    lo_digest->update( if_data = data_as_xstring ).
    "finalise digest
    lo_digest->digest( ).
    hash_value = lo_digest->TO_STRING( ).
  CATCH cx_abap_message_digest.
    CLEAR hash_value.
  CATCH cx_sy_codepage_converter_init
        cx_sy_conversion_codepage
        cx_parameter_invalid_type.
    CLEAR hash_value.
ENDTRY.

1 ACCEPTED SOLUTION

touzik_itc
Active Participant
2,195

I suppose, because you uploading binary file as a ASCII file, try filetype = 'BIN'.

2 REPLIES 2

touzik_itc
Active Participant
2,196

I suppose, because you uploading binary file as a ASCII file, try filetype = 'BIN'.

0 Kudos
2,195

Thanks touzik_itc