Encode Base64 doesn't return the correct value
-
- Site Admin
- Posts: 674
- Joined: Sun Jul 04, 2021 5:12 am
Re: Encode Base64 doesn't return the correct value
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.
-
- Posts: 2
- Joined: Tue Sep 05, 2023 2:41 pm
Re: Encode Base64 doesn't return the correct value
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
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
-
- Posts: 2
- Joined: Tue Sep 05, 2023 2:41 pm
Re: Encode Base64 doesn't return the correct value
I am able to fix the code.. thank you all