开发者

Calculating SHA2 checksum on string in ABAP

开发者 https://www.devze.com 2023-03-19 04:02 出处:网络
I am trying to calculate a SHA2 checksum on a string in ABAP. I have come across the functions CALCULATE_HASH_FOR_CHAR and CALCULATE_HMAC_FOR_CHAR. However, CALCULATE_HASH_FOR_CHAR only can calculate

I am trying to calculate a SHA2 checksum on a string in ABAP. I have come across the functions CALCULATE_HASH_FOR_CHAR and CALCULATE_HMAC_FOR_CHAR. However, CALCULATE_HASH_FOR_CHAR only can calculate a SHA1 (entering SHA2 returns nothing).

By contrast, CALCULATE_HMAC_FOR_CHAR seems to rely on entries maintained in the SecureStorage, so this is not really helpful to me (and I'm not sure it will give me the results I need).

Also, after seeing how FM SSFH_F4_HASHALG returns possible values for hash algorithms, it seems that the possible values are dependent on the开发者_Go百科 version of sapseculib that you have installed.

Any ideas how else I can calculate a SHA2 hash in ABAP?


OK, it seems that the answer is to use class CL_ABAP_MESSAGE_DIGEST (and then specify SHA256 as the algorithm). This information is in note 1410294 (Support SHA2-family for Message Digest and HMAC) and requires a certain kernel patch level etc.


Assuming your kernel is up-to-date (SAP Note) you can use the SHA512 algorithm as follows:

DATA result type string.
TRY.
  cl_abap_message_digest=>calculate_hash_for_char(
    EXPORTING
      if_algorithm = 'SHA512'
      if_data      = 'My String to Hash'
    IMPORTING
      ef_hashstring = result
  ).
CATCH cx_root.
  " Eh, what're you gonna do?
ENDTRY.
0

精彩评论

暂无评论...
验证码 换一张
取 消