color-hash.ksh: add SHA-[12] family

This commit is contained in:
Lucas Gabriel Vuotto 2024-07-13 13:56:18 +00:00
parent f2d8419d1b
commit d2e60b08ce

View File

@ -43,6 +43,37 @@ function _color_hash_fnv1a
print -nr -- $(( ($h >> 16) ^ ($h & 0xffff) )) print -nr -- $(( ($h >> 16) ^ ($h & 0xffff) ))
} }
function _color_hash_sha
{
local c h s p
p=$1
shift
s=$(printf "%08x%s" "${2:-0}" "$1")
h=$("$p" -qs "$s")
c=$((0x${h#${h%?}}))
while (( $c > 0 )); do
h=${h#??}
c=$(($c - 1))
done
h=$((0x${h%${h#????}}))
print -nr -- $h
}
function _color_hash_sha1
{
_color_hash_sha sha1 "$@"
}
function _color_hash_sha256
{
_color_hash_sha sha256 "$@"
}
function _color_hash_sha512
{
_color_hash_sha sha512 "$@"
}
function color_hash function color_hash
{ {
local bright impl flag h n s local bright impl flag h n s
@ -52,7 +83,7 @@ function color_hash
while getopts bi: flag; do while getopts bi: flag; do
case $flag in case $flag in
b) bright=true ;; b) bright=true ;;
i) if [[ $OPTARG != @(djb2|fnv1|fnv1a) ]]; then i) if [[ $OPTARG != @(djb2|fnv1|fnv1a|sha1|sha256|sha512) ]]; then
return 1 return 1
fi fi
impl=_color_hash_$OPTARG impl=_color_hash_$OPTARG