dirkx / base45-js

Base45 (for QR and Aztec barcode alphanumeric mode) library for Javascript/JS/node
2 stars 15 forks source link

Decoded value is not same as encoded one #3

Open raziel057 opened 3 years ago

raziel057 commented 3 years ago

Hello,

I notice that your code is not working correctly in some cases. For example here:

var test = new Uint8Array([120,156,75,244,51,201,14,14,45,9,41,203,1,0,18,222,3,161]);
console.log('Original text:  ' + test.toString());
test = base45.encode(test)
console.log('Base45 encoded: ' + test);
test = base45.decode(test)
console.log('Base45 decoded: ' + test);

Produce the following log:

Original text:  120,156,75,244,51,201,14,14,45,9,41,203,1,0,18,222,3,161
Base45 encoded: 6BF4R9RO6/Y19V5YC5V50FH2TK0
Base45 decoded: 120,156,75,244,51,201,14,14,45,9,41,203,0,256,18,222,3,161

Is it normal that the decoded Uint8Array is not equals to the one provided for encode ?

Please note that the decoded Uint8Array generates an error when using zlib.inflate() function from https://github.com/nodeca/pako, but not the original one:

zlib.inflate(new Uint8Array([120,156,75,244,51,201,14,14,45,9,41,203,0,256,18,222,3,161]))
node_modules\pako\lib\inflate.js:384
  if (inflator.err) throw inflator.msg || msg[inflator.err];
                    ^
incorrect data check
dirkx commented 3 years ago

This is absolutely a bug !

raziel057 commented 3 years ago

Thanks.

Please note that I don't have this issue if I use the following implementation: https://github.com/ehn-digital-green-development/base45-js

vitorpamplona commented 3 years ago

@dirkx can you merge from https://github.com/ehn-digital-green-development/base45-js and release a new version?

I can confirm the current version has several bugs (it fails to decode to the original payload in about 10% of the DGCs I create) and the version at EHN DGC seems to work flawlessly.

vitorpamplona commented 3 years ago

If you want to add another test case that fails in the production version but passes on the EHN DGC version

const LONG_BYTE_ARRAY = [120, 156, 187, 212, 226, 187, 136, 81, 141, 197, 195, 205, 124, 121, 245, 17, 125, 119, 134, 5, 145, 140, 190, 139, 217, 164, 18, 166, 48, 125, 96, 145, 74, 9, 189, 112, 192, 146, 145, 121, 33, 227, 146, 228, 178, 212, 162, 84, 67, 61, 3, 61, 131, 228, 188, 196, 220, 37, 73, 105, 121, 165, 41, 234, 142, 69, 197, 135, 119, 228, 21, 43, 232, 42, 148, 37, 230, 41, 120, 36, 230, 164, 230, 37, 165, 231, 229, 185, 21, 37, 230, 29, 94, 158, 159, 89, 172, 235, 149, 159, 152, 151, 156, 150, 87, 82, 232, 226, 24, 20, 236, 239, 23, 108, 19, 230, 232, 103, 227, 225, 232, 227, 234, 151, 156, 158, 87, 146, 235, 22, 228, 232, 231, 236, 239, 25, 108, 227, 229, 239, 232, 151, 156, 146, 159, 148, 101, 100, 96, 96, 169, 107, 96, 164, 107, 100, 145, 88, 210, 184, 42, 169, 36, 61, 211, 194, 196, 192, 212, 216, 210, 192, 192, 44, 169, 164, 36, 203, 39, 192, 200, 208, 220, 208, 210, 66, 215, 56, 169, 164, 40, 211, 200, 204, 192, 196, 208, 212, 192, 192, 32, 41, 55, 49, 197, 208, 200, 216, 40, 169, 56, 185, 66, 210, 200, 192, 200, 80, 215, 192, 68, 215, 208, 56, 196, 208, 196, 202, 200, 192, 202, 192, 64, 219, 0, 68, 38, 165, 20, 161, 203, 154, 0, 197, 13, 161, 178, 37, 201, 21, 82, 238, 238, 46, 10, 110, 69, 149, 197, 57, 135, 23, 229, 233, 40, 248, 232, 122, 164, 230, 100, 38, 231, 231, 149, 167, 166, 39, 37, 231, 39, 249, 249, 36, 101, 22, 231, 251, 102, 230, 101, 22, 151, 20, 85, 42, 228, 167, 41, 132, 133, 7, 39, 37, 103, 86, 200, 148, 22, 229, 89, 149, 150, 37, 103, 2, 77, 179, 242, 243, 177, 2, 26, 163, 111, 97, 232, 232, 232, 97, 104, 230, 24, 21, 225, 144, 171, 88, 115, 99, 217, 222, 71, 119, 85, 180, 51, 194, 11, 76, 221, 85, 36, 18, 103, 159, 250, 250, 187, 181, 168, 233, 141, 247, 158, 162, 235, 223, 60, 219, 124, 246, 9, 188, 182, 169, 88, 245, 81, 111, 71, 153, 227, 26, 93, 71, 213, 164, 8, 241, 234, 85, 31, 167, 50, 52, 247, 47, 89, 204, 52, 57, 26, 0, 43, 202, 143, 209]; 

describe("Field Failure", () => { 
    it("encode long byte array", () => strictEqual(b45.decode(b45.encode(LONG_BYTE_ARRAY)), LONG_BYTE_ARRAY));
});
pp-ps commented 3 years ago

@raziel057 I happened to experience the same issue, it should be fixed in latest main after 1c53b26.

vitorpamplona commented 3 years ago

I have had new decoding problems with 1c53b26, so I moved out to this library which seems to have everything fixed. https://www.npmjs.com/package/base45.

pp-ps commented 3 years ago

@vitorpamplona I have not caught any error yet, do you have a test case?

vitorpamplona commented 3 years ago

Sorry, not really. I was basically doing

try 
  oldVersion.decode()
catch 
  newVersion.decode()  

and when both of them were crashing I just gave up on the lib.