diff --git a/src/operations/operations.ts b/src/operations/operations.ts index b130be5..eaec73c 100644 --- a/src/operations/operations.ts +++ b/src/operations/operations.ts @@ -3,6 +3,14 @@ import { RemoveSpaces, Lowercase, Uppercase, + Base64Encode, + Base64Decode, + Base16Encode, + Base16Decode, + Base32Encode, + Base32Decode, + Base85Encode, + Base85Decode, } from "./string-operations"; import { Operation } from "./types"; @@ -11,4 +19,12 @@ export const operations: Operation[] = [ RemoveSpaces, Lowercase, Uppercase, + Base16Encode, + Base16Decode, + Base32Encode, + Base32Decode, + Base64Encode, + Base64Decode, + Base85Encode, + Base85Decode, ]; diff --git a/src/operations/string-operations.ts b/src/operations/string-operations.ts index 795d28c..745edc3 100644 --- a/src/operations/string-operations.ts +++ b/src/operations/string-operations.ts @@ -4,6 +4,14 @@ import { uppercase_string, lowercase_string, remove_whitespace, + encode_base64, + decode_base64, + encode_base16, + decode_base16, + encode_base32, + decode_base32, + encode_base85, + decode_base85, } from "wasm"; const validateOutputTypeStringToString = @@ -63,3 +71,91 @@ export const RemoveSpaces: Operation = { inputs: { input: IOTypes.Text }, outputs: { output: IOTypes.Text }, }; + +export const Base64Encode: Operation = { + name: "Base64 Encode", + id: "base64-encode", + description: "Encodes the input string to Base64", + value: "", + func: validateOutputTypeStringToString(encode_base64), + tags: [OperationTags.Encoding, OperationTags.All], + inputs: { input: IOTypes.Text }, + outputs: { output: IOTypes.Text }, +}; + +export const Base64Decode: Operation = { + name: "Base64 Decode", + id: "base64-decode", + description: "Decodes a Base64 encoded string", + value: "", + func: validateOutputTypeStringToString(decode_base64), + tags: [OperationTags.Encoding, OperationTags.All], + inputs: { input: IOTypes.Text }, + outputs: { output: IOTypes.Text }, +}; + +export const Base16Encode: Operation = { + name: "Base16 (Hex) Encode", + id: "base16-encode", + description: "Encodes the input string to Base16 (hexadecimal)", + value: "", + func: validateOutputTypeStringToString(encode_base16), + tags: [OperationTags.Encoding, OperationTags.All], + inputs: { input: IOTypes.Text }, + outputs: { output: IOTypes.Text }, +}; + +export const Base16Decode: Operation = { + name: "Base16 (Hex) Decode", + id: "base16-decode", + description: "Decodes a Base16 (hexadecimal) encoded string", + value: "", + func: validateOutputTypeStringToString(decode_base16), + tags: [OperationTags.Encoding, OperationTags.All], + inputs: { input: IOTypes.Text }, + outputs: { output: IOTypes.Text }, +}; + +export const Base32Encode: Operation = { + name: "Base32 Encode", + id: "base32-encode", + description: "Encodes the input string to Base32", + value: "", + func: validateOutputTypeStringToString(encode_base32), + tags: [OperationTags.Encoding, OperationTags.All], + inputs: { input: IOTypes.Text }, + outputs: { output: IOTypes.Text }, +}; + +export const Base32Decode: Operation = { + name: "Base32 Decode", + id: "base32-decode", + description: "Decodes a Base32 encoded string", + value: "", + func: validateOutputTypeStringToString(decode_base32), + tags: [OperationTags.Encoding, OperationTags.All], + inputs: { input: IOTypes.Text }, + outputs: { output: IOTypes.Text }, +}; + +export const Base85Encode: Operation = { + name: "Base85 Encode", + id: "base85-encode", + description: "Encodes the input string to Base85", + value: "", + func: validateOutputTypeStringToString(encode_base85), + tags: [OperationTags.Encoding, OperationTags.All], + inputs: { input: IOTypes.Text }, + outputs: { output: IOTypes.Text }, +}; + +export const Base85Decode: Operation = { + name: "Base85 Decode", + id: "base85-decode", + description: "Decodes a Base85 encoded string", + value: "", + func: validateOutputTypeStringToString(decode_base85), + tags: [OperationTags.Encoding, OperationTags.All], + inputs: { input: IOTypes.Text }, + outputs: { output: IOTypes.Text }, +}; diff --git a/src/operations/types.ts b/src/operations/types.ts index 261a2f8..64fc5bb 100644 --- a/src/operations/types.ts +++ b/src/operations/types.ts @@ -2,6 +2,7 @@ export enum OperationTags { All = "All", Text = "Text", BasicEncryption = "Basic Encryption", + Encoding = "Encoding", } export enum IOTypes { diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index 9e1ad63..26cf350 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -19,6 +19,9 @@ wasm-bindgen = "0.2.84" # code size when deploying. console_error_panic_hook = { version = "0.1.7", optional = true } image = "0.25.5" +base64 = "0.21.0" +data-encoding = "2.3.3" +base85 = "1.0.0" [dev-dependencies] wasm-bindgen-test = "0.3.34" diff --git a/wasm/src/string_ops/mod.rs b/wasm/src/string_ops/mod.rs index 0148273..e7f81f3 100644 --- a/wasm/src/string_ops/mod.rs +++ b/wasm/src/string_ops/mod.rs @@ -1,4 +1,7 @@ use wasm_bindgen::prelude::*; +use base64::{Engine as _, engine::general_purpose}; +use data_encoding::{HEXLOWER as BASE16, BASE32}; +use base85::{encode, decode}; #[wasm_bindgen] pub fn reverse_string(s: &str) -> String { @@ -25,4 +28,47 @@ pub fn uppercase_string(s: &str) -> String { #[wasm_bindgen] pub fn remove_whitespace(s: &str) -> String { s.chars().filter(|c| !c.is_whitespace() || *c == '\n').collect() +} + +#[wasm_bindgen] +pub fn encode_base64(s: &str) -> String { + general_purpose::STANDARD.encode(s.as_bytes()) +} + +#[wasm_bindgen] +pub fn decode_base64(s: &str) -> String { + match general_purpose::STANDARD.decode(s) { + Ok(decoded) => String::from_utf8_lossy(&decoded).into_owned(), + Err(_) => String::new() + } +} + +#[wasm_bindgen] +pub fn encode_base16(s: &str) -> String { + BASE16.encode(s.as_bytes()) +} + +#[wasm_bindgen] +pub fn decode_base16(s: &str) -> String { + BASE16.decode(s.as_bytes()).map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) +} + +#[wasm_bindgen] +pub fn encode_base32(s: &str) -> String { + BASE32.encode(s.as_bytes()) +} + +#[wasm_bindgen] +pub fn decode_base32(s: &str) -> String { + BASE32.decode(s.as_bytes()).map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) +} + +#[wasm_bindgen] +pub fn encode_base85(s: &str) -> String { + encode(s.as_bytes()) +} + +#[wasm_bindgen] +pub fn decode_base85(s: &str) -> String { + decode(s).map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) } \ No newline at end of file