otpauth.test.js 664 Bytes
/**
 * Created by lintry on 2017/5/19.
 */

//用到了eval函数,不能在小程序内使用
const OTPAuth = require('otpauth');

let totp = new OTPAuth.TOTP({
  'issuer': 'ACME',
  'label': 'AzureDiamond',
  'algorithm': 'SHA1',
  'digits': 6,
  'period': 30,
  'secret': OTPAuth.Secret.fromB32('NB2W45DFOIZA')
});

// Generate TOTP token
let token = totp.generate();

// Validate TOTP token
let delta = totp.validate({
  'token': token,
  'window': 10
});

// Convert to Google Authenticator key URI
// otpauth://totp/ACME:AzureDiamond?issuer=ACME&secret=NB2W45DFOIZA&algorithm=SHA1&digits=6&period=30
let uri = totp.toString();
console.log(uri, delta, token)