Page 2 of 2

Re: Encode Base64 doesn't return the correct value

Posted: Mon Jul 04, 2022 4:34 am
by Scott Klement
You have to pass the correct length. Don't expect RPG to figure it out for you with %LEN (or %SIZE). The data isn't text, it's binary data. You need to know the length of the signature, and then pass that to base64_encode.

Re: Encode Base64 doesn't return the correct value

Posted: Mon Jul 04, 2022 10:55 am
by hmauricio
jonboy49 wrote: Fri Jul 01, 2022 5:36 pm The signature length is being returned to by Qc3CalculateSignature. Any reason you cannot use that ?
Hello,
that was the problem, it's ok now.

Thank you so much for your help.

Best regards

Re: Encode Base64 doesn't return the correct value

Posted: Tue Sep 05, 2023 3:30 pm
by ravi.as400
I'm working jwt token for one of our vendor. since this post is related to JWT signature. I am trying to start the same thread..
I am struck at the signature part. I am able to convert the header and Payload (Claim set), I am unable to get the Signature part. I need help. I was using the same code provided by hmauricio. I am using the same Qc3CalculateSignature with signature is defined as ( dcl-s signature char(512) ccsid(*hex);)

I got the Private key converted from .PEM to .DAR in the keystore.
Here is my code for the signature..

BegSr $JWTSign;
keyDescription = *allx'00';
keyDescription.keystoreFile = 'KEYSTORE';
keyDescription.keystoreLibrary = '*LIBL';
keyDescription.recordLabel = 'PROS_PRICEAPI'; //PROS_PRICEAPI

algorithmDescription = *allx'00';
algorithmDescription.publicKeyCipherAlgorithm
= CRYPTO_ALGORITHM_RSA;
algorithmDescription.pkaBlockFormat = '1';
algorithmDescription.signingHashAlgorithm = HASH_ALGORITHM_SHA256;
Payload = %Trim($Rsp_Hdr) + '.'+ %Trim($Rsp_Clm);

Qc3CalculateSignature(
payload : %len(payload) : 'DATA0100' :
algorithmDescription : 'ALGD0400' :
keyDescription : 'KEYD0400' :
ANY_CRYPTO_SRV : '' :
signature : %size(signature) :
signatureLength : stdError );

// test code to to check what this code returns..
myBase64DataLen = apr_base64_encode_binary(myBase64Data
:signature
:signatureLength);
// would like to use this code..
myBase64DataLen = base64_encode(%addr(signature)
: signatureLength
: %addr(AsciiData)
: %size(AsciiData) );

$Rsp_Sign = %trim(AsciiData);

EndSr;
return $Rsp_Sign does not match the signature returned from jwt.io.
I need help here.

thanks
Ravi

Re: Encode Base64 doesn't return the correct value

Posted: Wed Sep 06, 2023 9:33 pm
by ravi.as400
I am able to fix the code.. thank you all