Skip to content
4 changes: 4 additions & 0 deletions src/core/lib/Base32.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ export const ALPHABET_OPTIONS = [
name: "Hex Extended", // https://www.rfc-editor.org/rfc/rfc4648#section-7
value: "0-9A-V=",
},
{
name: "Crockford's alphabet", // https://www.crockford.com/base32.html
value: "0-9A-HJKMNP-TV-Z=",
},
];

23 changes: 19 additions & 4 deletions src/core/operations/FromBase32.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -42,12 +43,17 @@ class FromBase32 extends Operation {
{
pattern: "^(?:[A-Z2-7]{8})+(?:[A-Z2-7]{2}={6}|[A-Z2-7]{4}={4}|[A-Z2-7]{5}={3}|[A-Z2-7]{7}={1})?$",
flags: "",
args: ["A-Z2-7=", false]
args: [ALPHABET_OPTIONS.find(opt => opt.name === "Standard").value, false]
},
{
pattern: "^(?:[0-9A-V]{8})+(?:[0-9A-V]{2}={6}|[0-9A-V]{4}={4}|[0-9A-V]{5}={3}|[0-9A-V]{7}={1})?$",
flags: "",
args: ["0-9A-V=", false]
args: [ALPHABET_OPTIONS.find(opt => opt.name === "Hex Extended").value, false]
},
{
pattern: "^(?:[0-9A-TV-Za-tv-z]{8})+(?:[0-9A-TV-Za-tv-z]{2}={6}|[0-9A-TV-Za-tv-z]{4}={4}|[0-9A-TV-Za-tv-z]{5}={3}|[0-9A-TV-Za-tv-z]{7}=)?$",
flags: "",
args: [ALPHABET_OPTIONS.find(opt => opt.name === "Crockford's alphabet").value, false]
}
];
}
Expand All @@ -60,11 +66,20 @@ 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
}
const isCrockford = alphabet === Utils.expandAlphRange(ALPHABET_OPTIONS.find(opt => opt.name === "Crockford's alphabet").value).join("");
if (isCrockford) {
input = input
.replace(/[oO]/g, "0")
.replace(/[iIlL]/g, "1")
.toUpperCase();
}
let chr1, chr2, chr3, chr4, chr5,
enc1, enc2, enc3, enc4, enc5, enc6, enc7, enc8,
i = 0;
Expand Down
12 changes: 9 additions & 3 deletions src/core/operations/RC2Decrypt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class RC2Decrypt extends Operation {
"value": "",
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
},
{
name: "Effective key bits",
type: "number",
value: 128
},
{
"name": "IV",
"type": "toggleString",
Expand All @@ -58,9 +63,10 @@ class RC2Decrypt extends Operation {
*/
run(input, args) {
const key = Utils.convertToByteString(args[0].string, args[0].option),
iv = Utils.convertToByteString(args[1].string, args[1].option),
[,, inputType, outputType] = args,
decipher = forge.rc2.createDecryptionCipher(key);
effective = args[1],
iv = Utils.convertToByteString(args[2].string, args[2].option),
[,,, inputType, outputType] = args,
decipher = forge.rc2.createDecryptionCipher(key, effective);

input = Utils.convertToByteString(input, inputType);

Expand Down
13 changes: 9 additions & 4 deletions src/core/operations/RC2Encrypt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Operation from "../Operation.mjs";
import Utils from "../Utils.mjs";
import forge from "node-forge";


/**
* RC2 Encrypt operation
*/
Expand All @@ -33,6 +32,11 @@ class RC2Encrypt extends Operation {
"value": "",
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
},
{
name: "Effective key bits",
type: "number",
value: 128
},
{
"name": "IV",
"type": "toggleString",
Expand All @@ -59,9 +63,10 @@ class RC2Encrypt extends Operation {
*/
run(input, args) {
const key = Utils.convertToByteString(args[0].string, args[0].option),
iv = Utils.convertToByteString(args[1].string, args[1].option),
[,, inputType, outputType] = args,
cipher = forge.rc2.createEncryptionCipher(key);
effective = args[1],
iv = Utils.convertToByteString(args[2].string, args[2].option),
[,,, inputType, outputType] = args,
cipher = forge.rc2.createEncryptionCipher(key, effective);

input = Utils.convertToByteString(input, inputType);

Expand Down
7 changes: 6 additions & 1 deletion src/core/operations/ToBase32.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Loading
Loading