base32.d.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * Encode a `Uint8Array` to a Crockford base32 string
  55. *
  56. * @param arr - The input bytes
  57. * @param options - Encoding options (padding defaults to false)
  58. * @returns The Crockford base32 encoded string
  59. */
  60. export function toBase32crockford(arr: Uint8Array, options?: ToBase32Options): string;
  61. /**
  62. * Decode a base32 string to bytes
  63. *
  64. * Operates in strict mode for last chunk, does not allow whitespace
  65. *
  66. * @param string - The base32 encoded string
  67. * @param options - Decoding options
  68. * @returns The decoded bytes
  69. */
  70. export function fromBase32(string: string, options?: FromBase32Options & { format?: 'uint8' }): Uint8ArrayBuffer;
  71. export function fromBase32(string: string, options: FromBase32Options & { format: 'arraybuffer' }): ArrayBuffer;
  72. export function fromBase32(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
  73. export function fromBase32(string: string, options?: FromBase32Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
  74. /**
  75. * Decode a base32hex string to bytes
  76. *
  77. * Operates in strict mode for last chunk, does not allow whitespace
  78. *
  79. * @param string - The base32hex encoded string
  80. * @param options - Decoding options
  81. * @returns The decoded bytes
  82. */
  83. export function fromBase32hex(string: string, options?: FromBase32Options & { format?: 'uint8' }): Uint8ArrayBuffer;
  84. export function fromBase32hex(string: string, options: FromBase32Options & { format: 'arraybuffer' }): ArrayBuffer;
  85. export function fromBase32hex(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
  86. export function fromBase32hex(string: string, options?: FromBase32Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
  87. /**
  88. * Decode a Crockford base32 string to bytes
  89. *
  90. * Operates in strict mode for last chunk, does not allow whitespace
  91. *
  92. * Crockford base32 decoding follows extra mapping per spec: `LIli -> 1, Oo -> 0`
  93. *
  94. * @param string - The Crockford base32 encoded string
  95. * @param options - Decoding options
  96. * @returns The decoded bytes
  97. */
  98. export function fromBase32crockford(string: string, options?: FromBase32Options & { format?: 'uint8' }): Uint8ArrayBuffer;
  99. export function fromBase32crockford(string: string, options: FromBase32Options & { format: 'arraybuffer' }): ArrayBuffer;
  100. export function fromBase32crockford(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
  101. export function fromBase32crockford(string: string, options?: FromBase32Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;