diff --git a/utils/scripts/hash-colorize.sh b/utils/scripts/hash-colorize.sh new file mode 100644 index 0000000..363d620 --- /dev/null +++ b/utils/scripts/hash-colorize.sh @@ -0,0 +1,41 @@ +#!/bin/sh +# env +# Written in 2021 by Lucas +# CC0 1.0 Universal/Public domain - No rights reserved +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any +# warranty. You should have received a copy of the CC0 Public Domain +# Dedication along with this software. If not, see +# . + +djb2() +{ + _djb2_s=$1 _djb2_h=${2:-5381} + while [ ${#_djb2_s} -gt 0 ]; do + _djb2_c=$(printf "%u" "'${_djb2_s%${_djb2_s#?}}") + _djb2_s=${_djb2_s#?} + _djb2_h=$(( ((($_djb2_h << 5) + $_djb2_h) ^ $_djb2_c) & 65535 )) + done + printf "%u\n" $_djb2_h +} + +hash_colorize() +{ + _h= + while :; do + _h=$(djb2 "$1" $_h) + # removes bias in number selection. Taken from + # arc4random_uniform source code. + [ $_h -ge 4 ] && break + done + _h=$(( $_h % 12 )) + tput setaf $(( $_h < 6 ? 1 + $_h : 9 + $_h - 6 )) 0 0 +} + +for a; do + hash_colorize "$a" + printf "%s\n" "$a" + tput sgr0 +done