base32.d.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Implements base32 and base32hex from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
  3. * (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
  4. *
  5. * ```js
  6. * import { fromBase32, toBase32 } from '@exodus/bytes/base32.js'
  7. * import { fromBase32hex, toBase32hex } from '@exodus/bytes/base32.js'
  8. * ```
  9. *
  10. * @module @exodus/bytes/base32.js
  11. */
  12. /// <reference types="node" />
  13. import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
  14. /**
  15. * Options for base32 encoding
  16. */
  17. export interface ToBase32Options {
  18. /** Whether to include padding characters (default: false) */
  19. padding?: boolean;
  20. }
  21. /**
  22. * Padding mode for base32 decoding
  23. * - `true`: padding is required
  24. * - `false`: padding is not allowed
  25. * - `'both'`: padding is optional (default)
  26. */
  27. export type PaddingMode = boolean | 'both';
  28. /**
  29. * Options for base32 decoding
  30. */
  31. export interface FromBase32Options {
  32. /** Output format (default: 'uint8') */
  33. format?: OutputFormat;
  34. /** Padding mode */
  35. padding?: PaddingMode;
  36. }
  37. /**
  38. * Encode a `Uint8Array` to a base32 string (RFC 4648)
  39. *
  40. * @param arr - The input bytes
  41. * @param options - Encoding options
  42. * @returns The base32 encoded string
  43. */
  44. export function toBase32(arr: Uint8Array, options?: ToBase32Options): string;
  45. /**
  46. * Encode a `Uint8Array` to a base32hex string (RFC 4648)
  47. *
  48. * @param arr - The input bytes
  49. * @param options - Encoding options (padding defaults to false)
  50. * @returns The base32hex encoded string
  51. */
  52. export function toBase32hex(arr: Uint8Array, options?: ToBase32Options): string;
  53. /**
  54. * Decode a base32 string to bytes
  55. *
  56. * Operates in strict mode for last chunk, does not allow whitespace
  57. *
  58. * @param string - The base32 encoded string
  59. * @param options - Decoding options
  60. * @returns The decoded bytes
  61. */
  62. export function fromBase32(string: string, options?: FromBase32Options): Uint8ArrayBuffer;
  63. export function fromBase32(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
  64. /**
  65. * Decode a base32hex string to bytes
  66. *
  67. * Operates in strict mode for last chunk, does not allow whitespace
  68. *
  69. * @param string - The base32hex encoded string
  70. * @param options - Decoding options
  71. * @returns The decoded bytes
  72. */
  73. export function fromBase32hex(string: string, options?: FromBase32Options): Uint8ArrayBuffer;
  74. export function fromBase32hex(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;