Skip to content

Commit 2df1343

Browse files
committed
docs(totp): add string secrets and authenticator compatibility notes to README
1 parent 5d88575 commit 2df1343

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

packages/totp/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,44 @@ const token = await generate({
5151
});
5252
```
5353

54+
#### With raw string secrets
55+
56+
If your secret is a plain string (e.g. a passphrase), convert it to `Uint8Array` first using `stringToBytes` from `@otplib/core`:
57+
58+
```typescript
59+
import { generate } from "@otplib/totp";
60+
import { crypto } from "@otplib/plugin-crypto-node";
61+
import { stringToBytes } from "@otplib/core";
62+
63+
const token = await generate({
64+
secret: stringToBytes("mysecretpassphrase"),
65+
crypto,
66+
});
67+
```
68+
69+
Alternatively, [`@otplib/plugin-base32-alt`](https://www.npmjs.com/package/@otplib/plugin-base32-alt) provides bypass plugins for secrets in other encodings:
70+
71+
```typescript
72+
import { generate } from "@otplib/totp";
73+
import { crypto } from "@otplib/plugin-crypto-node";
74+
import { bypassAsHex } from "@otplib/plugin-base32-alt";
75+
76+
const token = await generate({
77+
secret: "48656c6c6f", // hex-encoded secret
78+
base32: bypassAsHex,
79+
crypto,
80+
});
81+
```
82+
83+
| Name | Input format |
84+
| ---------------- | ---------------- |
85+
| `bypassAsString` | Raw UTF-8 string |
86+
| `bypassAsHex` | Hex string |
87+
| `bypassAsBase64` | Base64 string |
88+
89+
> [!NOTE]
90+
> Raw string and bypass secrets are **not compatible** with authenticator apps or `otpauth://` URIs, which always expect Base32-encoded secrets.
91+
5492
### verify
5593

5694
Verify a TOTP code:
@@ -117,6 +155,19 @@ const token = generateSync({ secret, crypto });
117155
const result = verifySync({ secret, token, crypto });
118156
```
119157

158+
## Compatibility with Authenticator Apps
159+
160+
Most authenticator apps (Google Authenticator, Authy, Microsoft Authenticator, 1Password, etc.) use the following defaults:
161+
162+
| Parameter | Compatible Value | Notes |
163+
| ----------- | ---------------- | ------------------------------------------- |
164+
| `algorithm` | `sha1` | SHA-256/512 not supported by most apps |
165+
| `digits` | `6` | 8-digit tokens not supported by most apps |
166+
| `period` | `30` | 60-second period not supported by most apps |
167+
| `secret` | Base32 string | Always Base32-encoded in QR codes/URIs |
168+
169+
If you need to provision an authenticator app via QR code, use [`@otplib/uri`](https://www.npmjs.com/package/@otplib/uri) to generate an `otpauth://totp/` URI — this is the standard format recognized by all major apps.
170+
120171
## Documentation
121172

122173
Full documentation available at [otplib.yeojz.dev](https://otplib.yeojz.dev):

packages/totp/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"2fa",
2121
"mfa",
2222
"time-based",
23-
"one-time-password"
23+
"one-time-password",
24+
"authenticator",
25+
"google-authenticator"
2426
],
2527
"sideEffects": false,
2628
"type": "module",

0 commit comments

Comments
 (0)