Skip to content

Instantly share code, notes, and snippets.

@WMAL
Last active October 27, 2024 17:48
Show Gist options
  • Save WMAL/57bb7a700d8a7ba0c0a0d0d26d5afc8a to your computer and use it in GitHub Desktop.
Save WMAL/57bb7a700d8a7ba0c0a0d0d26d5afc8a to your computer and use it in GitHub Desktop.
This Bash script generates UUIDs and customizable Fake IDs, hashes them with MD5, SHA-256, and SHA-384, and supports output in JSON or colored plain text. It includes logging, dependency checks, error handling, and options for batch generation and custom Fake ID lengths.
#!/bin/bash
# =========================================
# Ultimate Bash ID Generator
# =========================================
#
# Version: 2.4
# Script written by Warith Al Maawali
#
# Discord channel: https://discord.gg/KEFErEx
# Twitter: http://twitter.com/warith2020
# Linkedin: http://www.linkedin.com/in/warith1977
# Website: http://www.digi77.com
# (c) 2024
#
# Description:
# This script generates UUIDs and Fake IDs, hashes them using multiple algorithms,
# encodes them in Base64, and outputs the results in either JSON or plain text.
# It supports generating multiple IDs, customizable Fake ID lengths, logging, and
# robust error handling.
#
# This software is dual-licensed:
#
# Personal, non-commercial use: Apache License 2.0
# Commercial, corporate, or organizational use: Separate commercial license required.
# Contact me for licensing inquiries.
#
# Usage: ./id-generator.sh [options]
#
# Usage Examples:
# Generate a single ID:
# ./id-generator.sh
# Generate five IDs:
# ./id-generator.sh -n 5
# Generate three IDs with JSON output:
# ./id-generator.sh -n 3 -o json
# Specify Fake ID length:
# ./id-generator.sh -n 2 -f 128
# Display Help:
# ./id-generator.sh -h
# =========================================
# Constants
DEFAULT_NUM_IDS=1
DEFAULT_OUTPUT_FORMAT="text"
DEFAULT_FAKE_ID_LENGTH=128
DEFAULT_LOG_FILE="id_generator.log"
OUTPUT_FILE=""
JSON_OUTPUT=()
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Dependencies
REQUIRED_COMMANDS=("uuidgen" "md5sum" "sha256sum" "sha384sum" "base64" "jq" "tr" "head" "awk")
# Functions
# Display usage instructions
usage() {
cat <<EOF
Usage: $0 [options]
Options:
-n NUM Number of UUIDs and Fake IDs to generate (default: 1)
-o FORMAT Output format: text (default) or json
-f LENGTH Length of the Fake ID (default: 128)
-l LOGFILE Log file path (default: ./id_generator.log)
-h, --help Display this help message
Examples:
Generate a single ID:
$0
Generate five IDs:
$0 -n 5
Generate three IDs with JSON output:
$0 -n 3 -o json
Specify Fake ID length:
$0 -n 2 -f 128
Specify a custom log file:
$0 -l /var/log/id_generator.log
EOF
exit 1
}
# Log messages with timestamp
log() {
local message="$1"
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" | tee -a "$LOG_FILE"
}
# Check and install dependencies
check_dependencies() {
for cmd in "${REQUIRED_COMMANDS[@]}"; do
if ! command -v "$cmd" &>/dev/null; then
log "Dependency '$cmd' not found. Attempting to install..."
case "$cmd" in
uuidgen)
sudo apt-get update && sudo apt-get install -y uuid-runtime || { log "Failed to install uuidgen."; exit 1; }
;;
jq)
sudo apt-get update && sudo apt-get install -y jq || { log "Failed to install jq."; exit 1; }
;;
*)
sudo apt-get update && sudo apt-get install -y "$cmd" || { log "Failed to install $cmd."; exit 1; }
;;
esac
log "Installed '$cmd' successfully."
fi
done
}
# Function to hash using different algorithms
hash_id() {
local id="$1"
local algorithm="$2"
case "$algorithm" in
md5)
echo -n "$id" | md5sum | awk '{print $1}'
;;
sha256)
echo -n "$id" | sha256sum | awk '{print $1}'
;;
sha384)
echo -n "$id" | sha384sum | awk '{print $1}'
;;
*)
echo "Unsupported hash algorithm: $algorithm" >&2
return 1
;;
esac
}
# Generate a single UUID and Fake ID, returning JSON data
generate_single_id() {
local fake_id_length="$1"
local uuid
uuid=$(uuidgen)
if [ -z "$uuid" ]; then
log "Failed to generate UUID."
return 1
fi
local fake_id
fake_id=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | tr -d '\n' | head -c "$fake_id_length")
if [ ${#fake_id} -ne "$fake_id_length" ]; then
log "Failed to generate Fake ID of specified length."
return 1
fi
local uuid_md5 uuid_sha256 uuid_sha384 uuid_base64
uuid_md5=$(hash_id "$uuid" "md5")
uuid_sha256=$(hash_id "$uuid" "sha256")
uuid_sha384=$(hash_id "$uuid" "sha384")
uuid_base64=$(echo -n "$uuid" | base64)
local fake_id_md5 fake_id_sha256 fake_id_sha384 fake_id_base64
fake_id_md5=$(hash_id "$fake_id" "md5")
fake_id_sha256=$(hash_id "$fake_id" "sha256")
fake_id_sha384=$(hash_id "$fake_id" "sha384")
fake_id_base64=$(echo -n "$fake_id" | base64)
# Generate JSON object for this ID
jq -n \
--arg uuid "$uuid" \
--arg uuid_md5 "$uuid_md5" \
--arg uuid_sha256 "$uuid_sha256" \
--arg uuid_sha384 "$uuid_sha384" \
--arg uuid_base64 "$uuid_base64" \
--arg fake_id "$fake_id" \
--arg fake_id_md5 "$fake_id_md5" \
--arg fake_id_sha256 "$fake_id_sha256" \
--arg fake_id_sha384 "$fake_id_sha384" \
--arg fake_id_base64 "$fake_id_base64" \
'{
uuid: {
original: $uuid,
md5: $uuid_md5,
sha256: $uuid_sha256,
sha384: $uuid_sha384,
base64: $uuid_base64
},
fake_id: {
original: $fake_id,
md5: $fake_id_md5,
sha256: $fake_id_sha256,
sha384: $fake_id_sha384,
base64: $fake_id_base64
}
}'
}
# Generate multiple IDs
generateIDs() {
local count="$1"
local fake_id_length="$2"
local i=1
local json_array=()
local text_output=""
log "Starting generation of $count ID(s)."
while [ "$i" -le "$count" ]; do
log "Generating ID $i..."
local id_json
id_json=$(generate_single_id "$fake_id_length")
if [ $? -eq 0 ]; then
json_array+=("$id_json")
log "Successfully generated ID $i."
# Parse JSON to extract fields for terminal output
local uuid original_uuid uuid_md5 uuid_sha256 uuid_sha384 uuid_base64
local fake_id original_fake_id fake_id_md5 fake_id_sha256 fake_id_sha384 fake_id_base64
original_uuid=$(echo "$id_json" | jq -r '.uuid.original')
uuid_md5=$(echo "$id_json" | jq -r '.uuid.md5')
uuid_sha256=$(echo "$id_json" | jq -r '.uuid.sha256')
uuid_sha384=$(echo "$id_json" | jq -r '.uuid.sha384')
uuid_base64=$(echo "$id_json" | jq -r '.uuid.base64')
original_fake_id=$(echo "$id_json" | jq -r '.fake_id.original')
fake_id_md5=$(echo "$id_json" | jq -r '.fake_id.md5')
fake_id_sha256=$(echo "$id_json" | jq -r '.fake_id.sha256')
fake_id_sha384=$(echo "$id_json" | jq -r '.fake_id.sha384')
fake_id_base64=$(echo "$id_json" | jq -r '.fake_id.base64')
# Terminal Output
echo -e "${BLUE}UUID Generation:${NC}"
echo -e " ${GREEN}Original UUID:${NC} $original_uuid"
echo -e " ${GREEN}UUID MD5:${NC} $uuid_md5"
echo -e " ${GREEN}UUID SHA-256:${NC} $uuid_sha256"
echo -e " ${GREEN}UUID SHA-384:${NC} $uuid_sha384"
echo -e " ${GREEN}UUID Base64:${NC} $uuid_base64"
echo ""
echo -e "${YELLOW}Fake ID Generation:${NC}"
echo -e " ${GREEN}Original Fake ID:${NC} $original_fake_id"
echo -e " ${GREEN}Fake ID MD5:${NC} $fake_id_md5"
echo -e " ${GREEN}Fake ID SHA-256:${NC} $fake_id_sha256"
echo -e " ${GREEN}Fake ID SHA-384:${NC} $fake_id_sha384"
echo -e " ${GREEN}Fake ID Base64:${NC} $fake_id_base64"
echo -e "${NC}----------------------------------------${NC}"
# Append to text_output for file output
text_output+="UUID Generation:\n"
text_output+=" Original UUID: $original_uuid\n"
text_output+=" UUID MD5: $uuid_md5\n"
text_output+=" UUID SHA-256: $uuid_sha256\n"
text_output+=" UUID SHA-384: $uuid_sha384\n"
text_output+=" UUID Base64: $uuid_base64\n\n"
text_output+="Fake ID Generation:\n"
text_output+=" Original Fake ID: $original_fake_id\n"
text_output+=" Fake ID MD5: $fake_id_md5\n"
text_output+=" Fake ID SHA-256: $fake_id_sha256\n"
text_output+=" Fake ID SHA-384: $fake_id_sha384\n"
text_output+=" Fake ID Base64: $fake_id_base64\n"
text_output+="----------------------------------------\n\n"
else
echo -e "${RED}ID $i: FAILED${NC}"
log "Failed to generate ID $i."
fi
i=$((i + 1))
done
# Handle file output
if [ "$OUTPUT_FORMAT" == "json" ]; then
# Combine all JSON objects into a JSON array
printf '%s\n' "${json_array[@]}" | jq -s '.' > "$OUTPUT_FILE"
echo -e "\n${BLUE}JSON results saved to $OUTPUT_FILE${NC}"
log "JSON results saved to $OUTPUT_FILE."
elif [ "$OUTPUT_FORMAT" == "text" ]; then
# Write text output to file
echo -e "$text_output" > "$OUTPUT_FILE"
echo -e "\n${BLUE}Text results saved to $OUTPUT_FILE${NC}"
log "Text results saved to $OUTPUT_FILE."
fi
}
# Parse command-line arguments
parse_args() {
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-n)
NUM_IDS="$2"
if ! [[ "$NUM_IDS" =~ ^[0-9]+$ ]]; then
echo -e "${RED}ERROR: -n requires a numeric argument.${NC}"
usage
fi
shift # past argument
shift # past value
;;
-o)
OUTPUT_FORMAT="$2"
if [[ "$OUTPUT_FORMAT" != "text" && "$OUTPUT_FORMAT" != "json" ]]; then
echo -e "${RED}ERROR: -o accepts 'text' or 'json' only.${NC}"
usage
fi
OUTPUT_FILE="output.$OUTPUT_FORMAT"
shift
shift
;;
-f)
FAKE_ID_LENGTH="$2"
if ! [[ "$FAKE_ID_LENGTH" =~ ^[0-9]+$ ]]; then
echo -e "${RED}ERROR: -f requires a numeric argument.${NC}"
usage
fi
shift
shift
;;
-l)
LOG_FILE="$2"
shift
shift
;;
-h|--help)
usage
;;
*)
echo -e "${RED}ERROR: Unknown option: $1${NC}"
usage
;;
esac
done
# Set default values if not set
NUM_IDS=${NUM_IDS:-$DEFAULT_NUM_IDS}
OUTPUT_FORMAT=${OUTPUT_FORMAT:-$DEFAULT_OUTPUT_FORMAT}
FAKE_ID_LENGTH=${FAKE_ID_LENGTH:-$DEFAULT_FAKE_ID_LENGTH}
LOG_FILE=${LOG_FILE:-$DEFAULT_LOG_FILE}
# Set default output file if not specified
if [ -z "$OUTPUT_FILE" ]; then
OUTPUT_FILE="output.$OUTPUT_FORMAT"
fi
}
# Main Execution
# Initialize default values
NUM_IDS=$DEFAULT_NUM_IDS
OUTPUT_FORMAT=$DEFAULT_OUTPUT_FORMAT
FAKE_ID_LENGTH=$DEFAULT_FAKE_ID_LENGTH
LOG_FILE=$DEFAULT_LOG_FILE
# Parse arguments
parse_args "$@"
# Check dependencies
check_dependencies
# Generate IDs and capture output
generateIDs "$NUM_IDS" "$FAKE_ID_LENGTH"
# Save results to file is handled within generateIDs
log "ID generation process completed."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment