@aitianyu.cn/types 0.1.0 → 0.1.1

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/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  /**@format */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.SHA = exports.RSA = exports.QRCode = exports.hash = exports.guid = exports.Base32 = exports.Json = exports.Integer = exports.DataView = exports.Bytes = exports.StringHelper = exports.ArrayHelper = exports.ObjectHelper = exports.ObjectCalculater = exports.getBoolean = exports.Performance = exports.Log = exports.parseAreaString = exports.parseAreaCode = exports.ObjectDiffMergeFailedException = exports.ObjectDiffApplyInvalidStatusException = exports.ObjectMergeStatusCheckFailedException = exports.ObjectCloneFunctionNotSupportException = exports.ArgumentNullOrEmptyException = exports.Path = exports.PathDirAndFileConvertInvaild = exports.PathDirectoryValidationFailException = exports.PathProcessorSourceLostException = exports.TMap = exports.EncryptOption = exports.PathBase = exports.LogLevel = exports.Exception = exports.AreaCode = void 0;
4
+ exports.TOTP = exports.SHA = exports.RSA = exports.QRCode = exports.hash = exports.guid = exports.Base32 = exports.Json = exports.Integer = exports.DataView = exports.Bytes = exports.StringHelper = exports.ArrayHelper = exports.ObjectHelper = exports.ObjectCalculater = exports.getBoolean = exports.Performance = exports.Log = exports.parseAreaString = exports.parseAreaCode = exports.ObjectDiffMergeFailedException = exports.ObjectDiffApplyInvalidStatusException = exports.ObjectMergeStatusCheckFailedException = exports.ObjectCloneFunctionNotSupportException = exports.ArgumentNullOrEmptyException = exports.Path = exports.PathDirAndFileConvertInvaild = exports.PathDirectoryValidationFailException = exports.PathProcessorSourceLostException = exports.TMap = exports.EncryptOption = exports.PathBase = exports.LogLevel = exports.Exception = exports.AreaCode = void 0;
5
5
  // src/types
6
6
  var AreaCode_1 = require("./types/AreaCode");
7
7
  Object.defineProperty(exports, "AreaCode", { enumerable: true, get: function () { return AreaCode_1.AreaCode; } });
@@ -68,3 +68,5 @@ var RSA_1 = require("./security/RSA");
68
68
  Object.defineProperty(exports, "RSA", { enumerable: true, get: function () { return RSA_1.RSA; } });
69
69
  var SHA_1 = require("./security/SHA");
70
70
  Object.defineProperty(exports, "SHA", { enumerable: true, get: function () { return SHA_1.SHA; } });
71
+ var TOTP_1 = require("./security/TOTP");
72
+ Object.defineProperty(exports, "TOTP", { enumerable: true, get: function () { return TOTP_1.TOTP; } });
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ /** @format */
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.TOTP = void 0;
8
+ const crypto_1 = __importDefault(require("crypto"));
9
+ const Integer_1 = require("../core/object/Integer");
10
+ const DataView_1 = require("../core/object/DataView");
11
+ const Base32_1 = require("./Base32");
12
+ /** TOTP Lib */
13
+ class TOTP {
14
+ /**
15
+ * To generate a new TOTP secret key
16
+ *
17
+ * @returns TOTP secret key
18
+ */
19
+ static generate() {
20
+ return Base32_1.Base32.random(20);
21
+ }
22
+ /**
23
+ * Generate TOTP random code
24
+ *
25
+ * @param key TOTP secret key
26
+ * @returns return TOTP random code
27
+ */
28
+ static code(key, time) {
29
+ const K = Base32_1.Base32.decode(key.toUpperCase(), "RFC4648");
30
+ const T = Math.floor((time || Date.now()) / 1000 / 30);
31
+ const hmac = crypto_1.default.createHmac("sha1", DataView_1.DataView.parse(K));
32
+ const T1 = Buffer.from(Integer_1.Integer.toBytes(T));
33
+ const HS = hmac.update(T1).digest();
34
+ const offset = Integer_1.Integer.and(HS[19], 0xf);
35
+ const bytes = Integer_1.Integer.or(Integer_1.Integer.left(Integer_1.Integer.and(HS[offset], 0x7f) /** 这里是为了忽略符号位 */, 24), Integer_1.Integer.left(Integer_1.Integer.and(HS[offset + 1], 0xff), 16), Integer_1.Integer.left(Integer_1.Integer.and(HS[offset + 2], 0xff), 8), Integer_1.Integer.and(HS[offset + 3], 0xff));
36
+ let code = bytes.toString().slice(-6);
37
+ /* istanbul ignore next */
38
+ for (let i = 0; i > 6 - code.length; i++) {
39
+ code = `0${code}`;
40
+ }
41
+ return code;
42
+ }
43
+ /**
44
+ * Generate a key and TOTP register URL from given user
45
+ *
46
+ * @param user user name
47
+ * @returns return the secret key and url
48
+ */
49
+ static getUrl(user, project = "tianyu-template-project") {
50
+ const key = TOTP.generate();
51
+ const url = `otpauth://totp/${user}?secret=${key}&issuer=${project}`;
52
+ return { key, url };
53
+ }
54
+ }
55
+ exports.TOTP = TOTP;
@@ -27,3 +27,4 @@ export { hash } from "./security/Hash";
27
27
  export { QRCode } from "./security/QRCode";
28
28
  export { RSA } from "./security/RSA";
29
29
  export { SHA } from "./security/SHA";
30
+ export { TOTP } from "./security/TOTP";
@@ -0,0 +1,27 @@
1
+ /** @format */
2
+ /** TOTP Lib */
3
+ export declare class TOTP {
4
+ /**
5
+ * To generate a new TOTP secret key
6
+ *
7
+ * @returns TOTP secret key
8
+ */
9
+ static generate(): string;
10
+ /**
11
+ * Generate TOTP random code
12
+ *
13
+ * @param key TOTP secret key
14
+ * @returns return TOTP random code
15
+ */
16
+ static code(key: string, time?: number): string;
17
+ /**
18
+ * Generate a key and TOTP register URL from given user
19
+ *
20
+ * @param user user name
21
+ * @returns return the secret key and url
22
+ */
23
+ static getUrl(user: string, project?: string): {
24
+ key: string;
25
+ url: string;
26
+ };
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aitianyu.cn/types",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "private": false,
5
5
  "description": "Common modules (types, functions, classes) for aitianyu",
6
6
  "main": "./dist/lib/index.js",