Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode func return the incorrect base32 string #381

Open
haleygu64 opened this issue Aug 22, 2024 · 1 comment
Open

Encode func return the incorrect base32 string #381

haleygu64 opened this issue Aug 22, 2024 · 1 comment

Comments

@haleygu64
Copy link

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

@berubenic
Copy link

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)];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants