time2fa icon indicating copy to clipboard operation
time2fa copied to clipboard

Encode func return the incorrect base32 string

Open haleygu64 opened this issue 1 year ago • 1 comments

Details:

call Encode32(Buffer.from(2cwnayytkdv6k2z5rmw1h2tl6byt52q3)) expected: GJRXO3TBPF4XI23EOY3GWMT2GVZG25ZRNAZHI3BWMJ4XINJSOEZQ return: GJRXO3TBPF4XI23EOY3GWMT2GVZG25ZRNAZHI3BWMJ4XINJSOEZB

for (let i = 0; i < binary.length; i += 5) {
    const chunk = binary.substring(i, i + 5); //return incorrect binary in the end of input, i.e should return 10000 instead of 1
    base32 += BASE32_CHARS[parseInt(chunk, 2)];
  }

https://github.com/PlanetHoster/time2fa/blob/d3baa4214cdbca0547fffd1ee818a98faad04e0a/src/utils/encode.ts#L19C3-L22C4

haleygu64 avatar Aug 22 '24 09:08 haleygu64

Thank you for opening this issue.

The chunk should be padded when less than 5 bits.

https://datatracker.ietf.org/doc/html/rfc4648#section-3.2

for (let i = 0; i < binary.length; i += 5) {
    let chunk = binary.substring(i, i + 5);
  
    if (chunk.length < 5) {
        chunk = chunk.padEnd(5, '0');
    }

    base32 += BASE32_CHARS[parseInt(chunk, 2)];
}

berubenic avatar Nov 08 '24 04:11 berubenic