16to62.sh
· 753 B · Bash
Surowy
#!/bin/sh
set -eu
# Converts a base-16 hash argument to base-62, using only the shell
# and utilities specified by POSIX.1-2024.
hash16="$(printf "%s" "$1" | tr a-f A-F)"
printf "obase=62; ibase=16; %s\n" "$hash16" | bc | {
# `bc` prints a sequence of ordinal values split across multiple
# lines using backslash as a continuation character. `read` will,
# by default, assemble this back into a single line for us.
read ordinals
hash62=
for ord in $ordinals; do
[ -n "$ord" ] || break
ord="${ord#0}" # strip leading 0 so it's not parsed as octal
offset=61
[ $ord -lt 36 ] && offset=55
[ $ord -lt 10 ] && offset=48
byte=$((ord+offset))
hash62="$hash62\\$(printf "%03o" $byte)"
done
printf "$hash62\n"
}
| 1 | #!/bin/sh |
| 2 | set -eu |
| 3 | |
| 4 | # Converts a base-16 hash argument to base-62, using only the shell |
| 5 | # and utilities specified by POSIX.1-2024. |
| 6 | |
| 7 | hash16="$(printf "%s" "$1" | tr a-f A-F)" |
| 8 | printf "obase=62; ibase=16; %s\n" "$hash16" | bc | { |
| 9 | # `bc` prints a sequence of ordinal values split across multiple |
| 10 | # lines using backslash as a continuation character. `read` will, |
| 11 | # by default, assemble this back into a single line for us. |
| 12 | read ordinals |
| 13 | hash62= |
| 14 | |
| 15 | for ord in $ordinals; do |
| 16 | [ -n "$ord" ] || break |
| 17 | ord="${ord#0}" # strip leading 0 so it's not parsed as octal |
| 18 | |
| 19 | offset=61 |
| 20 | [ $ord -lt 36 ] && offset=55 |
| 21 | [ $ord -lt 10 ] && offset=48 |
| 22 | |
| 23 | byte=$((ord+offset)) |
| 24 | hash62="$hash62\\$(printf "%03o" $byte)" |
| 25 | done |
| 26 | |
| 27 | printf "$hash62\n" |
| 28 | } |
| 29 |