fix, chore, feat: refactor of settings app, fix of Google Auth migration parse, added rename button(testing), added sort feature, added timeout to screen turning off, formated codes with space between 6-digits codes

Co-authored-by: DemiarUA <demiar97@gmail.com>
https://github.com/Lisoveliy/totpfit/pull/1
This commit is contained in:
2025-08-07 16:53:54 +03:00
parent ea75f03fbe
commit 56d5edc37b
20 changed files with 825 additions and 601 deletions

View File

@@ -1,53 +1,59 @@
import { decode } from "./base32decoder.js";
import jsSHA from "jssha";
"use bigint"
("use bigint");
/**
* get HOTP based on counter
* @param {BigInt} counter BigInt counter of HOTP
* @param {string} secret base32 encoded string
* @param {string} secret base32 encoded string
* @param {number} [digits=6] number of digits in OTP token
* @param {string} [hashType='SHA-1'] type of hash (more in jsSHA documentation)
* @returns HOTP string
*/
export function getHOTP(counter, secret, digits = 6, hashType = 'SHA-1'){
export function getHOTP(counter, secret, digits = 6, hashType = "SHA-1") {
//Stage 1: Prepare data
const rawDataCounter = new DataView(new ArrayBuffer(8))
rawDataCounter.setUint32(4, counter)
const bCounter = new Uint8Array(rawDataCounter.buffer)
const bSecret = new Uint8Array(decode(secret).match(/.{1,2}/g).map(chunk => parseInt(chunk, 16))); //confirmed
const rawDataCounter = new DataView(new ArrayBuffer(8));
rawDataCounter.setUint32(4, counter);
const bCounter = new Uint8Array(rawDataCounter.buffer);
const bSecret = new Uint8Array(
decode(secret)
.match(/.{1,2}/g)
.map((chunk) => parseInt(chunk, 16)),
); //confirmed
//Stage 2: Hash data
const jssha = new jsSHA(hashType, 'UINT8ARRAY')
jssha.setHMACKey(bSecret, 'UINT8ARRAY')
jssha.update(bCounter)
const hmacResult = jssha.getHMAC('UINT8ARRAY') //confirmed
const jssha = new jsSHA(hashType, "UINT8ARRAY");
jssha.setHMACKey(bSecret, "UINT8ARRAY");
jssha.update(bCounter);
const hmacResult = jssha.getHMAC("UINT8ARRAY"); //confirmed
//Stage 3: Dynamic truncate
const offsetB = hmacResult[19] & 0xf;
const P = hmacResult.slice(offsetB, offsetB + 4)
const P = hmacResult.slice(offsetB, offsetB + 4);
P[0] = P[0] & 0x7f;
//Stage 4: Format string
let res = (new DataView(P.buffer).getInt32(0) % Math.pow(10, digits)).toString()
while(res.length < digits)
res = '0' + res;
let res = (
new DataView(P.buffer).getInt32(0) % Math.pow(10, digits)
).toString();
while (res.length < digits) res = "0" + res;
return res;
}
/**
* get OTP based on current time
* @param {string} secret base32 encoded string
* @param {string} secret base32 encoded string
* @param {number} [digits=6] digits in OTP
* @param {number} [time=Date.now()] time for counter (default unix time epoch)
* @param {number} [fetchTime=30] period of token in seconds
* @param {number} [timeOffset=0] time offset for token in seconds
* @param {number} [timeOffset=0] time offset for token in seconds
* @param {string} [hashType='SHA-1'] type of hash (more in jsSHA documentation)
* @returns TOTP string
*/
export function getTOTP(secret, digits = 6, time = Date.now(), fetchTime = 30, timeOffset = 0, hashType = 'SHA-1')
{
const unixTime = Math.round((time / 1000 + timeOffset) / fetchTime)
return getHOTP(BigInt(unixTime), secret, digits)
}
export function getTOTP(
secret,
digits = 6,
time = Date.now(),
fetchTime = 30,
timeOffset = 0,
hashType = "SHA-1",
) {
const unixTime = Math.round((time / 1000 + timeOffset) / fetchTime);
return getHOTP(BigInt(unixTime), secret, digits);
}

View File

@@ -1,60 +1,62 @@
export function decode(base32) {
for (
var base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
bits = "",
hex = "",
i = 0;
i < base32.length;
i++
) {
var val = base32chars.indexOf(base32.charAt(i).toUpperCase());
bits += leftpad(val.toString(2), 5, "0");
}
for (i = 0; i + 4 <= bits.length; i += 4) {
var chunk = bits.substr(i, 4);
hex += parseInt(chunk, 2).toString(16);
}
return hex;
for (
var base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
bits = "",
hex = "",
i = 0;
i < base32.length;
i++
) {
var val = base32chars.indexOf(base32.charAt(i).toUpperCase());
bits += leftpad(val.toString(2), 5, "0");
}
for (i = 0; i + 4 <= bits.length; i += 4) {
var chunk = bits.substr(i, 4);
hex += parseInt(chunk, 2).toString(16);
}
return hex;
}
function leftpad(str, len, pad) {
return (
len + 1 >= str.length &&
(str = new Array(len + 1 - str.length).join(pad) + str),
str
);
return (
len + 1 >= str.length &&
(str = new Array(len + 1 - str.length).join(pad) + str),
str
);
}
export function encode(bytes) {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
let bits = 0;
let value = 0;
let output = '';
let output = "";
for (let i = 0; i < bytes.length; i++) {
value = (value << 8) | bytes[i];
bits += 8;
while (bits >= 5) {
output += alphabet[(value >>> (bits - 5)) & 0x1F];
output += alphabet[(value >>> (bits - 5)) & 0x1f];
bits -= 5;
}
}
if (bits > 0) {
output += alphabet[(value << (5 - bits)) & 0x1F];
output += alphabet[(value << (5 - bits)) & 0x1f];
}
const paddingLength = (8 - (output.length % 8)) % 8;
output += '='.repeat(paddingLength);
output += "=".repeat(paddingLength);
return output;
}
export function base64decode(base64) {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let result = [];
let i = 0, j = 0;
let i = 0,
j = 0;
let b1, b2, b3, b4;
while (i < base64.length) {
@@ -77,4 +79,4 @@ export function base64decode(base64) {
}
return result.slice(0, j);
}
}

View File

@@ -1,60 +1,63 @@
import { getHOTP } from "./OTPGenerator.js"
import { getHOTP } from "./OTPGenerator.js";
/**
* TOTP instance
*/
export class TOTP {
/**
*
* @param {string} secret base32 encoded string
*
* @param {string} secret base32 encoded string
* @param {string} issuer issuer of TOTP
* @param {string} client client of TOTP
* @param {number} [digits=6] number of digits in OTP token
* @param {number} [fetchTime=30] period of token in seconds
* @param {number} [timeOffset=0] time offset for token in seconds
* @param {number} [timeOffset=0] time offset for token in seconds
* @param {string} [hashType='SHA-1'] type of hash (more in jsSHA documentation)
*/
constructor(secret,
constructor(
secret,
issuer,
client,
digits = 6,
fetchTime = 30,
timeOffset = 0,
hashType = 'SHA-1') {
this.secret = secret
this.issuer = issuer
this.client = client
this.digits = digits
this.fetchTime = fetchTime
this.timeOffset = timeOffset
this.hashType = hashType
hashType = "SHA-1",
) {
this.secret = secret;
this.issuer = issuer;
this.client = client;
this.digits = digits;
this.fetchTime = fetchTime;
this.timeOffset = timeOffset;
this.hashType = hashType;
}
static copy(totp){
static copy(totp) {
return new TOTP(
secret = totp.secret,
issuer = totp.TOTPissuer,
client = totp.client,
digits = totp.digits,
fetchTime = totp.fetchTime,
timeOffset = totp.timeOffset,
hashType = totp.hashType
)
(secret = totp.secret),
(issuer = totp.TOTPissuer),
(client = totp.client),
(digits = totp.digits),
(fetchTime = totp.fetchTime),
(timeOffset = totp.timeOffset),
(hashType = totp.hashType),
);
}
/**
*
*
* @param {number} time time for counter (default unix time epoch)
* @returns OTP instance
*/
getOTP(time = Date.now()) {
const unixTime = (time / 1000 + this.timeOffset) / this.fetchTime
const otp = getHOTP(Math.floor(unixTime), this.secret, this.digits)
const expireTime = time +
const unixTime = (time / 1000 + this.timeOffset) / this.fetchTime;
const otp = getHOTP(Math.floor(unixTime), this.secret, this.digits);
const expireTime =
time +
(this.fetchTime -
(time / 1000 + this.timeOffset) %
this.fetchTime) * 1000
const createdTime = time - (((time / 1000 + this.timeOffset) %
this.fetchTime) * 1000)
return new OTP(otp, createdTime, expireTime)
((time / 1000 + this.timeOffset) % this.fetchTime)) *
1000;
const createdTime =
time - ((time / 1000 + this.timeOffset) % this.fetchTime) * 1000;
return new OTP(otp, createdTime, expireTime);
}
}
@@ -63,14 +66,14 @@ export class TOTP {
*/
export class OTP {
/**
*
*
* @param {string} otp OTP string
* @param {number} createdTime time in unix epoch created OTP
* @param {number} expireTime time in unix epoch to expire OTP
* @param {number} createdTime time in unix epoch created OTP
* @param {number} expireTime time in unix epoch to expire OTP
*/
constructor(otp, createdTime, expireTime) {
this.otp = otp
this.createdTime = createdTime
this.expireTime = expireTime
this.otp = otp;
this.createdTime = createdTime;
this.expireTime = expireTime;
}
}
}