Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove three-two / Buffer dependency #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
import base32 from 'thirty-two';
// https://github.com/dpikalov/hotp-totp

const hexToBuf = (hex) => {
const bytes = []
for (let c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16))
return new Uint8Array(bytes)
}

const b32ToHex = (base32) => {
const base32chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
let bits = ''
let hex = ''

for (let i = 0; i < base32.length; i++) {
let val = base32chars.indexOf(base32.charAt(i).toUpperCase())
bits += val.toString(2).padStart(5, '0')
}

for (let i = 0; i + 4 <= bits.length; i += 4) {
let chunk = bits.substring(i, i + 4)
hex += parseInt(chunk, 2).toString(16)
}
return hex
}

/* Time-based OTP */
const totp = async (secret) => {
return hotp( secret, Math.floor( +new Date() / 30000 ) );
export const totp = async (secret) => {
return hotp(secret, Math.floor(+new Date() / 30000))
}

/* HMAC-based OTP */
const hotp = async (secret, counter) => {

export const hotp = async (secret, counter) => {
// Uint8Array(20)
const hmac = async (secret, counter) => {
const keyData = Uint8Array.from(base32.decode(secret));
// const keyData = Uint8Array.from(secret.split('').map((e) => parseInt(e, 32)))
const keyData = hexToBuf(b32ToHex(secret))
const key = await crypto.subtle.importKey(
'raw',
keyData,
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"name": "hotp-totp",
"version": "1.0.6",
"version": "1.1.6",
"description": "Generate One-Time passwords in JS using WebCrypto",
"type": "module",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"engines": {
"node": ">=14"
},
"keywords": [
"otp",
"totp",
Expand All @@ -19,8 +23,5 @@
"url": "git://github.com/dpikalov/hotp-totp.git"
},
"author": "dpikalov",
"license": "MIT",
"dependencies": {
"thirty-two": "^1.0.2"
}
"license": "MIT"
}