{
  "version": 3,
  "sources": ["../src/main.ts", "../src/hash.ts", "../src/token.ts", "../src/pre-config.ts", "../src/api.ts"],
  "sourcesContent": ["export * from './hash.js';\nexport * from './token.js';\nexport * from './api.js';\nexport * from './type.js';\nexport * from './pre-config.js';\n", "import {createHash, randomBytes, type BinaryLike, type BinaryToTextEncoding} from 'node:crypto';\n\nimport type {CryptoAlgorithm} from './type.js';\n\n/**\n * Represents the configuration for a hash generator.\n */\nexport interface HashGeneratorConfig {\n  /**\n   * The prefix to be added to the generated hash.\n   */\n  prefix: string;\n\n  /**\n   * The algorithm used for hashing.\n   */\n  algorithm: CryptoAlgorithm;\n\n  /**\n   * The encoding used for the generated hash.\n   */\n  encoding: BinaryToTextEncoding;\n\n  /**\n   * The length of the CRC (Cyclic Redundancy Check) value.\n   */\n  crcLength: number;\n}\n\n/**\n * Secure **self-validate** hash generator.\n */\nexport class AlwatrHashGenerator {\n  /**\n   * Creates a new instance of the AlwatrHashGenerator class.\n   * @param config The configuration for the hash generator.\n   */\n  constructor(public config: HashGeneratorConfig) {}\n\n  /**\n   * Generate a random hash.\n   * @returns The generated hash.\n   * @example\n   * ```typescript\n   * const clientId = hashGenerator.generateRandom();\n   * ```\n   */\n  public generateRandom(): string {\n    return this.generate(randomBytes(16));\n  }\n\n  /**\n   * Generate a **self-validate** random hash.\n   * @returns The generated self-validated hash.\n   * @example\n   * ```typescript\n   * const userId = hashGenerator.generateRandomSelfValidate();\n   * ```\n   */\n  public generateRandomSelfValidate(): string {\n    return this.generateSelfValidate(randomBytes(16));\n  }\n\n  /**\n   * Generate a hash from data.\n   * @param data - The data to generate the hash from.\n   * @returns The generated hash.\n   * @example\n   * ```typescript\n   * const crcHash = hashGenerator.generate(data);\n   * ```\n   */\n  public generate(data: BinaryLike): string {\n    return this.config.prefix + createHash(this.config.algorithm).update(data).digest(this.config.encoding);\n  }\n\n  /**\n   * Generate a crc hash.\n   * @param data - The data to generate the crc hash from.\n   * @returns The generated crc hash.\n   */\n  public generateCrc(data: BinaryLike): string {\n    const crc = createHash('sha1').update(data).digest(this.config.encoding);\n    return this.config.crcLength == null || this.config.crcLength < 1 ? crc : crc.slice(0, this.config.crcLength);\n  }\n\n  /**\n   * Generate a **self-validate** hash from data.\n   * @param data - The data to generate the self-validated hash from.\n   * @returns The generated self-validated hash.\n   * @example\n   * ```typescript\n   * const userId = hashGenerator.generateSelfValidate(data);\n   * ```\n   */\n  public generateSelfValidate(data: BinaryLike): string {\n    const mainHash = this.generate(data);\n    const crcHash = this.generateCrc(mainHash);\n    return mainHash + crcHash;\n  }\n\n  /**\n   * Verify if the generated hash matches the provided hash.\n   * @param data - The data to verify.\n   * @param hash - The hash to compare against.\n   * @returns `true` if the hash is verified, `false` otherwise.\n   * @example\n   * ```typescript\n   * if (!hashGenerator.verify(data, hash)) {\n   *   new Error('data_corrupted');\n   * }\n   * ```\n   */\n  public verify(data: BinaryLike, hash: string): boolean {\n    return hash === this.generate(data);\n  }\n\n  /**\n   * Verify a **self-validate** hash to check if it was generated by this class (with the same options).\n   * @param hash - The self-validated hash to verify.\n   * @returns `true` if the hash is verified, `false` otherwise.\n   * @example\n   * ```typescript\n   * if (!hashGenerator.verifySelfValidate(hash)) {\n   *   new Error('invalid_hash');\n   * }\n   * ```\n   */\n  public verifySelfValidate(hash: string): boolean {\n    const gapPos = hash.length - this.config.crcLength;\n    const mainHash = hash.slice(0, gapPos);\n    const crcHash = hash.slice(gapPos);\n    return crcHash === this.generateCrc(mainHash);\n  }\n}\n", "import {createHmac, type BinaryToTextEncoding} from 'node:crypto';\n\nimport {parseDuration, type Duration} from '@alwatr/parse-duration';\n\nimport type {CryptoAlgorithm} from './type.js';\n\nexport type TokenValidity = 'valid' | 'invalid' | 'expired';\n\n/**\n * Represents the configuration for a token generator.\n */\nexport interface TokenGeneratorConfig {\n  /**\n   * The prefix to be added to the generated hash.\n   */\n  prefix: string;\n\n  /**\n   * The algorithm used for hashing.\n   */\n  algorithm: CryptoAlgorithm;\n\n  /**\n   * The encoding used for the generated hash.\n   */\n  encoding: BinaryToTextEncoding;\n\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 * Secure authentication HOTP token generator (HMAC-based One-Time Password algorithm).\n */\nexport class AlwatrTokenGenerator {\n  private _duration: number;\n\n  /**\n   * The current epoch based on the configured duration.\n   */\n  protected get _epoch(): number {\n    return this._duration == 0 ? 0 : Math.floor(Date.now() / this._duration);\n  }\n\n  /**\n   * Creates a new instance of AlwatrTokenGenerator.\n   * @param config The configuration for the token generator.\n   */\n  constructor(public config: TokenGeneratorConfig) {\n    this._duration = config.duration == 'infinite' ? 0 : parseDuration(config.duration);\n  }\n\n  /**\n   * Generates a HOTP token based on the provided data for special duration.\n   * @param data The data to generate the token from.\n   * @returns The generated token.\n   * @example\n   * ```typescript\n   * user.auth = tokenGenerator.generate(`${user.id}-${user.role}`);\n   * ```\n   */\n  public generate(data: string): string {\n    return this._generate(data, this._epoch);\n  }\n\n  /**\n   * Verifies if a token is valid.\n   * @param data The data used to generate the token.\n   * @param token The token to verify.\n   * @returns The validity of the token.\n   * @example\n   * ```typescript\n   * const validateStatus = tokenGenerator.verify([user.id,user.role].join(), user.auth);\n   * ```\n   */\n  public verify(data: string, token: string): TokenValidity {\n    const epoch = this._epoch;\n    if (token === this._generate(data, epoch)) return 'valid';\n    if (this._duration == 0) return 'invalid';\n    if (token === this._generate(data, epoch - 1)) return 'expired';\n    return 'invalid';\n  }\n\n  /**\n   * Generates a cryptographic token based on the provided data and epoch.\n   * @param data - The data to be used in the token generation.\n   * @param epoch - The epoch value to be used in the token generation.\n   * @returns The generated cryptographic token.\n   */\n  protected _generate(data: string, epoch: number): string {\n    return (\n      this.config.prefix +\n      createHmac(this.config.algorithm, data)\n        .update(data + epoch)\n        .digest(this.config.encoding)\n    );\n  }\n}\n", "import type {HashGeneratorConfig} from './hash.js';\nimport type {TokenGeneratorConfig} from './token.js';\n\n/**\n * Alwatr hash generator recommended configuration for making random self-validate **user-id**.\n */\nexport const userIdGeneratorRecommendedConfig: HashGeneratorConfig = /* #__PURE__ */ {\n  prefix: 'u',\n  algorithm: 'sha1',\n  encoding: 'base64url',\n  crcLength: 4,\n};\n\n/**\n * Hash generator recommended configuration for making random self-validate **device-id**.\n */\nexport const deviceIdGeneratorRecommendedConfig: HashGeneratorConfig = /* #__PURE__ */ {\n  ...userIdGeneratorRecommendedConfig,\n  prefix: 'd',\n};\n\n/**\n * Hash generator pre configuration for making random self-validate **secrets**.\n */\nexport const secretGeneratorRecommendedConfig: HashGeneratorConfig = /* #__PURE__ */ {\n  prefix: 's',\n  algorithm: 'sha384',\n  encoding: 'base64url',\n  crcLength: 4,\n};\n\n/**\n * Token generator recommended configuration for making secure self-validate **user-token**.\n */\nexport const userTokenGeneratorRecommendedConfig: Omit<TokenGeneratorConfig, 'secret' | 'duration'> = /* #__PURE__ */ {\n  prefix: 't',\n  algorithm: 'sha224',\n  encoding: 'base64url',\n};\n", "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"],
  "mappings": ";;qqBAAA,+fCAA,uBAAkF,uBAgC3E,IAAM,oBAAN,KAA0B,CAK/B,YAAmB,OAA6B,CAA7B,kBAA8B,CAU1C,gBAAyB,CAC9B,OAAO,KAAK,YAAS,gCAAY,EAAE,CAAC,CACtC,CAUO,4BAAqC,CAC1C,OAAO,KAAK,wBAAqB,gCAAY,EAAE,CAAC,CAClD,CAWO,SAAS,KAA0B,CACxC,OAAO,KAAK,OAAO,UAAS,+BAAW,KAAK,OAAO,SAAS,EAAE,OAAO,IAAI,EAAE,OAAO,KAAK,OAAO,QAAQ,CACxG,CAOO,YAAY,KAA0B,CAC3C,MAAM,OAAM,+BAAW,MAAM,EAAE,OAAO,IAAI,EAAE,OAAO,KAAK,OAAO,QAAQ,EACvE,OAAO,KAAK,OAAO,WAAa,MAAQ,KAAK,OAAO,UAAY,EAAI,IAAM,IAAI,MAAM,EAAG,KAAK,OAAO,SAAS,CAC9G,CA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