(buffer: Buffer)
| 481 | * @returns {string} |
| 482 | */ |
| 483 | export function base32Encode(buffer: Buffer): string { |
| 484 | let output = ''; |
| 485 | // This can grow up to 12 bits |
| 486 | let digest = 0; |
| 487 | // How many bits are we actually using from digest |
| 488 | let bits = 0; |
| 489 | |
| 490 | function encodeNewWord() { |
| 491 | // Get first 5 bits |
| 492 | const word = digest & 0b11111; |
| 493 | const character = BASE32_ALPHABET[word]; |
| 494 | output += character; |
| 495 | bits -= 5; |
| 496 | // Shift out the newly processed word |
| 497 | digest = digest >>> 5; |
| 498 | } |
| 499 | |
| 500 | for (const byte of buffer) { |
| 501 | // Append current byte to digest |
| 502 | digest = (byte << bits) | digest; |
| 503 | bits += 8; |
| 504 | |
| 505 | // Add characters until we run out of bits |
| 506 | while (bits >= 5) { |
| 507 | encodeNewWord(); |
| 508 | } |
| 509 | } |
| 510 | // Do we need a last pass? |
| 511 | if (bits !== 0) { |
| 512 | encodeNewWord(); |
| 513 | } |
| 514 | |
| 515 | return output; |
| 516 | } |
| 517 | |
| 518 | // Splits a : separated list into its own array, or to default if input is undefined |
| 519 | export function splitIntoArray(input?: string, defaultArray: string[] = []): string[] { |
nothing calls this directly
no test coverage detected