1   method request_signature_json.
  2     data: lv_json_md5   type string.
  3     data: lv_param      type string,
  4           lv_param_out  type xstring,
  5           lv_json_sha   type string.
  6 
  7     try.
  8 
  9       "JSON报文通过MD5加密:JSON+KEY -> MD5
 10       try.
 11         call method cl_abap_hmac=>calculate_hmac_for_char
 12           exporting
 13             if_algorithm     = 'MD5'
 14             if_key           = lv_param_out
 15             if_data          = iv_json
 16           importing
 17             ef_hmacstring    = lv_json_md5
 18             .
 19 
 20          translate lv_json_md5 to lower case.
 21 
 22          catch cx_abap_message_digest into data(lr_digest).
 23          raise exception type zcx_root
 24            exporting
 25              textid = value #( msgid  = lr_digest->if_t100_message~t100key-msgid
 26                                msgno  = lr_digest->if_t100_message~t100key-msgid
 27                                attr1  = lr_digest->if_t100_message~t100key-attr1
 28                                attr2  = lr_digest->if_t100_message~t100key-attr2
 29                                attr3  = lr_digest->if_t100_message~t100key-attr3
 30                                attr4  = lr_digest->if_t100_message~t100key-attr4
 31                                ).
 32       endtry.
 33 
 34       "key
 35       select single *
 36         into @data(ls_2005)
 37         from ztap2005
 38        where repid = @gv_repid.
 39 
 40       "时间戳:SAP日期时间转JAVA时间戳
 41       call method cl_pco_utility=>convert_abap_timestamp_to_java
 42         exporting
 43           iv_date      =  sy-datum
 44           iv_time      =  sy-uzeit
 45           iv_msec      =  000
 46         importing
 47           ev_timestamp =  data(lv_timestamp) .
 48 
 49       "Post/ + MD5加密Body报文 + uuid唯一值 + timestamp时间戳 + API key固定值
 50       lv_param =  'param=post/' &&
 51                   lv_json_md5 &&
 52                   '&uuid=' && iv_uuid &&
 53                   '&timeSpan=' && lv_timestamp &&
 54                   '&key=' && ls_2005-zprikey.
 55 
 56       "签名加密
 57       try.
 58         "需要Token(zpubkey)转XSTRING
 59         lv_json_sha = ls_2005-zpubkey.
 60         call method cl_abap_hmac=>string_to_xstring
 61           exporting
 62             if_input  = lv_json_sha
 63           receiving
 64             er_output = lv_param_out
 65             .
 66         catch cx_abap_message_digest into lr_digest.
 67          raise exception type zcx_root
 68            exporting
 69              textid = value #( msgid  = lr_digest->if_t100_message~t100key-msgid
 70                                msgno  = lr_digest->if_t100_message~t100key-msgid
 71                                attr1  = lr_digest->if_t100_message~t100key-attr1
 72                                attr2  = lr_digest->if_t100_message~t100key-attr2
 73                                attr3  = lr_digest->if_t100_message~t100key-attr3
 74                                attr4  = lr_digest->if_t100_message~t100key-attr4
 75                                ).
 76       endtry.
 77 
 78       try.
 79         "通过SHA256加密算法签名
 80         clear:lv_json_sha.
 81         call method cl_abap_hmac=>calculate_hmac_for_char
 82           exporting
 83             if_algorithm     = 'SHA256'
 84             if_key           = lv_param_out
 85             if_data          = lv_param
 86           importing
 87             ef_hmacstring    = lv_json_sha
 88             .
 89           translate lv_json_sha to lower case.
 90           catch cx_abap_message_digest into lr_digest.
 91           raise exception type zcx_root
 92             exporting
 93               textid = value #( msgid  = lr_digest->if_t100_message~t100key-msgid
 94                                 msgno  = lr_digest->if_t100_message~t100key-msgid
 95                                 attr1  = lr_digest->if_t100_message~t100key-attr1
 96                                 attr2  = lr_digest->if_t100_message~t100key-attr2
 97                                 attr3  = lr_digest->if_t100_message~t100key-attr3
 98                                 attr4  = lr_digest->if_t100_message~t100key-attr4
 99                                 ).
100       endtry.
101 
102       "签名
103       ev_signature =  'sign=' && lv_json_sha &&
104                       '&uuid=' && iv_uuid &&
105                       '&timeSpan=' && lv_timestamp &&
106                       '&key=' && ls_2005-zprikey.
107 
108 
109       catch cx_root into data(lr_root).
110         er_root = lr_root.
111     endtry.
112   endmethod.

 

posted on 2022-01-17 11:12  ricoo  阅读(1680)  评论(1编辑  收藏  举报