Son aktivite 1 month ago

Revizyon 29cbdee16d6683ae6f4c7c9eced24c9a88084700

16to62.sh Ham
1#!/bin/sh
2set -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
7hash16="$(printf "%s" "$1" | tr a-f A-F)"
8printf "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