Skip to content

Instantly share code, notes, and snippets.

@zatricky
Last active December 23, 2025 13:59
Show Gist options
  • Select an option

  • Save zatricky/7952afebb9a47d297062408ae407e806 to your computer and use it in GitHub Desktop.

Select an option

Save zatricky/7952afebb9a47d297062408ae407e806 to your computer and use it in GitHub Desktop.
Stationeers hash function for bash using rhash only
# This is free and unencumbered software released into the public domain.
# For more information, please refer to <https://unlicense.org>
stationeers.hash() {
# Get CRC32 as unsigned 32-bit hex value
local crc32_hex=$(printf "%s" "$1" | rhash --crc32 - -p '%C')
# Convert hex to decimal unsigned
local crc32_dec=$((16#$crc32_hex))
# Convert to signed 32-bit decimal (matches Python's logic)
local signed
if [ $crc32_dec -ge $((0x80000000)) ]; then
signed=$((crc32_dec - 0x100000000))
else
signed=$crc32_dec
fi
# Generate hex representation
if [ "$signed" -lt 0 ]; then
# Negative: use 64-bit two's complement representation
# For negative 32-bit values, extend sign to 64-bit
# The CRC is already the lower 32 bits, just need FFFFFFFF prefix
echo "\$FFFFFFFF$crc32_hex"
else
# Positive: use compact format (uppercase)
echo "\$$crc32_hex"
fi
echo "$signed"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment