Re: Trouble Decoding Base64 Text

2018-06-30 Thread Bernd Fröhlich via 4D_Tech
Dave Nasralla:

> Nevermind, though I'm not entirely sure this isn't a kind of bug.
> If you change the Encoding by adding two equal signs to the end
> "eyJ1c2VySWQiOiJiMDhmODZhZi0zNWRhLTQ4ZjItOGZhYi1jZWYzOTA0NjYwYmQifQ=="
> 
> It properly decodes to:
> {"userId":"b08f86af-35da-48f2-8fab-cef3904660bd"}

It´s not a 4D bug although 4Ds implementation could be better.
If you base64 encode your string the correct result includes the == at the end.
If you ommit them, then some decoders are more robust than 4Ds and can decode 
the string anyway but technically the string without the == ist not a properly 
base64 encoded string.

Greetings from Germany,
Bernd Fröhlich
**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: Trouble Decoding Base64 Text

2018-06-29 Thread Bruno LEGAY via 4D_Tech
Hi,

Base64 encoded data has a length which is multiple of 4.
If not, it should be padded with "=" (1 to 3 "=")

Your data is not strict base64 as it does not follow this.

Looks like 4D does not like when it is not padded.

// pad with "=" to make text a multiple of 4
C_LONGINT($vl_padModulo)
$vl_padModulo:=Mod(Length($vt_base64Encoded);4)
if ($vl_padModulo>0)
  $vt_base64Encoded:=$vt_base64Encoded+((4-$vl_padModulo)*"=")
End if

PS : there is a base64Url safe, where you should also do :

$vt_base64Encoded:=Replace string($vt_base64Encoded;"_";"/";*)  // convert "_" 
to "/"
$vt_base64Encoded:=Replace string($vt_base64Encoded;"-";"+";*)  // convert "-" 
to "+"

Bruno LEGAY
A&C Consulting


signature.asc
Description: Message signed with OpenPGP using GPGMail
**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: Trouble Decoding Base64 Text

2018-06-29 Thread Dave Nasralla via 4D_Tech
Nevermind, though I'm not entirely sure this isn't a kind of bug.
If you change the Encoding by adding two equal signs to the end
"eyJ1c2VySWQiOiJiMDhmODZhZi0zNWRhLTQ4ZjItOGZhYi1jZWYzOTA0NjYwYmQifQ=="

It properly decodes to:
{"userId":"b08f86af-35da-48f2-8fab-cef3904660bd"}

Some online decoding sites don't have the same issue. Like this site
does decode the original with the same outcome.
https://www.base64decode.org/

Just a bit confusing. It seems like 4D should throw and error or something.

dave


On Fri, Jun 29, 2018 at 8:06 AM, Dave Nasralla  wrote:
> Can anyone tell me what I am doing wrong here. In the code below I am
> getting back the same text I and trying to decode.
>
> C_BLOB($Blob_vx)
> C_TEXT($EncodedText_vt)
> $EncodedText_vt:="eyJ1c2VySWQiOiJiMDhmODZhZi0zNWRhLTQ4ZjItOGZhYi1jZWYzOTA0NjYwYmQifQ"
> TEXT TO BLOB($EncodedText_vt;$Blob_vx;UTF8 text without length)
> BASE64 DECODE($Blob_vx)
> $DecodedText_vt:=BLOB to text($Blob_vx;UTF8 text without length)
> ALERT($DecodedText_vt)
>
> I must be missing something...
>
> I am using v16R6 on Mac OS.
>
> --
> David Nasralla
> Clean Air Engineering



-- 
David Nasralla
Clean Air Engineering
**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**