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

REST API - Convert XML to Base64 encoding with SHA 1 algorithm using Private key encryption

former_member778170
Discoverer
0 Likes
1,699
  • SAP Managed Tags

Hi All,

We have a new requirement to send XML data to the client url using REST API, which requires "base64 encoded signature value of the <Body> segment of the XML document, Hash algorithm used is SHA-1" which also uses the private and public keys to encrypt and decrypt.

By applying the below code we are able to generate XML body with the Base64 encoded along with SHA 1 algorithm,

But we are not able to figure out the use of private key to encrypt the XML Body, as our client needs public key to decrypt the same.

Could you advice how to achieve it, thank you.

data : P_INPUT TYPE STRING.

P_INPUT = '<Body><BillPaymentRequestMessage><BillPaymentRequest><BillPaymentRef>3666236</BillPaymentRef><SourceAccountNumber>2300829689940</SourceAccountNumber><BillAmount>219,56</BillAmount></BillPaymentRequest></BillPaymentRequestMessage></Body>'.


CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
TEXT = P_INPUT
IMPORTING
BUFFER = LV_XSTRING
EXCEPTIONS
FAILED = 1
OTHERS = 2.


LV_LEN = XSTRLEN( LV_XSTRING ).

CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
EXPORTING
INPUT = LV_XSTRING
IMPORTING
OUTPUT = LV_BASE64.

DATA ALG TYPE HASHALG.
DATA DATA TYPE STRING.
DATA LENGTH TYPE I.

DATA HASHB64STRING TYPE STRING.

CALL FUNCTION 'CALCULATE_HASH_FOR_CHAR'
EXPORTING
ALG = 'SHA1'
DATA = LV_BASE64
LENGTH = LV_LEN
IMPORTING
HASHB64STRING = HASHB64STRING
EXCEPTIONS
UNKNOWN_ALG = 1
PARAM_ERROR = 2
INTERNAL_ERROR = 3
OTHERS = 4.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

1 REPLY 1
Read only

former_member778170
Discoverer
0 Likes
1,254
  • SAP Managed Tags

Hi Mr. Robert,

Thanks a lot for sharing this,

As per your explanation, I tried with xml body as massage and passing public and private keys same as explained, result am passing to

CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'

CALL FUNCTION 'CALCULATE_HASH_FOR_CHAR'

while testing third party url on SOAP UI, invalid signature error coming.

My issue is how to applying "Base64 encoded signature value of the <Body> segment of the XML document, HASH algorithm used is SHA1."

Can you help me ,

Thanks in advance.