feat: ported totp.js library to zepp quick-js

This commit is contained in:
Савелий Савенок
2024-11-09 16:37:03 +03:00
commit b67cddf4eb
31 changed files with 645 additions and 0 deletions

3
lib/totp.js/.travis.yml Normal file
View File

@@ -0,0 +1,3 @@
language: node_js
node_js:
- "8"

5
lib/totp.js/README.md Normal file
View File

@@ -0,0 +1,5 @@
# totp.js
Two-factor authentication implementation in pure javascript. One-time password generator (HOTP/TOTP) with support for Google Authenticator.
[![Build Status](https://travis-ci.org/wuyanxin/totp.js.svg?branch=master)](https://travis-ci.org/wuyanxin/totp.js)

9
lib/totp.js/index.js Normal file
View File

@@ -0,0 +1,9 @@
/*
* @Author: wuyanxin
* @Date: 2018-03-21 23:12:14
* @Last Modified by: wuyanxin
* @Last Modified time: 2018-03-21 23:12:14
*/
export {HOTP} from './lib/hotp.js'
export {TOTP} from './lib/totp.js'

77
lib/totp.js/lib/hotp.js Normal file
View File

@@ -0,0 +1,77 @@
/*
* @Author: wuyanxin
* @Date: 2018-03-21 22:25:37
* @Last Modified by: wuyanxin
* @Last Modified time: 2018-03-21 22:29:43
*/
import {encode, decode} from '../../thirty-two/index.js'
import jsSHA from 'jssha'
export class HOTP {
/**
*
* @param {string} key secret key
* @param {*} digit lenth of otp code
*/
constructor(key, digit = 6) {
this.key = key;
this.digit = digit;
}
/**
* generate secret key
* @param {int} len
*/
static randomKey(len = 16) {
const str = Math.random().toString(36);
return encode(str).toString().substr(0, len);
}
/**
* generate a OTP base on HMAC-SHA-1
* @param {int} movingFactor counter
*/
genOTP(movingFactor) {
const hmacSha = new jsSHA('SHA-1', 'BYTES');
hmacSha.setHMACKey(decode(this.key).toString(), 'BYTES');
const factorByte = this._factor2ByteText(movingFactor);
hmacSha.update(factorByte);
const hmac_result = hmacSha.getHMAC('BYTES');
return this._truncat(hmac_result);
}
/**
* verify a OPT code
* @param {string} opt opt code
* @param {int} movingFactor counter
*/
verify(opt, movingFactor) {
return opt === this.genOTP(movingFactor);
}
_truncat(hmac_result) {
const offset = hmac_result[19].charCodeAt() & 0xf;
const bin_code = (hmac_result[offset].charCodeAt() & 0x7f) << 24
| (hmac_result[offset+1].charCodeAt() & 0xff) << 16
| (hmac_result[offset+2].charCodeAt() & 0xff) << 8
| (hmac_result[offset+3].charCodeAt() & 0xff);
let otp = (bin_code % 10 ** this.digit).toString();
while (otp.length < this.digit) {
otp = '0' + otp;
}
return otp;
}
_factor2ByteText(movingFactor) {
const text = new Array(8);
for (let i = text.length - 1; i >= 0; i--) {
text[i] = String.fromCharCode(movingFactor & 0xFF);
movingFactor >>= 8;
}
return text.join('');
}
}

24
lib/totp.js/lib/totp.js Normal file
View File

@@ -0,0 +1,24 @@
/*
* @Author: wuyanxin
* @Date: 2018-03-21 22:25:42
* @Last Modified by: wuyanxin
* @Last Modified time: 2018-03-21 22:42:28
*/
import {HOTP} from "./hotp.js";
export class TOTP extends HOTP {
constructor(key){
super(key)
}
genOTP(timeStep = 30, t0 = 0) {
const T = Math.floor((Date.now() / 1000 - t0) / timeStep);
return super.genOTP(T);
}
verify(otp, timeStep = 30, t0 = 0) {
return otp === this.genOTP(timeStep, t0);
}
}

33
lib/totp.js/package.json Normal file
View File

@@ -0,0 +1,33 @@
{
"name": "totp.js",
"version": "0.0.1",
"description": "Two-factor authentication implementation in pure javascript. One-time password generator (HOTP/TOTP) with support for Google Authenticator. ",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/wuyanxin/totp.js.git"
},
"keywords": [
"otp",
"hotp",
"totp",
"Two-factor authentication",
"Google authenticator"
],
"author": "wuyanxin",
"license": "MIT",
"bugs": {
"url": "https://github.com/wuyanxin/totp.js/issues"
},
"homepage": "https://github.com/wuyanxin/totp.js#readme",
"dependencies": {
"jssha": "^2.3.1",
"thirty-two": "^1.0.2"
},
"devDependencies": {
"mocha": "^5.0.4"
}
}