2要素認証のコードはこんな感じ。secret はサービス側で QR コードを表示された時に、表示するオプションがあるのでそれを押すと base32 テキストが出てきます。
```
//

RFC 4226: HOTP: An HMAC-Based One-Time Password Algorithm | RFC Editor
This document describes an algorithm to generate one-time password values, based on Hashed Message Authentication Code (HMAC). A security analysis ...
const hotp = (secret: BinaryLike, count: number, digits: number = 6) => {
const c = Buffer.alloc(8);
c.writeBigInt64BE(BigInt(count));
const hs = createHmac('sha1', secret).update(c).digest();
const s = (hs.readUInt32BE(hs[19] & 0x0f) & 0x7fffffff);
return (s % (10 ** digits)).toString().padStart(digits, '0');
};
//

RFC 6238: TOTP: Time-Based One-Time Password Algorithm | RFC Editor
This document describes an extension of the One-Time Password (OTP) algorithm, namely the HMAC-based One-Time Password (HOTP) algorithm, as defined...
const totp = (secret: BinaryLike, time: number, step: number = 30, digits: number = 6, t0: number = 0) => {
const count = Math.floor((time - t0) / step);
return hotp(secret, count, digits);
};
//

GitHub
Key Uri Format
Open source version of Google Authenticator (except the Android app) - google/google-authenticator
const twoFA = (secret: string, length: number = 1) => {
const sec = base32.decode(secret);
const now = Math.floor(Date.now() / 1000);
return { totp: Array.from({ length }, (_, i) => totp(sec, now + 30 * i, 30, 6)), time: 30 - (now % 30) };
}
```