sam revidoval tento gist 1 month ago. Přejít na revizi
1 file changed, 28 insertions
16to62.sh(vytvořil soubor)
| @@ -0,0 +1,28 @@ | |||
| 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 | + | } | |
Novější
Starší