Skip to content

Instantly share code, notes, and snippets.

@mgild
Created April 24, 2024 00:18
Show Gist options
  • Save mgild/8568ea74c69270e496bbb7e819ec10e0 to your computer and use it in GitHub Desktop.
Save mgild/8568ea74c69270e496bbb7e819ec10e0 to your computer and use it in GitHub Desktop.
#!/bin/bash
which base58 > /dev/null || (brew install pipx && pipx install base58)
# Function to detect if the input is hex
is_hex() {
[[ $1 =~ ^[0-9a-fA-F]+$ ]] && return 0 || return 1
}
# Function to detect if the input is base64
is_base64() {
[[ $1 =~ ^[A-Za-z0-9+/=]+$ ]] && return 0 || return 1
}
is_base58() {
[[ $1 =~ ^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$ ]] && return 0 || return 1
}
# Function to convert hex to binary
hex_to_binary() {
echo "$1" | xxd -r -p
}
# Function to convert binary to hex
binary_to_hex() {
echo "$1" | xxd -p | tr -d '\n'
}
# Function to convert binary to base64
binary_to_base64() {
echo -n "$1" | base64
}
# Function to convert binary to base58
binary_to_base58() {
echo -n "$1" | base58
}
# Function to convert base64 to binary
base64_to_binary() {
echo "$1" | base64 --decode
}
# Function to convert base58 to binary
base58_to_binary() {
echo "$1" | base58 -d
}
# Main script starts here
input="$1"
if is_hex "$input"; then
echo "Detected Hex"
binary=$(hex_to_binary "$input")
elif is_base58 "$input"; then
echo "Detected Base58"
binary=$(base58_to_binary "$input")
elif is_base64 "$input"; then
echo "Detected Base64"
binary=$(base64_to_binary "$input")
else
echo "The input does not seem to be valid Hex, Base64, or Base58."
exit 1
fi
# Convert binary to all formats
hex_output=$(binary_to_hex "$binary")
base64_output=$(binary_to_base64 "$binary")
base58_output=$(binary_to_base58 "$binary")
echo "Hex: $hex_output"
echo "Base64: $base64_output"
echo "Base58: $base58_output"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment