diff --git a/src/core/operations/FromBase32.mjs b/src/core/operations/FromBase32.mjs index 06674919f..20944cca6 100644 --- a/src/core/operations/FromBase32.mjs +++ b/src/core/operations/FromBase32.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; import Utils from "../Utils.mjs"; import {ALPHABET_OPTIONS} from "../lib/Base32.mjs"; @@ -65,11 +66,14 @@ class FromBase32 extends Operation { run(input, args) { if (!input) return []; - const alphabet = args[0] ? - Utils.expandAlphRange(args[0]).join("") : "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=", + const alphabet = Utils.expandAlphRange(args[0]).join(""), removeNonAlphChars = args[1], output = []; + if (alphabet.length !== 33) { + throw new OperationError("Alphabet must be of length 33"); // 32 characters + 1 padding + } + let chr1, chr2, chr3, chr4, chr5, enc1, enc2, enc3, enc4, enc5, enc6, enc7, enc8, i = 0; diff --git a/src/core/operations/ToBase32.mjs b/src/core/operations/ToBase32.mjs index 44eb8b484..9e7255c2a 100644 --- a/src/core/operations/ToBase32.mjs +++ b/src/core/operations/ToBase32.mjs @@ -7,6 +7,7 @@ import Operation from "../Operation.mjs"; import Utils from "../Utils.mjs"; import {ALPHABET_OPTIONS} from "../lib/Base32.mjs"; +import OperationError from "../errors/OperationError.mjs"; /** * To Base32 operation @@ -43,7 +44,11 @@ class ToBase32 extends Operation { if (!input) return ""; input = new Uint8Array(input); - const alphabet = args[0] ? Utils.expandAlphRange(args[0]).join("") : "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567="; + const alphabet = Utils.expandAlphRange(args[0]).join(""); + if (alphabet.length !== 33) { + throw new OperationError("Alphabet must be of length 33"); // 32 characters + 1 padding + } + let output = "", chr1, chr2, chr3, chr4, chr5, enc1, enc2, enc3, enc4, enc5, enc6, enc7, enc8, diff --git a/tests/operations/tests/Base32.mjs b/tests/operations/tests/Base32.mjs index 28913b855..0afb3787f 100644 --- a/tests/operations/tests/Base32.mjs +++ b/tests/operations/tests/Base32.mjs @@ -44,6 +44,8 @@ const ALL_BYTES_EXTENDED_OUT = "000G40O40K30E209185GO38E1S8124GJ2GAHC5OO34D1M70T const ALL_BYTES_STANDARD_OUT = "AAAQEAYEAUDAOCAJBIFQYDIOB4IBCEQTCQKRMFYYDENBWHA5DYPSAIJCEMSCKJRHFAUSUKZMFUXC6MBRGIZTINJWG44DSOR3HQ6T4P2AIFBEGRCFIZDUQSKKJNGE2TSPKBIVEU2UKVLFOWCZLJNVYXK6L5QGCYTDMRSWMZ3INFVGW3DNNZXXA4LSON2HK5TXPB4XU634PV7H7AEBQKBYJBMGQ6EITCULRSGY5D4QSGJJHFEVS2LZRGM2TOOJ3HU7UCQ2FI5EUWTKPKFJVKV2ZLNOV6YLDMVTWS23NN5YXG5LXPF5X274BQOCYPCMLRWHZDE4VS6MZXHM7UGR2LJ5JVOW27MNTWW33TO55X7A4HROHZHF43T6R2PK5PWO33XP6DY7F47U6X3PP6HZ7L57Z7P674======"; const ALL_BYTES_CROCKFORD_OUT = "000G40R40M30E209185GR38E1W8124GK2GAHC5RR34D1P70X3RFJ08924CJ2A9H750MJMASC5MQ2YC1H68SK8D9P6WW3JEHV7GYKWFT085146H258S3MGJAA9D64TKJFA18N4MTMANB5EP2SB9DNRQAYBXG62RK3CHJPCSV8D5N6PV3DDSQQ0WBJEDT7AXKQF1WQMYVWFNZ7Z041GA1R91C6GY48K2MBHJ6RX3WGJ699754NJTBSH6CTKEE9V7MZM2GT58X4MPKAFA59NANTSBDENYRB3CNKPJTVDDXRQ6XBQF5XQTZW1GE2RF2CBHP7S34WNJYCSQ7CZM6HTB9X9NEPTZCDKPPVVKEXXQZ0W7HE7S75WVKYHTFAXFPEVVQFY3RZ5WZMYQVFFY7SZBXZSZFYZW======"; +const WRONG_ALPHABET = "Alphabet must be of length 33" + TestRegister.addTests([ { name: "To Base32 Standard: nothing", @@ -243,5 +245,71 @@ TestRegister.addTests([ }, ], }, + { + name: "To Base32 Standard: wrong alphabet", + input: STANDARD_INP, + expectedOutput: WRONG_ALPHABET, + recipeConfig: [ + { + op: "To Base32", + args: [""], + }, + ], + }, + { + name: "To Base32 Hex Extended: wrong alphabet", + input: EXTENDED_INP, + expectedOutput: WRONG_ALPHABET, + recipeConfig: [ + { + op: "To Base32", + args: [""], + }, + ], + }, + { + name: "To Base32 Crockford: wrong alphabet", + input: CROCKFORD_INP, + expectedOutput: WRONG_ALPHABET, + recipeConfig: [ + { + op: "To Base32", + args: [""], + }, + ], + }, + { + name: "From Base32 Standard: wrong alphabet", + input: STANDARD_OUT, + expectedOutput: WRONG_ALPHABET, + recipeConfig: [ + { + op: "From Base32", + args: ["", false], + }, + ], + }, + { + name: "From Base32 Hex Extended: wrong alphabet", + input: EXTENDED_OUT, + expectedOutput: WRONG_ALPHABET, + recipeConfig: [ + { + op: "From Base32", + args: ["", false], + }, + ], + }, + { + name: "From Base32 Crockford: wrong alphabet", + input: CROCKFORD_OUT, + expectedOutput: WRONG_ALPHABET, + recipeConfig: [ + { + op: "From Base32", + args: ["", false], + }, + ], + }, ]);