vanishcode / Blog

vanishcodeのblog
https://vanishcode.com
6 stars 0 forks source link

将宁盾令牌 (Dkey Token) 转换为标准 TOTP 从而能够使用 Google Authenticator #121

Open vanishcode opened 8 months ago

vanishcode commented 8 months ago

参考这位博主的思路:

https://blog.steven53.top/p/%E5%AE%81%E7%9B%BE%E4%BB%A4%E7%89%8Cdkey-token%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%A0%87%E5%87%86totp/

实现代码:

import base32Encode from 'base32-encode';
import { decode as base64Decode } from 'js-base64';
import qrcodeTerminal from 'qrcode-terminal';

const url = process.argv[2];

const { hash } = new URL(url);
const data = hash.slice(1);

const { token } = JSON.parse(base64Decode(data));
const { seed, passwordLength, timeStep, serial } = token;

const secret = base32Encode(
  new Uint8Array(Buffer.from(seed, 'hex')),
  'RFC4648',
);

const googleOtpauthURI = `otpauth://totp/${serial}?secret=${secret}&algorithm=SHA1&digits=${passwordLength}&period=${timeStep}`;

qrcodeTerminal.generate(googleOtpauthURI, { small: true });

使用:

  1. npm i base32-encode js-base64 qrcode-terminal
  2. node index.js https://mtc.ndkey.com/mtc/appDownload/index.html#xxxxxxxxxxx

同样功能的 App 为什么要装两个 😠