2013 Nov 19 1:51 PM
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
2013 Nov 19 2:56 PM
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
2013 Nov 19 2:00 PM
2013 Nov 19 2:14 PM
Not sure if I understood you right. The correct hash is D019D317A24288999CB58FAB23016993, the wrong one is D41D8CD98F00B204E9800998ECF8427E.
Gerald
2013 Nov 19 2:29 PM
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
2013 Nov 19 2:56 PM
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.
2013 Nov 20 6:52 AM
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
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |