foronce 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base32.cjs +2 -4
- package/dist/base32.mjs +2 -4
- package/dist/index.cjs +2 -3
- package/dist/index.mjs +4 -5
- package/dist/universal/universal.cjs +2 -1
- package/dist/universal/universal.mjs +3 -2
- package/package.json +9 -18
- package/src/index.js +2 -1
- package/src/lib/crypto.js +4 -9
- package/src/universal/hmac.ts +2 -1
package/dist/base32.cjs
CHANGED
|
@@ -106,10 +106,8 @@ const toBinary = (char, padLimit = 8) => {
|
|
|
106
106
|
};
|
|
107
107
|
const chunk = (arr, chunkSize = 1, cache = []) => {
|
|
108
108
|
const tmp = [...arr];
|
|
109
|
-
if (chunkSize <= 0)
|
|
110
|
-
|
|
111
|
-
while (tmp.length)
|
|
112
|
-
cache.push(tmp.splice(0, chunkSize));
|
|
109
|
+
if (chunkSize <= 0) return cache;
|
|
110
|
+
while (tmp.length) cache.push(tmp.splice(0, chunkSize));
|
|
113
111
|
return cache;
|
|
114
112
|
};
|
|
115
113
|
|
package/dist/base32.mjs
CHANGED
|
@@ -104,10 +104,8 @@ const toBinary = (char, padLimit = 8) => {
|
|
|
104
104
|
};
|
|
105
105
|
const chunk = (arr, chunkSize = 1, cache = []) => {
|
|
106
106
|
const tmp = [...arr];
|
|
107
|
-
if (chunkSize <= 0)
|
|
108
|
-
|
|
109
|
-
while (tmp.length)
|
|
110
|
-
cache.push(tmp.splice(0, chunkSize));
|
|
107
|
+
if (chunkSize <= 0) return cache;
|
|
108
|
+
while (tmp.length) cache.push(tmp.splice(0, chunkSize));
|
|
111
109
|
return cache;
|
|
112
110
|
};
|
|
113
111
|
|
package/dist/index.cjs
CHANGED
|
@@ -26,8 +26,7 @@ function isTOTPValid(secret, token, options = {}) {
|
|
|
26
26
|
for (let index = -2; index < 3; index += 1) {
|
|
27
27
|
const fromSys = totp(secret, Date.now() / 1e3 + index, _options);
|
|
28
28
|
const valid = fromSys === token;
|
|
29
|
-
if (valid)
|
|
30
|
-
return true;
|
|
29
|
+
if (valid) return true;
|
|
31
30
|
}
|
|
32
31
|
return false;
|
|
33
32
|
}
|
|
@@ -45,7 +44,7 @@ function bigEndian64(hash) {
|
|
|
45
44
|
return buf;
|
|
46
45
|
}
|
|
47
46
|
function generateTOTPSecret(num = 32) {
|
|
48
|
-
return base32.encode(node_crypto.randomBytes(num).toString("
|
|
47
|
+
return base32.encode(node_crypto.randomBytes(num).toString("hex"));
|
|
49
48
|
}
|
|
50
49
|
|
|
51
50
|
exports.generateTOTPSecret = generateTOTPSecret;
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomBytes, createHmac } from 'node:crypto';
|
|
2
2
|
import { Buffer } from 'buffer';
|
|
3
|
-
import {
|
|
3
|
+
import { encode, decode } from './base32.mjs';
|
|
4
4
|
|
|
5
5
|
/*!
|
|
6
6
|
* base-32.js
|
|
@@ -24,8 +24,7 @@ function isTOTPValid(secret, token, options = {}) {
|
|
|
24
24
|
for (let index = -2; index < 3; index += 1) {
|
|
25
25
|
const fromSys = totp(secret, Date.now() / 1e3 + index, _options);
|
|
26
26
|
const valid = fromSys === token;
|
|
27
|
-
if (valid)
|
|
28
|
-
return true;
|
|
27
|
+
if (valid) return true;
|
|
29
28
|
}
|
|
30
29
|
return false;
|
|
31
30
|
}
|
|
@@ -43,7 +42,7 @@ function bigEndian64(hash) {
|
|
|
43
42
|
return buf;
|
|
44
43
|
}
|
|
45
44
|
function generateTOTPSecret(num = 32) {
|
|
46
|
-
return encode(randomBytes(num).toString("
|
|
45
|
+
return encode(randomBytes(num).toString("hex"));
|
|
47
46
|
}
|
|
48
47
|
|
|
49
48
|
export { generateTOTPSecret, generateTOTPURL, isTOTPValid, totp };
|
|
@@ -15,10 +15,11 @@ const algoMap = {
|
|
|
15
15
|
sha512: "SHA-512"
|
|
16
16
|
};
|
|
17
17
|
async function createHmac(algorithm, secret, data) {
|
|
18
|
+
const secretBuffer = !Buffer.isBuffer(secret) ? Buffer.from(secret, "utf-8") : secret;
|
|
18
19
|
const key = await uncrypto.subtle.importKey(
|
|
19
20
|
"raw",
|
|
20
21
|
// raw format of the key - should be Uint8Array
|
|
21
|
-
|
|
22
|
+
secretBuffer,
|
|
22
23
|
{
|
|
23
24
|
// algorithm details
|
|
24
25
|
name: "HMAC",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { subtle, getRandomValues } from 'uncrypto';
|
|
2
|
-
import {
|
|
2
|
+
import { encode, decode } from '../base32.mjs';
|
|
3
3
|
|
|
4
4
|
function bigEndian64(hash) {
|
|
5
5
|
const buf = Buffer.allocUnsafe(64 / 8);
|
|
@@ -13,10 +13,11 @@ const algoMap = {
|
|
|
13
13
|
sha512: "SHA-512"
|
|
14
14
|
};
|
|
15
15
|
async function createHmac(algorithm, secret, data) {
|
|
16
|
+
const secretBuffer = !Buffer.isBuffer(secret) ? Buffer.from(secret, "utf-8") : secret;
|
|
16
17
|
const key = await subtle.importKey(
|
|
17
18
|
"raw",
|
|
18
19
|
// raw format of the key - should be Uint8Array
|
|
19
|
-
|
|
20
|
+
secretBuffer,
|
|
20
21
|
{
|
|
21
22
|
// algorithm details
|
|
22
23
|
name: "HMAC",
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "foronce",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "The OTP Library",
|
|
5
|
-
"repository":
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+ssh://git@github.com/dumbjs/foronce.git"
|
|
8
|
+
},
|
|
6
9
|
"license": "MIT",
|
|
7
10
|
"author": "Reaper <ahoy@barelyhuman.dev>",
|
|
8
11
|
"type": "module",
|
|
@@ -43,11 +46,10 @@
|
|
|
43
46
|
"scripts": {
|
|
44
47
|
"build": "unbuild",
|
|
45
48
|
"fix": "prettier --write .",
|
|
46
|
-
"next": "bumpp",
|
|
47
49
|
"prepare": "husky install",
|
|
48
50
|
"size": "sizesnap",
|
|
49
|
-
"test": "
|
|
50
|
-
"test:ci": "c8 uvu -
|
|
51
|
+
"test": "tsc -p ./tsconfig.spec.json && uvu dist-tests",
|
|
52
|
+
"test:ci": "tsc -p ./tsconfig.spec.json && c8 uvu dist-tests"
|
|
51
53
|
},
|
|
52
54
|
"keywords": [
|
|
53
55
|
"otp",
|
|
@@ -63,29 +65,18 @@
|
|
|
63
65
|
"@barelyhuman/prettier-config": "^1.0.0",
|
|
64
66
|
"@types/node": "^20.10.8",
|
|
65
67
|
"buffer": "^6.0.3",
|
|
66
|
-
"bumpp": "^9.2.0",
|
|
67
68
|
"c8": "^8.0.1",
|
|
68
69
|
"esm": "^3.2.25",
|
|
69
70
|
"husky": "^8.0.3",
|
|
70
|
-
"lint-staged": "^
|
|
71
|
+
"lint-staged": "^16.3.1",
|
|
71
72
|
"prettier": "^2.7.1",
|
|
72
73
|
"publint": "^0.2.7",
|
|
73
|
-
"
|
|
74
|
-
"tsm": "^2.3.0",
|
|
75
|
-
"unbuild": "^2.0.0",
|
|
74
|
+
"unbuild": "^3.6.1",
|
|
76
75
|
"uvu": "^0.5.6"
|
|
77
76
|
},
|
|
78
77
|
"publishConfig": {
|
|
79
78
|
"access": "public"
|
|
80
79
|
},
|
|
81
|
-
"sizesnap": {
|
|
82
|
-
"files": [
|
|
83
|
-
"dist/*.dts",
|
|
84
|
-
"dist/*.ts",
|
|
85
|
-
"dist/*.js",
|
|
86
|
-
"dist/*.cjs"
|
|
87
|
-
]
|
|
88
|
-
},
|
|
89
80
|
"dependencies": {
|
|
90
81
|
"uncrypto": "^0.1.3"
|
|
91
82
|
}
|
package/src/index.js
CHANGED
|
@@ -31,6 +31,7 @@ export function totp(secret, when = floor(Date.now() / 1000), options = {}) {
|
|
|
31
31
|
const now = floor(when / _options.period)
|
|
32
32
|
const key = decode(secret)
|
|
33
33
|
const buff = bigEndian64(BigInt(now))
|
|
34
|
+
// @ts-expect-error buffers are allowed
|
|
34
35
|
const hmac = createHmac(_options.algorithm, key).update(buff).digest()
|
|
35
36
|
const offset = hmac[hmac.length - 1] & 0xf
|
|
36
37
|
const truncatedHash = hmac.subarray(offset, offset + 4)
|
|
@@ -88,5 +89,5 @@ function bigEndian64(hash) {
|
|
|
88
89
|
}
|
|
89
90
|
|
|
90
91
|
export function generateTOTPSecret(num = 32) {
|
|
91
|
-
return encode(randomBytes(num).toString('
|
|
92
|
+
return encode(randomBytes(num).toString('hex'))
|
|
92
93
|
}
|
package/src/lib/crypto.js
CHANGED
|
@@ -7,17 +7,12 @@ const algoMap = {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export async function createHmac(algorithm, secret, data) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// enc = new TextEncoder('utf-8')
|
|
14
|
-
// } else {
|
|
15
|
-
// enc = new TextEncoder()
|
|
16
|
-
// }
|
|
17
|
-
|
|
10
|
+
let secretBuffer = !Buffer.isBuffer(secret)
|
|
11
|
+
? Buffer.from(secret, 'utf-8')
|
|
12
|
+
: secret
|
|
18
13
|
const key = await subtle.importKey(
|
|
19
14
|
'raw', // raw format of the key - should be Uint8Array
|
|
20
|
-
|
|
15
|
+
secretBuffer,
|
|
21
16
|
{
|
|
22
17
|
// algorithm details
|
|
23
18
|
name: 'HMAC',
|
package/src/universal/hmac.ts
CHANGED
|
@@ -13,9 +13,10 @@ export async function createHmac(
|
|
|
13
13
|
secret: string,
|
|
14
14
|
data: Buffer
|
|
15
15
|
) {
|
|
16
|
+
const secretBuffer = !Buffer.isBuffer(secret) ? Buffer.from(secret, 'utf-8') : secret;
|
|
16
17
|
const key = await subtle.importKey(
|
|
17
18
|
'raw', // raw format of the key - should be Uint8Array
|
|
18
|
-
|
|
19
|
+
secretBuffer,
|
|
19
20
|
{
|
|
20
21
|
// algorithm details
|
|
21
22
|
name: 'HMAC',
|