gchq/CyberChef · error · OperationError

Error: alphabet must be of length 58

Error message

Error: alphabet must be of length 58

What it means

To Base58 requires an alphabet of exactly 58 characters, and all 58 must be distinct. The guard checks both length === 58 and unique count === 58, because Base58's whole purpose is a 58-symbol alphabet (Bitcoin-style excludes ambiguous 0/O/I/l).

Source

Thrown at src/core/operations/ToBase58.mjs:52

            }
        ];
    }

    /**
     * @param {ArrayBuffer} input
     * @param {Object[]} args
     * @returns {string}
     */
    run(input, args) {
        input = new Uint8Array(input);
        let alphabet = args[0] || ALPHABET_OPTIONS[0].value,
            result = [];

        alphabet = Utils.expandAlphRange(alphabet).join("");

        if (alphabet.length !== 58 ||
            [].unique.call(alphabet).length !== 58) {
            throw new OperationError("Error: alphabet must be of length 58");
        }

        if (input.length === 0) return "";

        let zeroPrefix = 0;
        for (let i = 0; i < input.length && input[i] === 0; i++) {
            zeroPrefix++;
        }

        input.forEach(function(b) {
            let carry = b;

            for (let i = 0; i < result.length; i++) {
                carry += result[i] << 8;
                result[i] = carry % 58;
                carry = (carry / 58) | 0;
            }

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Use the default Bitcoin alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz.
  2. Count the alphabet characters and ensure exactly 58 unique ones.
  3. Use an alphabetic range expansion (e.g. 'A-Z') only if it expands to 58 unique characters.

Example fix

// before: alphabet = "12345..." (truncated, <58 chars) -> throws
// after:  alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" -> 58 unique, passes
Defensive patterns

Strategy: validation

Validate before calling

const set = new Set([...alphabet]);
if (alphabet.length !== 58 || set.size !== 58) {
  throw new Error("Base58 alphabet must have 58 unique chars");
}

Type guard

const isValidBase58Alphabet = a => a.length === 58 && new Set(a).size === 58;

Try / catch

try { toBase58(input, [alphabet]); }
catch (e) { if (/length 58/.test(e.message)) { alphabet = DEFAULT_BASE58; } else throw e; }

Prevention

When it happens

Trigger: Editing the alphabet argument so it no longer has 58 unique characters: fewer/more characters, duplicate characters, or an expanded alphabetic range (e.g. A-Z) that does not total 58.

Common situations: Customising the alphabet for a non-standard Base58 variant; accidental truncation when pasting; typing an alphabet with a repeated symbol.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/0d605661e5929ea8. Report an issue: GitHub.