Skip to content

Commit

Permalink
add base 16 32 64 85 encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
CenekSanzak committed Jan 30, 2025
1 parent 1156f63 commit 40921b1
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/operations/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import {
RemoveSpaces,
Lowercase,
Uppercase,
Base64Encode,
Base64Decode,
Base16Encode,
Base16Decode,
Base32Encode,
Base32Decode,
Base85Encode,
Base85Decode,
} from "./string-operations";
import { Operation } from "./types";

Expand All @@ -11,4 +19,12 @@ export const operations: Operation[] = [
RemoveSpaces,
Lowercase,
Uppercase,
Base16Encode,
Base16Decode,
Base32Encode,
Base32Decode,
Base64Encode,
Base64Decode,
Base85Encode,
Base85Decode,
];
96 changes: 96 additions & 0 deletions src/operations/string-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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 },
};
1 change: 1 addition & 0 deletions src/operations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum OperationTags {
All = "All",
Text = "Text",
BasicEncryption = "Basic Encryption",
Encoding = "Encoding",
}

export enum IOTypes {
Expand Down
3 changes: 3 additions & 0 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
46 changes: 46 additions & 0 deletions wasm/src/string_ops/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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())
}

0 comments on commit 40921b1

Please sign in to comment.