mybase 1.1.41 → 1.1.42

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.1.41",
3
+ "version": "1.1.42",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
package/ts/index.d.ts CHANGED
@@ -29,3 +29,4 @@ export * from "./funcs/knexConnection";
29
29
  export * from "./models/Unixtime";
30
30
  export * from "./models/Timespan";
31
31
  export * from "./models/IPAddress";
32
+ export * from "./models/OTPGenerator";
package/ts/index.js CHANGED
@@ -46,4 +46,5 @@ __exportStar(require("./funcs/knexConnection"), exports);
46
46
  __exportStar(require("./models/Unixtime"), exports);
47
47
  __exportStar(require("./models/Timespan"), exports);
48
48
  __exportStar(require("./models/IPAddress"), exports);
49
+ __exportStar(require("./models/OTPGenerator"), exports);
49
50
  //# sourceMappingURL=index.js.map
package/ts/index.ts CHANGED
@@ -32,5 +32,6 @@ export * from "./funcs/knexConnection"
32
32
  export * from "./models/Unixtime"
33
33
  export * from "./models/Timespan"
34
34
  export * from "./models/IPAddress"
35
+ export * from "./models/OTPGenerator"
35
36
 
36
37
 
@@ -0,0 +1,8 @@
1
+ export declare class OTPGenerator {
2
+ private passkey;
3
+ private length;
4
+ constructor(passkey: string, length?: number);
5
+ generateOTP(): string;
6
+ validateOTP(otp: string): boolean;
7
+ private generateOTPWithNonce;
8
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.OTPGenerator = void 0;
27
+ const crypto = __importStar(require("crypto"));
28
+ class OTPGenerator {
29
+ constructor(passkey, length = 5) {
30
+ this.passkey = passkey;
31
+ this.length = length;
32
+ }
33
+ // Generate OTP with the current timestamp as nonce
34
+ generateOTP() {
35
+ return this.generateOTPWithNonce(Date.now());
36
+ }
37
+ // Validate if the OTP matches by extracting the nonce and regenerating OTP
38
+ validateOTP(otp) {
39
+ if (otp.length !== this.length + 13)
40
+ return false;
41
+ const nonceHex = otp.slice(-13);
42
+ const nonce = parseInt(nonceHex, 16);
43
+ return otp === this.generateOTPWithNonce(nonce);
44
+ }
45
+ // Helper to generate OTP based on a specific nonce
46
+ generateOTPWithNonce(nonce) {
47
+ const hash = crypto.createHmac('sha256', this.passkey).update(nonce.toString()).digest('hex');
48
+ const otp = [...hash.slice(0, this.length)].map(char => String.fromCharCode((parseInt(char, 16) % 26) + 97)).join('');
49
+ return `${otp}${nonce.toString(16).padStart(13, '0')}`;
50
+ }
51
+ }
52
+ exports.OTPGenerator = OTPGenerator;
53
+ //# sourceMappingURL=OTPGenerator.js.map
@@ -0,0 +1,30 @@
1
+ import { OTPGenerator } from './OTPGenerator';
2
+ import { wait } from './../funcs/wait';
3
+
4
+ describe('OTPGenerator', () => {
5
+ it('should generate OTP', () => {
6
+ const otpGenerator = new OTPGenerator('h0424m43io4', 5);
7
+ const otp = otpGenerator.generateOTP();
8
+ // length should be 5 + 13
9
+ expect(otp.length).toBe(18);
10
+ console.log(otp);
11
+ })
12
+
13
+ it('should generate different OTPs in milliseconds', async () => {
14
+ const otpGenerator = new OTPGenerator('h0424m43io4', 5);
15
+ const otp1 = otpGenerator.generateOTP()
16
+ await wait(1/1000);
17
+ const otp2 = otpGenerator.generateOTP();
18
+ expect(otp1).not.toBe(otp2);
19
+ })
20
+
21
+ it('should generate different OTPs in milliseconds per password', async () => {
22
+ const otpGenerator1 = new OTPGenerator('h0424m43io4', 5);
23
+ const otpGenerator2 = new OTPGenerator('h0424m43io5', 5);
24
+ for(let i=0; i<10000; i++) {
25
+ const otp1 = otpGenerator1.generateOTP()
26
+ const otp2 = otpGenerator2.generateOTP();
27
+ expect(otp1).not.toBe(otp2);
28
+ }
29
+ })
30
+ });
@@ -0,0 +1,25 @@
1
+ import * as crypto from 'crypto';
2
+
3
+ export class OTPGenerator {
4
+ constructor(private passkey: string, private length: number = 5) {}
5
+
6
+ // Generate OTP with the current timestamp as nonce
7
+ public generateOTP(): string {
8
+ return this.generateOTPWithNonce(Date.now());
9
+ }
10
+
11
+ // Validate if the OTP matches by extracting the nonce and regenerating OTP
12
+ public validateOTP(otp: string): boolean {
13
+ if (otp.length !== this.length + 13) return false;
14
+ const nonceHex = otp.slice(-13);
15
+ const nonce = parseInt(nonceHex, 16);
16
+ return otp === this.generateOTPWithNonce(nonce);
17
+ }
18
+
19
+ // Helper to generate OTP based on a specific nonce
20
+ private generateOTPWithNonce(nonce: number): string {
21
+ const hash = crypto.createHmac('sha256', this.passkey).update(nonce.toString()).digest('hex');
22
+ const otp = [...hash.slice(0, this.length)].map(char => String.fromCharCode((parseInt(char, 16) % 26) + 97)).join('');
23
+ return `${otp}${nonce.toString(16).padStart(13, '0')}`;
24
+ }
25
+ }