{"version":3,"file":"index.cjs","names":[],"sources":["../src/identity-exception.ts","../src/identity-common-exception.ts","../src/base64url.ts","../src/decode-jwt.ts","../src/hex.ts","../src/types.ts"],"sourcesContent":["/**\n * IdentityException is the base error class for all identity-common-ts packages.\n *\n * Package-specific exception classes should extend this class.\n */\nexport class IdentityException extends Error {\n  public details?: unknown\n\n  constructor(message: string, details?: unknown) {\n    super(message)\n    Object.setPrototypeOf(this, IdentityException.prototype)\n    this.name = 'IdentityException'\n    this.details = details\n  }\n\n  getFullMessage(): string {\n    return `${this.name}: ${this.message} ${this.details ? `- ${JSON.stringify(this.details)}` : ''}`\n  }\n}\n","import { IdentityException } from './identity-exception'\n\n/**\n * IdentityCommonException is a custom error class for identity-common-related exceptions.\n */\nexport class IdentityCommonException extends IdentityException {\n  constructor(message: string, details?: unknown) {\n    super(message, details)\n    Object.setPrototypeOf(this, IdentityCommonException.prototype)\n    this.name = 'IdentityCommonException'\n  }\n}\n","import { IdentityCommonException } from './identity-common-exception'\n\nconst BASE64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'\n\nconst bytesToBase64 = (bytes: Uint8Array): string => {\n  let result = ''\n  let i: number\n\n  for (i = 0; i < bytes.length - 2; i += 3) {\n    const chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]\n    result += BASE64_CHARS[(chunk >> 18) & 63]\n    result += BASE64_CHARS[(chunk >> 12) & 63]\n    result += BASE64_CHARS[(chunk >> 6) & 63]\n    result += BASE64_CHARS[chunk & 63]\n  }\n\n  // Handle padding\n  if (i < bytes.length) {\n    const chunk = (bytes[i] << 16) | (i + 1 < bytes.length ? bytes[i + 1] << 8 : 0)\n    result += BASE64_CHARS[(chunk >> 18) & 63]\n    result += BASE64_CHARS[(chunk >> 12) & 63]\n    result += i + 1 < bytes.length ? BASE64_CHARS[(chunk >> 6) & 63] : '='\n    result += '='\n  }\n\n  return result\n}\n\nconst base64ToBytes = (base64: string): Uint8Array => {\n  // Validate input - only base64 characters and padding are allowed\n  const validBase64Regex = /^[A-Za-z0-9+/]*={0,2}$/\n  if (!validBase64Regex.test(base64)) {\n    throw new IdentityCommonException('Invalid base64 string: contains invalid characters')\n  }\n\n  // Remove padding\n  const cleanBase64 = base64.replace(/=/g, '')\n  const length = cleanBase64.length\n  const bytes = new Uint8Array((length * 3) >> 2)\n\n  let byteIndex = 0\n  for (let i = 0; i < length; i += 4) {\n    const a = BASE64_CHARS.indexOf(cleanBase64[i])\n    const b = BASE64_CHARS.indexOf(cleanBase64[i + 1])\n    const c = i + 2 < length ? BASE64_CHARS.indexOf(cleanBase64[i + 2]) : 0\n    const d = i + 3 < length ? BASE64_CHARS.indexOf(cleanBase64[i + 3]) : 0\n\n    bytes[byteIndex++] = (a << 2) | (b >> 4)\n    if (i + 2 < length) bytes[byteIndex++] = ((b & 15) << 4) | (c >> 2)\n    if (i + 3 < length) bytes[byteIndex++] = ((c & 3) << 6) | d\n  }\n\n  return bytes\n}\n\nconst bytesToBase64Url = (bytes: Uint8Array): string => {\n  return bytesToBase64(bytes).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=/g, '')\n}\n\nconst base64UrlToBytes = (base64url: string): Uint8Array => {\n  // Validate input - only base64url characters (no padding) are allowed\n  const validBase64UrlRegex = /^[A-Za-z0-9_-]*$/\n  if (!validBase64UrlRegex.test(base64url)) {\n    throw new IdentityCommonException('Invalid base64url string: contains invalid characters')\n  }\n\n  // Convert base64url to base64\n  let base64 = base64url.replace(/-/g, '+').replace(/_/g, '/')\n  // Add padding if needed\n  while (base64.length % 4) {\n    base64 += '='\n  }\n  return base64ToBytes(base64)\n}\n\nexport const stringToBytes = (str: string): Uint8Array => {\n  const bytes: number[] = []\n  for (let i = 0; i < str.length; i++) {\n    let codePoint = str.charCodeAt(i)\n\n    // Handle surrogate pairs\n    if (codePoint >= 0xd800 && codePoint <= 0xdbff && i + 1 < str.length) {\n      const low = str.charCodeAt(i + 1)\n      if (low >= 0xdc00 && low <= 0xdfff) {\n        codePoint = 0x10000 + ((codePoint - 0xd800) << 10) + (low - 0xdc00)\n        i++\n      }\n    }\n\n    if (codePoint < 0x80) {\n      // Single byte\n      bytes.push(codePoint)\n    } else if (codePoint < 0x800) {\n      // Two bytes\n      bytes.push(0xc0 | (codePoint >> 6))\n      bytes.push(0x80 | (codePoint & 0x3f))\n    } else if (codePoint < 0x10000) {\n      // Three bytes\n      bytes.push(0xe0 | (codePoint >> 12))\n      bytes.push(0x80 | ((codePoint >> 6) & 0x3f))\n      bytes.push(0x80 | (codePoint & 0x3f))\n    } else {\n      // Four bytes\n      bytes.push(0xf0 | (codePoint >> 18))\n      bytes.push(0x80 | ((codePoint >> 12) & 0x3f))\n      bytes.push(0x80 | ((codePoint >> 6) & 0x3f))\n      bytes.push(0x80 | (codePoint & 0x3f))\n    }\n  }\n\n  return new Uint8Array(bytes)\n}\n\nexport const bytesToString = (bytes: Uint8Array): string => {\n  let result = ''\n  let i = 0\n\n  while (i < bytes.length) {\n    const byte1 = bytes[i++]\n\n    if (byte1 < 0x80) {\n      // Single byte character\n      result += String.fromCharCode(byte1)\n    } else if (byte1 < 0xe0) {\n      // Two byte character\n      const byte2 = bytes[i++]\n      result += String.fromCharCode(((byte1 & 0x1f) << 6) | (byte2 & 0x3f))\n    } else if (byte1 < 0xf0) {\n      // Three byte character\n      const byte2 = bytes[i++]\n      const byte3 = bytes[i++]\n      result += String.fromCharCode(((byte1 & 0x0f) << 12) | ((byte2 & 0x3f) << 6) | (byte3 & 0x3f))\n    } else {\n      // Four byte character - convert to surrogate pair\n      const byte2 = bytes[i++]\n      const byte3 = bytes[i++]\n      const byte4 = bytes[i++]\n      const codePoint = ((byte1 & 0x07) << 18) | ((byte2 & 0x3f) << 12) | ((byte3 & 0x3f) << 6) | (byte4 & 0x3f)\n      const surrogate1 = 0xd800 + ((codePoint - 0x10000) >> 10)\n      const surrogate2 = 0xdc00 + ((codePoint - 0x10000) & 0x3ff)\n      result += String.fromCharCode(surrogate1, surrogate2)\n    }\n  }\n  return result\n}\n\nexport const concatBytes = (...byteArrays: Array<Uint8Array>) => {\n  const result = new Uint8Array(byteArrays.reduce((n, a) => n + a.byteLength, 0))\n  let offset = 0\n  for (const entry of byteArrays) {\n    result.set(entry, offset)\n    offset += entry.byteLength\n  }\n  return result\n}\n\nexport const compareBytes = (lhs: Uint8Array, rhs: Uint8Array) => {\n  if (lhs === rhs) return true\n  if (lhs.byteLength !== rhs.byteLength) return false\n  return lhs.every((b, i) => b === rhs[i])\n}\n\n// Public API - keeping backward compatible names\nexport const base64urlEncode = (input: string): string => bytesToBase64Url(stringToBytes(input))\n\nexport const base64urlDecode = (input: string): string => bytesToString(base64UrlToBytes(input))\n\nexport const uint8ArrayToBase64Url = bytesToBase64Url\n\nexport const base64UrlToUint8Array = base64UrlToBytes\n\n// Additional exports for flexibility\nexport const base64 = {\n  encode: bytesToBase64,\n  decode: base64ToBytes,\n}\n\nexport const base64url = {\n  encode: bytesToBase64Url,\n  decode: base64UrlToBytes,\n}\n","import { base64urlDecode } from './base64url'\nimport { IdentityCommonException } from './identity-common-exception'\n\nexport const decodeJwt = <H extends Record<string, unknown>, T extends Record<string, unknown>>(\n  jwt: string\n): { header: H; payload: T; signature: string } => {\n  const { 0: header, 1: payload, 2: signature, length } = jwt.split('.')\n  if (length !== 3) {\n    throw new IdentityCommonException('Invalid JWT as input')\n  }\n\n  return {\n    header: JSON.parse(base64urlDecode(header)),\n    payload: JSON.parse(base64urlDecode(payload)),\n    signature: signature,\n  }\n}\n","import { IdentityCommonException } from './identity-common-exception'\n\nconst HEX_CHARS = '0123456789abcdef'\n\nconst bytesToHex = (bytes: Uint8Array): string => {\n  let result = ''\n  for (let i = 0; i < bytes.length; i++) {\n    result += HEX_CHARS[(bytes[i] >> 4) & 0x0f]\n    result += HEX_CHARS[bytes[i] & 0x0f]\n  }\n  return result\n}\n\nconst hexToBytes = (hex: string): Uint8Array => {\n  // Validate input - only hex characters are allowed\n  const validHexRegex = /^[0-9a-fA-F]*$/\n  if (!validHexRegex.test(hex)) {\n    throw new IdentityCommonException('Invalid hex string: contains invalid characters')\n  }\n\n  // Handle odd-length hex strings by padding with leading zero\n  if (hex.length % 2 !== 0) {\n    hex = `0${hex}`\n  }\n\n  const bytes = new Uint8Array(hex.length / 2)\n  for (let i = 0; i < hex.length; i += 2) {\n    const high = HEX_CHARS.indexOf(hex[i].toLowerCase())\n    const low = HEX_CHARS.indexOf(hex[i + 1].toLowerCase())\n    bytes[i / 2] = (high << 4) | low\n  }\n  return bytes\n}\n\n// Public API - keeping backward compatible names\nexport const hexEncode = (input: Uint8Array): string => bytesToHex(input)\n\nexport const hexDecode = (input: string): Uint8Array => hexToBytes(input)\n\n// Additional exports for flexibility\nexport const hex = {\n  encode: bytesToHex,\n  decode: hexToBytes,\n}\n","// This type declaration is from lib.dom.ts\nexport interface RsaOtherPrimesInfo {\n  d?: string\n  r?: string\n  t?: string\n}\n\nexport interface JsonWebKey {\n  alg?: string\n  crv?: string\n  d?: string\n  dp?: string\n  dq?: string\n  e?: string\n  ext?: boolean\n  k?: string\n  key_ops?: string[]\n  kty?: string\n  n?: string\n  oth?: RsaOtherPrimesInfo[]\n  p?: string\n  q?: string\n  qi?: string\n  use?: string\n  x?: string\n  y?: string\n}\n\nexport interface JwtPayload {\n  cnf?: {\n    jwk: JsonWebKey\n  }\n  exp?: number\n  iat?: number\n  [key: string]: unknown\n}\n\nexport type Base64urlString = string\n\nexport type OrPromise<T> = T | Promise<T>\n\nexport type Signer = (data: string) => OrPromise<string>\nexport type Verifier<T = unknown> = (data: string, sig: string, options?: T) => OrPromise<boolean>\nexport type Hasher = (data: string | ArrayBuffer, alg: string) => OrPromise<Uint8Array>\nexport type SaltGenerator = (length: number) => OrPromise<string>\nexport type HasherAndAlg = {\n  hasher: Hasher\n  alg: string\n}\n\n// Sync versions\nexport type SignerSync = (data: string) => string\nexport type VerifierSync = (data: string, sig: string) => boolean\nexport type HasherSync = (data: string, alg: string) => Uint8Array\nexport type SaltGeneratorSync = (length: number) => string\nexport type HasherAndAlgSync = {\n  hasher: HasherSync\n  alg: string\n}\n\n// based on https://www.iana.org/assignments/named-information/named-information.xhtml\nexport const IANA_HASH_ALGORITHMS = [\n  'sha-256',\n  'sha-256-128',\n  'sha-256-120',\n  'sha-256-96',\n  'sha-256-64',\n  'sha-256-32',\n  'sha-384',\n  'sha-512',\n  'sha3-224',\n  'sha3-256',\n  'sha3-384',\n  'sha3-512',\n  'blake2s-256',\n  'blake2b-256',\n  'blake2b-512',\n  'k12-256',\n  'k12-512',\n] as const\n\nexport type HashAlgorithm = (typeof IANA_HASH_ALGORITHMS)[number]\n"],"mappings":";;;;;;;AAKA,IAAa,oBAAb,MAAa,0BAA0B,MAAM;CAG3C,YAAY,SAAiB,SAAmB;EAC9C,MAAM,OAAO;EACb,OAAO,eAAe,MAAM,kBAAkB,SAAS;EACvD,KAAK,OAAO;EACZ,KAAK,UAAU;CACjB;CAEA,iBAAyB;EACvB,OAAO,GAAG,KAAK,KAAK,IAAI,KAAK,QAAQ,GAAG,KAAK,UAAU,KAAK,KAAK,UAAU,KAAK,OAAO,MAAM;CAC/F;AACF;;;;;;ACbA,IAAa,0BAAb,MAAa,gCAAgC,kBAAkB;CAC7D,YAAY,SAAiB,SAAmB;EAC9C,MAAM,SAAS,OAAO;EACtB,OAAO,eAAe,MAAM,wBAAwB,SAAS;EAC7D,KAAK,OAAO;CACd;AACF;;;ACTA,MAAM,eAAe;AAErB,MAAM,iBAAiB,UAA8B;CACnD,IAAI,SAAS;CACb,IAAI;CAEJ,KAAK,IAAI,GAAG,IAAI,MAAM,SAAS,GAAG,KAAK,GAAG;EACxC,MAAM,QAAS,MAAM,MAAM,KAAO,MAAM,IAAI,MAAM,IAAK,MAAM,IAAI;EACjE,UAAU,aAAc,SAAS,KAAM;EACvC,UAAU,aAAc,SAAS,KAAM;EACvC,UAAU,aAAc,SAAS,IAAK;EACtC,UAAU,aAAa,QAAQ;CACjC;CA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