@alwatr/crypto 9.2.1 → 9.4.0

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/main.js CHANGED
@@ -1,5 +1,5 @@
1
- /* 📦 @alwatr/crypto v9.2.1 */
1
+ /* 📦 @alwatr/crypto v9.4.0 */
2
2
  import{createHash as K,randomBytes as M}from"node:crypto";class A{config;constructor(x){this.config=x}generateRandom(){return this.generate(M(16))}generateRandomSelfValidate(){return this.generateSelfValidate(M(16))}generate(x){return this.config.prefix+K(this.config.algorithm).update(x).digest(this.config.encoding)}generateCrc(x){let j=K("sha1").update(x).digest(this.config.encoding);return this.config.crcLength==null||this.config.crcLength<1?j:j.slice(0,this.config.crcLength)}generateSelfValidate(x){let j=this.generate(x),z=this.generateCrc(j);return j+z}verify(x,j){return j===this.generate(x)}verifySelfValidate(x){let j=x.length-this.config.crcLength,z=x.slice(0,j);return x.slice(j)===this.generateCrc(z)}}import{createHmac as U}from"node:crypto";import{parseDuration as W}from"@alwatr/parse-duration";class F{config;_duration;get _epoch(){return this._duration==0?0:Math.floor(Date.now()/this._duration)}constructor(x){this.config=x;this._duration=x.duration=="infinite"?0:W(x.duration)}generate(x){return this._generate(x,this._epoch)}verify(x,j){let z=this._epoch;if(j===this._generate(x,z))return"valid";if(this._duration==0)return"invalid";if(j===this._generate(x,z-1))return"expired";return"invalid"}_generate(x,j){return this.config.prefix+U(this.config.algorithm,x).update(x+j).digest(this.config.encoding)}}var J={prefix:"u",algorithm:"sha1",encoding:"base64url",crcLength:4},N={...J,prefix:"d"},O={prefix:"s",algorithm:"sha384",encoding:"base64url",crcLength:4},Q={prefix:"t",algorithm:"sha224",encoding:"base64url"};class X{_generators;constructor(x){this._generators={secret:new A(O),deviceId:new A(N),userId:new A(J),token:new F({...Q,...x})}}generateUserId(){return this._generators.userId.generateRandomSelfValidate()}verifyUserId(x){return this._generators.userId.verifySelfValidate(x)}generateToken(x){return this._generators.token.generate(x.join())}verifyToken(x,j){return this._generators.token.verify(x.join(),j)}generateSecret(){return this._generators.secret.generateRandomSelfValidate()}verifySecret(x){return this._generators.secret.verifySelfValidate(x)}generateDeviceId(){return this._generators.deviceId.generateRandomSelfValidate()}verifyDeviceId(x){return this._generators.deviceId.verifySelfValidate(x)}}export{Q as userTokenGeneratorRecommendedConfig,J as userIdGeneratorRecommendedConfig,O as secretGeneratorRecommendedConfig,N as deviceIdGeneratorRecommendedConfig,F as AlwatrTokenGenerator,A as AlwatrHashGenerator,X as AlwatrCryptoFactory};
3
3
 
4
- //# debugId=21C150EBE602C08264756E2164756E21
4
+ //# debugId=3BEBCFA2CF1EDC5764756E2164756E21
5
5
  //# sourceMappingURL=main.js.map
package/dist/main.js.map CHANGED
@@ -8,6 +8,6 @@
8
8
  "import {AlwatrHashGenerator} from './hash.js';\nimport {\n deviceIdGeneratorRecommendedConfig,\n secretGeneratorRecommendedConfig,\n userIdGeneratorRecommendedConfig,\n userTokenGeneratorRecommendedConfig,\n} from './pre-config.js';\nimport {AlwatrTokenGenerator, type TokenValidity} from './token.js';\n\nimport type {Duration} from '@alwatr/parse-duration';\n\n/**\n * Configuration options for the CryptoFactory.\n */\nexport interface CryptoFactoryConfig {\n /**\n * The secret used for encryption and decryption tokens.\n */\n secret: string;\n\n /**\n * The duration for which the token is valid.\n */\n duration: Duration | 'infinite';\n}\n\n/**\n * Crypto factory for generating self-validate user-id, user-token, secret, device-id.\n */\nexport class AlwatrCryptoFactory {\n protected _generators;\n\n /**\n * Creates a new instance of crypto factory.\n * @param config The configuration used to create the crypto factory.\n */\n constructor(config: CryptoFactoryConfig) {\n this._generators = {\n secret: new AlwatrHashGenerator(secretGeneratorRecommendedConfig),\n deviceId: new AlwatrHashGenerator(deviceIdGeneratorRecommendedConfig),\n userId: new AlwatrHashGenerator(userIdGeneratorRecommendedConfig),\n token: new AlwatrTokenGenerator({\n ...userTokenGeneratorRecommendedConfig,\n ...config,\n }),\n } as const;\n }\n\n /**\n * Generate self-verifiable user ID.\n * @returns The generated user ID.\n * @example\n * ```typescript\n * const newUser = {\n * id: cryptoFactory.generateUserId(),\n * ...\n * }\n * ```\n */\n public generateUserId(): string {\n return this._generators.userId.generateRandomSelfValidate();\n }\n\n /**\n * Verify a user ID without token.\n * @param userId The user ID to verify.\n * @returns A boolean indicating whether the user ID is valid.\n * @example\n * ```typescript\n * if (!cryptoFactory.verifyUserId(user.id)) {\n * throw new Error('invalid_user');\n * }\n * ```\n */\n public verifyUserId(userId: string): boolean {\n return this._generators.userId.verifySelfValidate(userId);\n }\n\n /**\n * Generate authentication token.\n * @param uniquelyList The list of uniq values to generate the token from.\n * @returns The generated user token.\n * @example\n * ```typescript\n * const userToken = cryptoFactory.generateToken([user.id, user.lpe]);\n * ```\n */\n public generateToken(uniquelyList: (string | number)[]): string {\n return this._generators.token.generate(uniquelyList.join());\n }\n\n /**\n * Verify a authentication token.\n * @param uniquelyList The list of uniq values used to generate the token.\n * @param token The user token to verify.\n * @returns The validity of the token.\n * @example\n * ```typescript\n * if (!cryptoFactory.verifyToken([user.id, user.lpe], userToken)) {\n * throw new Error('invalid_token');\n * }\n * ```\n */\n public verifyToken(uniquelyList: (string | number)[], token: string): TokenValidity {\n return this._generators.token.verify(uniquelyList.join(), token);\n }\n\n /**\n * Generate self-verifiable secret.\n * @returns The generated secret.\n * @example\n * ```typescript\n * const config = {\n * storageToken: cryptoFactory.generateSecret(),\n * ...\n * }\n * ```\n */\n public generateSecret(): string {\n return this._generators.secret.generateRandomSelfValidate();\n }\n\n /**\n * Verify a secret.\n * @param secret The secret to verify.\n * @returns A boolean indicating whether the secret is valid.\n * @example\n * ```typescript\n * if (!cryptoFactory.verifySecret(config.storageToken)) {\n * throw new Error('invalid_secret');\n * }\n * ```\n */\n public verifySecret(secret: string): boolean {\n return this._generators.secret.verifySelfValidate(secret);\n }\n\n /**\n * Generate self-verifiable device ID.\n * @returns The generated device ID.\n * @example\n * ```typescript\n * const deviceId = deviceFactory.generateDeviceId();\n * ```\n */\n public generateDeviceId(): string {\n return this._generators.deviceId.generateRandomSelfValidate();\n }\n\n /**\n * Verify a device ID.\n * @param deviceId The device ID to verify.\n * @returns A boolean indicating whether the device ID is valid.\n * @example\n * ```typescript\n * if (!deviceFactory.verifyDeviceId(bodyJson.deviceId)) {\n * throw {\n * ok: false,\n * status: 400,\n * error: 'invalid_device_id',\n * }\n * }\n * ```\n */\n public verifyDeviceId(deviceId: string): boolean {\n return this._generators.deviceId.verifySelfValidate(deviceId);\n }\n}\n"
9
9
  ],
10
10
  "mappings": ";AAAA,qBAAQ,iBAAY,oBAgCb,MAAM,CAAoB,CAKZ,OAAnB,WAAW,CAAQ,EAA6B,CAA7B,cAUZ,cAAc,EAAW,CAC9B,OAAO,KAAK,SAAS,EAAY,EAAE,CAAC,EAW/B,0BAA0B,EAAW,CAC1C,OAAO,KAAK,qBAAqB,EAAY,EAAE,CAAC,EAY3C,QAAQ,CAAC,EAA0B,CACxC,OAAO,KAAK,OAAO,OAAS,EAAW,KAAK,OAAO,SAAS,EAAE,OAAO,CAAI,EAAE,OAAO,KAAK,OAAO,QAAQ,EAQjG,WAAW,CAAC,EAA0B,CAC3C,IAAM,EAAM,EAAW,MAAM,EAAE,OAAO,CAAI,EAAE,OAAO,KAAK,OAAO,QAAQ,EACvE,OAAO,KAAK,OAAO,WAAa,MAAQ,KAAK,OAAO,UAAY,EAAI,EAAM,EAAI,MAAM,EAAG,KAAK,OAAO,SAAS,EAYvG,oBAAoB,CAAC,EAA0B,CACpD,IAAM,EAAW,KAAK,SAAS,CAAI,EAC7B,EAAU,KAAK,YAAY,CAAQ,EACzC,OAAO,EAAW,EAeb,MAAM,CAAC,EAAkB,EAAuB,CACrD,OAAO,IAAS,KAAK,SAAS,CAAI,EAc7B,kBAAkB,CAAC,EAAuB,CAC/C,IAAM,EAAS,EAAK,OAAS,KAAK,OAAO,UACnC,EAAW,EAAK,MAAM,EAAG,CAAM,EAErC,OADgB,EAAK,MAAM,CAAM,IACd,KAAK,Y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Y,CAAC,EAAyB,CAC3C,OAAO,KAAK,YAAY,OAAO,mBAAmB,CAAM,EAYnD,aAAa,CAAC,EAA2C,CAC9D,OAAO,KAAK,YAAY,MAAM,SAAS,EAAa,KAAK,CAAC,EAerD,WAAW,CAAC,EAAmC,EAA8B,CAClF,OAAO,KAAK,YAAY,MAAM,OAAO,EAAa,KAAK,EAAG,CAAK,EAc1D,cAAc,EAAW,CAC9B,OAAO,KAAK,YAAY,OAAO,2BAA2B,EAcrD,YAAY,CAAC,EAAyB,CAC3C,OAAO,KAAK,YAAY,OAAO,mBAAmB,CAAM,EAWnD,gBAAgB,EAAW,CAChC,OAAO,KAAK,YAAY,SAAS,2BAA2B,EAkBvD,cAAc,CAAC,EAA2B,CAC/C,OAAO,KAAK,YAAY,SAAS,mBAAmB,CAAQ,EAEhE",
11
- "debugId": "21C150EBE602C08264756E2164756E21",
11
+ "debugId": "3BEBCFA2CF1EDC5764756E2164756E21",
12
12
  "names": []
13
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alwatr/crypto",
3
- "version": "9.2.1",
3
+ "version": "9.4.0",
4
4
  "description": "A robust generator of secure authentication HOTP tokens, employing the HMAC-based One-Time Password algorithm, accompanied by a suite of cryptographic utilities, all encapsulated within a compact TypeScript module.",
5
5
  "license": "MPL-2.0",
6
6
  "author": "S. Ali Mihandoost <ali.mihandoost@gmail.com> (https://ali.mihandoost.com)",
@@ -21,11 +21,11 @@
21
21
  },
22
22
  "sideEffects": false,
23
23
  "dependencies": {
24
- "@alwatr/parse-duration": "9.1.1"
24
+ "@alwatr/parse-duration": "9.4.0"
25
25
  },
26
26
  "devDependencies": {
27
- "@alwatr/nano-build": "9.1.1",
28
- "@alwatr/tsconfig-base": "9.1.1",
27
+ "@alwatr/nano-build": "9.3.0",
28
+ "@alwatr/standard": "9.4.0",
29
29
  "@alwatr/type-helper": "9.1.1",
30
30
  "@types/node": "^24.12.2",
31
31
  "typescript": "^6.0.2"
@@ -72,5 +72,5 @@
72
72
  "token",
73
73
  "typescript"
74
74
  ],
75
- "gitHead": "b9dc0cb8b04b16216d44fbe9a9682f2a57926167"
75
+ "gitHead": "662f1047fc2a1b53a00902028d6cebfd04b52dd5"
76
76
  }