discrepency between zlib and pako functions
See original GitHub issueHello I have in my back end a simple system for encrypting and compressing data.
Backend NodeJS
export const aesEncrypt = (text: string, key: string = ENCRYPTION_KEY) => {
let iv = randomBytes(IV_LENGTH);
let cipher = createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
export const textCompressToBase64Url = (text: string) => {
return deflateSync(text).toString('base64url')
}
export const packDataForUrl = (data: string, key: string = ENCRYPTION_KEY) => {
return textCompressToBase64Url(aesEncrypt(data, key))
}
Result string:
eJwVU8cRxDAIbIkowXUjUv8lHP55BoM2Bp_W59hmGWH0DrxKpMlumMlfgAKD2DBKEx0gJs9j2ll-D7Gfl2PAgn1dPN696eDor3dWdR4iHH3hGQRKqKVy790Dneo7VZMEelNVag-n4rKAjhn4NNCiMmqwQ7QHMjgfkk0Rd3GlyviJuxz2zJWh6-_oAd4z9WHQPMhXgQ7P3LkegZHMt9-8h_IA-qSfm5LVIxn3PMJ0DEZSefcNW4CVKMluHkfhqjdIyXSPPWdeJGeJUWjInVq4E3tCClEQq2NSDFfA1QjSi71twSRRoMMk7GtceDIzTCHm9iqjFcB3D0U-N6tTIdnnkpnf_VpHDBJ5VnVZas7DQMVUrUu5TWXZV81j378DjVsYsGhdI7j7my6mFT6HH5dxxusT6_CiKpISF-u3A8U4O-aguJbnMapTBa0R8qEf6iK_GsZoZ89ZuTkBNoCazYWwUR8nQ0jBq9xu6Vr70nMKwAHeXWSNhI4qOS6QjSvtDg_iLoQVRmfAy_RVv3yN7buUFS6MiKCf0yVsgqr8KqYYXf0zR56utsdzXLFGauMhJ7QggHSphe0bwH1DyuQJT-DGAyfgUltmP4JcPUJ0akPPB0NTW6bV-61OIGcSV7N1zcik1_67QIH1c6i3An5X41WgZZfugnkWRxYF-2LxMuskueL8dNV_z7Ub5StKPVmZ2e52Y5M3I905SlaVdG2rF9X5dFg307uxiak2vgYSruvggU07bhP5wV273c6-Z65Zd_mjb0HXXs7pe-mrA-lsHzQZNvp8z2jt4rrID89spGGlyLdsagOyWd-eX13TX1DmNhnRdkMzt0NRHivqORe3nH_9zDtg
I then try to use Pako
in the frontend to deflate before doing a AES decrypt however i keep getting various errors:
Below is how I convert the base64url to base64 (this works fine)
export const textDecompressFromBase64Url = (text: string) => {
//@ts-ignore
const sanitizedbase64 = text.replace(/_/g, '/').replace(/-/g, '+')
const compressData = sanitizedbase64.split('').map((e) => {
return e.charCodeAt(0);
});
const binData = new Uint8Array(compressData);
const inflated = inflate(binData, { raw: true }).toString()
return inflated
}
If I don’t use { raw: true }
I get the following error in the inflate method:
incorrect header check
If I pass the {raw: true}
I get the following:
invalid code lengths set
In the backend however i’m able to inflate with zlib with no issue:
export const textDecompressFromBase64Url = (text: string) => {
return inflateSync(Buffer.from(text, 'base64')).toString()
}
Issue Analytics
- State:
- Created a year ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
Plans to upgrade Pako to be equivalent to zlib 1.2.11? #152
I'm having some issues getting Pako's gzip to work interchangeably with Node's. I doubt this is related to the version difference, ...
Read more >Compressed with pako(zlib in javascript), decompressing with ...
The code that does the compression in javascript using pako(https://github.com/nodeca/pako)Pako. It compresses string 't'
Read more >pako 2.1.0 API documentation
Why pako is cool: Results are binary equal to well known zlib (now contains ported zlib v1.2.8). Almost as fast in modern JS...
Read more >fflate - npm
fflate (short for fast flate) is the fastest, smallest, and most versatile pure JavaScript compression and decompression library in existence, ...
Read more >How to use the pako.gzip function in pako - Snyk
How to use the pako.gzip function in pako. To help you get started, we've selected a few pako examples, based on popular ways...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
FWIW I tested this and it works fine equally with
fflate.unzlibSync(data)
andpako.inflate(data)
.See https://github.com/nodeca/pako/blob/master/support/build_fixtures.js#L8-L10 (node should be < v12.17.0). This repo really has appropriate tests. You can just add your fixture, and if problem exists - this will be easy proof of no code mistake at your side.