Application Development and Automation 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: 
Read only

Gui_upload or Open Dataset

Former Member
0 Likes
1,159

Hi all,

I want to create a checksum for a file using the FM CALCULATE_HASH_FOR_RAW. When I read the file using OPEN DATASET and pass the content to the FM the hash is correct. Reading the file using  cl_gui_frontend_services=>gui_upload (either 'ASC','BIN','DAT') and pass the to the FM the hash is wrong.

The file is XML.

Sample code can be provided.

Any ideas?

Thanks in advance.

Gerald

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
981

I tried out gui_upload, and it is giving correct SHA1 checksum.

Below code calculates checksum of 'test\r\ntest2' string, and then it reads a text file that has 2 lines, first line having 'test' and second line having 'test2'.

The calculated checksum is 951C7EFC6413872A5E4B42853F44DD23AC22FE28 in both cases.

Result also matches with online hash generator.

DATA lv_string TYPE string VALUE 'test'.
CONCATENATE lv_string 'test2' INTO lv_string
  SEPARATED BY cl_abap_char_utilities=>cr_lf.

DATA v_data           TYPE xstring.
DATA v_hash           TYPE hash160.

v_data = cl_bcs_convert=>string_to_xstring( lv_string ).
CALL FUNCTION 'CALCULATE_HASH_FOR_RAW'
  EXPORTING
    data           = v_data
  IMPORTING
    hash           = v_hash
  EXCEPTIONS
    OTHERS         = 4.
WRITE:/ v_hash.

DATA lt_data TYPE TABLE OF string.
DATA ls_data TYPE string.
CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filename                = 'C:\temp\test.txt'
  TABLES
    data_tab                = lt_data
  EXCEPTIONS
    OTHERS                  = 17.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

CLEAR lv_string.
LOOP AT lt_data INTO ls_data.
  WRITE:/ ls_data.
  IF sy-tabix EQ 1.
    lv_string = ls_data.
  ELSE.
    CONCATENATE lv_string ls_data INTO lv_string SEPARATED BY cl_abap_char_utilities=>cr_lf.
  ENDIF.
ENDLOOP.


CLEAR v_data.
v_data = cl_bcs_convert=>string_to_xstring( lv_string ).
CALL FUNCTION 'CALCULATE_HASH_FOR_RAW'
  EXPORTING
    data           = v_data
  IMPORTING
    hash           = v_hash
  EXCEPTIONS
    OTHERS         = 4.
WRITE:/ v_hash.

Hi all,

I want to create a checksum for a file using the FM CALCULATE_HASH_FOR_RAW. When I read the file using OPEN DATASET and pass the content to the FM the hash is correct. Reading the file using  cl_gui_frontend_services=>gui_upload (either 'ASC','BIN','DAT') and pass the to the FM the hash is wrong.

The file is XML.

Sample code can be provided.

Any ideas?

Thanks in advance.

Gerald

5 REPLIES 5
Read only

Former Member
0 Likes
981

How far off is it?

Neal

Read only

0 Likes
981

Not sure if I understood you right. The correct hash is D019D317A24288999CB58FAB23016993, the wrong one is D41D8CD98F00B204E9800998ECF8427E.

Gerald

Read only

egenoves
Participant
0 Likes
981

Hi,

please keep in mind that i've not tested this , but the function you use has an import parameter DATA type XSTRING.

Maybe converting the data you upload from PC with the function SCMS_STRING_TO_XSTRING ??

I had some issues once with PDF's that solved with the above function....

Hope it helps

Regards

Read only

Former Member
0 Likes
982

I tried out gui_upload, and it is giving correct SHA1 checksum.

Below code calculates checksum of 'test\r\ntest2' string, and then it reads a text file that has 2 lines, first line having 'test' and second line having 'test2'.

The calculated checksum is 951C7EFC6413872A5E4B42853F44DD23AC22FE28 in both cases.

Result also matches with online hash generator.

DATA lv_string TYPE string VALUE 'test'.
CONCATENATE lv_string 'test2' INTO lv_string
  SEPARATED BY cl_abap_char_utilities=>cr_lf.

DATA v_data           TYPE xstring.
DATA v_hash           TYPE hash160.

v_data = cl_bcs_convert=>string_to_xstring( lv_string ).
CALL FUNCTION 'CALCULATE_HASH_FOR_RAW'
  EXPORTING
    data           = v_data
  IMPORTING
    hash           = v_hash
  EXCEPTIONS
    OTHERS         = 4.
WRITE:/ v_hash.

DATA lt_data TYPE TABLE OF string.
DATA ls_data TYPE string.
CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filename                = 'C:\temp\test.txt'
  TABLES
    data_tab                = lt_data
  EXCEPTIONS
    OTHERS                  = 17.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

CLEAR lv_string.
LOOP AT lt_data INTO ls_data.
  WRITE:/ ls_data.
  IF sy-tabix EQ 1.
    lv_string = ls_data.
  ELSE.
    CONCATENATE lv_string ls_data INTO lv_string SEPARATED BY cl_abap_char_utilities=>cr_lf.
  ENDIF.
ENDLOOP.


CLEAR v_data.
v_data = cl_bcs_convert=>string_to_xstring( lv_string ).
CALL FUNCTION 'CALCULATE_HASH_FOR_RAW'
  EXPORTING
    data           = v_data
  IMPORTING
    hash           = v_hash
  EXCEPTIONS
    OTHERS         = 4.
WRITE:/ v_hash.

Read only

0 Likes
981

Manish,

thank you very much. That code works for me and gives the same checksum for my file as the OPEN DATASET method.

Thanks for all your valuable responses!

Best Regards

Gerald