base64encode/DB replacing all "+" symbols with "space" ?

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

teriibi
Posts: 254
Joined: Mon Nov 13, 2017 3:49 pm
Location: Bolivia

base64encode/DB replacing all "+" symbols with "space" ?

Post by teriibi » Sat Feb 03, 2018 2:08 am

Hi,
I´m trying to compare two values generated using Hash and Base64encode, one local, the other retreived from a Sql Db.
In the one I retreive from DB, the plus symbol "+" is replaced by a "space". all other caracters of the string remains the same.
I googled and find various comments mentioning the same issue... :roll:

Can anyone tell me:
!) if they have also faced this issue in the past ? and
2) how to possibly get arround the problem - in LC - with a quick solution (other encoding maybe, change DB field format... :?: :?: :?:
(...without having to learn another coding language :!: :!: :lol:)

Here are the 2 strings, I store each in 2 fields before I start to compare them...
String 1

Code: Select all

k/DATXDPBQnk9efENHIc2+cWIPKFrzuObg3azWYthAUqUPbpelBdzs0VGno6M+yPc8D5+S3Y
eTnq9QaunubWJA==
String 2 (retreived from DB)

Code: Select all

k/DATXDPBQnk9efENHIc2 cWIPKFrzuObg3azWYthAUqUPbpelBdzs0VGno6M yPc8D5 S3Y
eTnq9QaunubWJA==
...All 3 "+" signs where replaced :!: :?: :?:

LC Comparing Code:

Code: Select all

on mouseup
   if fld "res1" is fld "res2" then
      put "Same string" into fld "info"
   else
      put "different strings" into fld "info"
   end if
end mouseup
REFERED SIMILAR ISSUE IN STACKOVERFLOW
I have a test entry in the database whose description is:
AAA `~!@#$%^&*()_+-={}|[]\:";'<>?,./ And wow this has a lot of special characters.
With the code above, for some reason when the request gets to the controller, the description loses the + sign (it becomes just a space).

teriibi
Posts: 254
Joined: Mon Nov 13, 2017 3:49 pm
Location: Bolivia

Base64encode unwanted character-switch : "+"<>"space" !

Post by teriibi » Sat Feb 03, 2018 2:23 pm

Hi,
I dont know - yet - if the base64Encode problem I am facing now is:
- a bug that LC can solve..or
- a bug from a third party software implied
- one from a Database limitation
...or just a missconfiguration from my script.
*viewtopic.php?f=11&t=30563

You can see the problem described above :
it was also related on StackOverflow* - but i could not understand how to apply a solution..if any yet.
(if its a bug from LC ...the above Topic could be moved here)

Script Code used to create the Hash locally:

Code: Select all

   ## Creating the Hash from tinput
   put messageDigest(tinput, "SHA3-512") into tOHash
   ## encoding to 64Encode
   put base64Encode(tOHash into datahashed
   put datahashed into fld "stringA"
   
   ## String hash is sent this way
    put "datahashed="&datahashed into Outgoing
   post Outgoing to url"http://www.myserver.com/addhash.php"
   ...
   
- Exact same code is used to create the 2nd HASH that is sent and stored on the MySql DB.
- DB field Format to store Hash uses VARCHAR (150)

Code: Select all

   ## Retreiving the previously stored Hash from the DB
   ## RecID is related to record´s ID
   post RecID to url "http://www.myserver.com/hash.php"
   put it into toParse
   ## Store received Hash into fiel for comparing Op. - String is NOT Decoded since its not needed.
   put item 1 of toParse into field "stringB"
   
   if fld "stringA" is "stringB" then
      put "Hashes are the same" into fld "info"
      ...
      
Strings displays fine in fileds but retreived String B is different from A since it now lacks ALL "+" signs originaly present.
Any real helping tips is welcomed. :?: :shock:

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: base64encode/DB replacing all "+" symbols with "space" ?

Post by FourthWorld » Sat Feb 03, 2018 7:09 pm

1. I've never seen a base64 string that included spaces. Could the database have a bug?

2. Why base64 a hash? Hex is a more commonly used representation.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: base64encode/DB replacing all "+" symbols with "space" ?

Post by jacque » Sat Feb 03, 2018 7:17 pm

It sounds like the db thinks the string is url encoded and is translating it before sending it back. There may be a way to tell the db not to do that. Or in the LC script, just do this:

replace space with "+" in tVar -- use your variable name
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Base64encode unwanted character-switch : "+"<>"space" !

Post by MaxV » Sat Feb 03, 2018 10:36 pm

The problem is you have to urlencode before sending by internet, otherwise special chars like + will transformed.
The urlencode change chars in safe transfer way.
Then you go back with urldecode.

For example:
urlencode("Ciao+") = "ciao%2B"
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

teriibi
Posts: 254
Joined: Mon Nov 13, 2017 3:49 pm
Location: Bolivia

Re: Base64encode unwanted character-switch : "+"<>"space" !

Post by teriibi » Sun Feb 04, 2018 12:43 pm

Oh, ok, my fault then...I ll try to fix that asap. :oops:
Tks

teriibi
Posts: 254
Joined: Mon Nov 13, 2017 3:49 pm
Location: Bolivia

Re: base64encode/DB replacing all "+" symbols with "space" ?

Post by teriibi » Sun Feb 04, 2018 5:05 pm

Hello ppl,

1rst things first.
someone is telling me that the script misses adding of the URLencode (which I am about to do now)

2nd...
since I thought it "could" be a bug, I then opened a Topic in the "possible bug" :
viewtopic.php?f=104&t=30567
This topic shows the script I used...

3rd : I m very new to this so..
to the question why Base64. ?
if i don t do it, I get all funny ???c??? character string that are stored in the DB field.
Using base64...everything display fine...just as it displays when I retreive each Value.

Could the DB have a Bug...dont know yet. I will first encodeURL and see if it keeps removing all "+"...
:wink:

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: base64encode/DB replacing all "+" symbols with "space" ?

Post by mwieder » Mon Feb 05, 2018 2:47 am

I see you have a response in the bugreport thread about urlencoding the string before posting it. That should solve the problem. The issue here is not with LiveCode or with the database or with base64 encoding itself, but with passing around information on the internet, where there are special characters that have a specific meaning there. Urlencoding the string before sending it to a remote database (and urldecoding it when you get it back) should take care of this. If not, post details here and we'll figure something else out.

teriibi
Posts: 254
Joined: Mon Nov 13, 2017 3:49 pm
Location: Bolivia

Re: base64encode/DB replacing all "+" symbols with "space" ?

Post by teriibi » Mon Feb 05, 2018 2:09 pm

Negativ news - applying URLencode / Urldecode steps doesnt solve the missing "+" :shock:
see steps taken in "Bug Post section please"

teriibi
Posts: 254
Joined: Mon Nov 13, 2017 3:49 pm
Location: Bolivia

Re: Base64encode unwanted character-switch : "+"<>"space" !

Post by teriibi » Mon Feb 05, 2018 2:13 pm

So, after applying the URlEncode and Decode steps, Im still gettin missing "+" replaced by empty spaces.

Would this have to do with the Database field ? I can´t think of another factor that could affect a value.
I dont think transfering converting data over the internet would "loose" a caracter in the process for this would have been noticed long time ago. meanwhile..using an unusual format combination for the DB "could" result in an unexpected bug...or am i going the wrong way here ? :shock:
DB field Format CHAR(128) UTF8_unicode_ci, also tested with Bynary and UTF16...
:!:

Modified script:

Code: Select all

   put messageDigest(tinput, "SHA3-512") into tOHash
   ## encoding to 64Encode
   put base64Encode(tOHash) into datahashed
    ## encoding to URLEncode
   put URLencode(datahashed) into dataU
   
   put datahashed into fld "stringA" 
   ##(not using the URL encoded value for comparision)
   
   ## String hash is sent this way
    put "datahash="&dataU into Outgoing
   post Outgoing to url"http://www.myserver.com/addhash.php"

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK
Contact:

Re: Base64encode unwanted character-switch : "+"<>"space" !

Post by dave.kilroy » Mon Feb 05, 2018 5:59 pm

Are you urlDecodeing in your php script on the server? If so then try without doing this step (your server should urldecode it without you doing anything)
"...this is not the code you are looking for..."

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: base64encode/DB replacing all "+" symbols with "space" ?

Post by FourthWorld » Mon Feb 05, 2018 6:07 pm

Threads merged.

The binary values returned from good hash functions are most commonly transmitted and stored as hexadecimal. LC's binaryConvert function can do this:

Code: Select all

function CleanHash pInput
   put messageDigest(pInput, "SHA3-512") into tOHash
   get binaryDecode("H*", tOHash, tHex)
   return tHex
end CleanHash
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

teriibi
Posts: 254
Joined: Mon Nov 13, 2017 3:49 pm
Location: Bolivia

Re: base64encode/DB replacing all "+" symbols with "space" ?

Post by teriibi » Mon Feb 05, 2018 8:22 pm

to Dave.
Nop, I m not doing anything on the server side. Php links the values to the DB, thats all.
The only encoding op..is made on the Client side from values it sends..
Then when needing to retreive decodes and compare...
i was sending it at first without Urlencoding..then used the Urlencode...which didnt make a diference.
in both case, "+" signs are replaced by a "space"...

teriibi
Posts: 254
Joined: Mon Nov 13, 2017 3:49 pm
Location: Bolivia

Re: base64encode/DB replacing all "+" symbols with "space" ?

Post by teriibi » Mon Feb 05, 2018 8:27 pm

To fourthWorld,
Are you suggesting that I should stop using base64encode and just replace it with:

binaryDecode("H*", tOHash, tHex)
...at uploading time ?
at both - uploading & downloading.?
..or just downloading time ?
:roll:

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK
Contact:

Re: base64encode/DB replacing all "+" symbols with "space" ?

Post by dave.kilroy » Mon Feb 05, 2018 8:30 pm

OK good (that you're not urldecoding on the server). I'm still suspicious that too many urlDecode() calls are being made because urldecode("secretText+") will produce "secretText ". When you retrieve text back from the db what does it look like before you urldecode() it in your app?
"...this is not the code you are looking for..."

Post Reply

Return to “Internet”