Encode Base64 doesn't return the correct value

Discussions relating to writing software in ILE RPG (RPG IV). This includes both fixed and free format RPG.
Scott Klement
Site Admin
Posts: 636
Joined: Sun Jul 04, 2021 5:12 am

Re: Encode Base64 doesn't return the correct value

Post 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.
hmauricio
Posts: 7
Joined: Fri Jul 01, 2022 3:09 pm

Re: Encode Base64 doesn't return the correct value

Post 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
ravi.as400
Posts: 2
Joined: Tue Sep 05, 2023 2:41 pm

Re: Encode Base64 doesn't return the correct value

Post 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
ravi.as400
Posts: 2
Joined: Tue Sep 05, 2023 2:41 pm

Re: Encode Base64 doesn't return the correct value

Post by ravi.as400 »

I am able to fix the code.. thank you all
Post Reply