-
-
Save inceabdullah/11f90488c29d6f1b6b535a789a813b7f to your computer and use it in GitHub Desktop.
Bash - generate random base64 string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Generate random base64 string using openssl | |
# Usage: generate_base64 [length] | |
# length: Optional, the length of the base64 string (default is 32) | |
generate_base64() { | |
if [[ "$1" == "--help" ]]; then | |
echo "Usage: generate_base64 [length]" | |
echo " length: Optional, the length of the base64 string (default is 32)" | |
return | |
fi | |
local length="${1:-32}" | |
openssl rand -base64 "$length" | |
} | |
# Check if an argument is provided (e.g., ./generate_base64.sh 64) | |
if [ $# -eq 1 ]; then | |
generate_base64 "$1" | |
else | |
# If no argument is provided, generate the default 32-character base64 string | |
generate_base64 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment