lz-string icon indicating copy to clipboard operation
lz-string copied to clipboard

Somehow compressToBase64() returns string end with 3 equal sign "==="

Open linkenmin opened this issue 7 years ago • 12 comments

Somehow compressToBase64() returns string end with 3 equal sign "===", which is not a valid format by base64 definition. Does this behavior meet your expectation? Thank you.

Example source: abcdef

Result: IYIwxgJgpgZkA===

linkenmin avatar Dec 14 '17 08:12 linkenmin

The faulty code is switch (res.length % 4) { // To produce valid Base64 and the following switch statement. It should be a mod 3 with cases for 0, 1 and 2.

nickion avatar Dec 15 '17 13:12 nickion

Nice catch, thanks. Will fix soon.

pieroxy avatar Jan 07 '18 14:01 pieroxy

and thanks for the library; spent a while looking and testing libs for compressing local storage, and yours was by far the best overall that I found.

nickion avatar Jan 08 '18 10:01 nickion

Yes @pieroxy we love this lib too — will likely implement it when this Base64 bug is squashed. Use the Base64 in testing alot, thanks @linkenmin for the catch and @nickion for the fix!

GeekStocks avatar Jan 13 '18 17:01 GeekStocks

import lzstring


x = "N4IgLgngDgpiBcIBGAnAhgOwCYgDQgBs0EQYM8QBHASxIAYBaGAOSwAUAOAMzAHY0AYgHcAWtQCuADQD2WNAQBeADyRIhCgIIBFLABlpANQIARAEIBNABIQAVlwCi0gKoBZALwVK4mN4QBGfAB9Ej8/Og46EABfIAAA="

z = lzstring.LZString().decompressFromBase64(x)

y = lzstring.LZString().compressToBase64(z)

print(y)

print(x == y)

The padding is incorrect!

AmericanY avatar Mar 23 '21 00:03 AmericanY

@nickion Hello, could you please help me out to solve that?

AmericanY avatar Mar 23 '21 00:03 AmericanY

LZString works the other way: For any string, if you compress it and then decompress it, the original and resulting strings will be equal.

The padding changed in a version (I don't remember which one to be honest). What are you trying to achieve here ?

pieroxy avatar Mar 23 '21 05:03 pieroxy

@pieroxy Thanks for your response. Actually, am trying to decompress and compress the value to be equal together. as am trying to call an API with those value which require lzstring compress

AmericanY avatar Mar 23 '21 13:03 AmericanY

@pieroxy solved by installing pip install "lzstring==1.0.3"

AmericanY avatar Mar 23 '21 13:03 AmericanY

Great. There's probably a tiny bit of loss in performance, but this should be nothing to worry about.

pieroxy avatar Mar 23 '21 19:03 pieroxy

Discussion - https://github.com/pieroxy/lz-string/commit/64f2f29cf88aed1c61a11be5e5dc7fca26ed1b33#commitcomment-119254610

Rycochet avatar Jun 22 '23 12:06 Rycochet

Will create a second endpoint that has the correct base64 encoding, and mark the old one as deprecated (in Typescript typing, and a one-time console warning about the old one) - will be released as part of v2, and the other implementations will be poked to support the correct encoding, followed by the old endpoint being removed in v3 and correct encoding being the way forward (no idea of that release date, but not any time soon!)

Rycochet avatar Jun 22 '23 12:06 Rycochet