Skip to content

Commit

Permalink
fix: convert to ascii before thirty-two encode
Browse files Browse the repository at this point in the history
  • Loading branch information
yeojz committed Jan 23, 2020
1 parent eb5271e commit e69c32a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Expand Up @@ -25,6 +25,7 @@ jobs:
run: |
npm run setup
npm run test
npm run test:extras -- --type=issue
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true

Expand Down
1 change: 0 additions & 1 deletion .prettierignore
@@ -1,7 +1,6 @@
.cache
builds
externals
extras
node_modules
package-lock.json
public
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -50,7 +50,7 @@
It implements both [HOTP][rfc-4226-wiki] - [RFC 4226][rfc-4226]
and [TOTP][rfc-6238-wiki] - [RFC 6238][rfc-6238],
and are tested against the test vectors provided in their respective RFC specifications.
These datasets can be found in the `packages/tests-data` folder.
These datasets can be found in the `tests/data` folder.

- [RFC 4226 Dataset][rfc-4226-dataset]
- [RFC 6238 Dataset][rfc-6238-dataset]
Expand Down
2 changes: 1 addition & 1 deletion packages/otplib-plugin-thirty-two/src/index.ts
Expand Up @@ -27,7 +27,7 @@ export const keyEncoder: KeyEncoder = (
encoding: KeyEncodings
): Base32SecretKey => {
return thirtyTwo
.encode(Buffer.from(secret, encoding).toString())
.encode(Buffer.from(secret, encoding).toString('ascii'))
.toString()
.replace(/=/g, '');
};
28 changes: 28 additions & 0 deletions tests/extras/issue-201.test.js
@@ -0,0 +1,28 @@
import { authenticatorGenerateSecret, KeyEncodings } from '@otplib/core';
import { createRandomBytes } from '@otplib/plugin-crypto';
import * as thirtyTwo from '@otplib/plugin-thirty-two';
import * as base32EncDec from '@otplib/plugin-base32-enc-dec';

[
{
name: 'thirty-two',
mod: thirtyTwo
},
{
name: 'base32-enc-dec',
mod: base32EncDec
}
].forEach(({ name, mod }) => {
const options = {
createRandomBytes,
encoding: KeyEncodings.HEX,
keyEncoder: mod.keyEncoder,
keyDecoder: mod.keyDecoder
};

test(`[${name}] generated secret should have consistent length`, async () => {
for (let i = 0; i < 20; i++) {
expect(authenticatorGenerateSecret(10, options)).toHaveLength(16);
}
});
});

0 comments on commit e69c32a

Please sign in to comment.