Skip to content

Instantly share code, notes, and snippets.

@velenux
Last active May 22, 2017 09:04
Show Gist options
  • Save velenux/a4aaeff7bda9e7c5c886 to your computer and use it in GitHub Desktop.
Save velenux/a4aaeff7bda9e7c5c886 to your computer and use it in GitHub Desktop.
Quickly create a visual representation of a hash checksum in bash using Unicode color-coded characters
#!/bin/bash
# outputs a visual hash for the system is running on:
# useful to identify which system you are running your commands on
# first two bytes for the unicode sequence, by default we use 28
# since the braille character set is fully compliant and easy to identify
# the characters will be: u+${BASE_UNICODE}${first_hex}${second_hex}
# good values are: 14, 22, 25, 28, 2F, 34, A5, FB, 106, 131
BASE_UNICODE=14
# create a temp file for the system info
FILE_TMP=$(mktemp)
# gather some info to identify the system
hostname -f > "$FILE_TMP"
ip link show 2>/dev/null | grep link | awk {'print $2'} | sort >> "$FILE_TMP"
lspci 2>/dev/null | sort >> "$FILE_TMP"
grep bogomips /proc/cpuinfo | head -n1 >> "$FILE_TMP"
find /sys -print | sort >> "$FILE_TMP"
head -n4096 /dev/sda >> "$FILE_TMP"
date >> "$FILE_TMP"
# calculate a checksum of the system info
SUM=$(md5sum "$FILE_TMP" | cut -b1-32)
#SUM=$(sha256sum "$FILE_TMP" | cut -b1-64)
# we don't need the info anymore
rm -f "$FILE_TMP"
# create an empty visual sum to start with
VISUAL_SUM=""
# iterate over the checksum
for (( i=0; i<${#SUM}; i++ )); do
first=${SUM:$i:1} # get the first hex value
(( i++ ))
second=${SUM:$i:1} # get the second hex value
if [ -z "$second" ]; then
second=0 # default value
fi
(( i++ ))
color=${SUM:$i:1} # get the color shade hex value
if [ -z "$color" ]; then
CL="\[\e[90;40m\]" # default value
else
# if we have data, we convert it to a bash color code
case $color in
0)
CL="\[\e[30;39m\]"
;;
1)
CL="\[\e[31;40m\]"
;;
2)
CL="\[\e[32;40m\]"
;;
3)
CL="\[\e[33;40m\]"
;;
4)
CL="\[\e[34;40m\]"
;;
5)
CL="\[\e[35;40m\]"
;;
6)
CL="\[\e[36;40m\]"
;;
7)
CL="\[\e[37;40m\]"
;;
8)
CL="\[\e[90;40m\]"
;;
9)
CL="\[\e[91;40m\]"
;;
[aA])
CL="\[\e[92;40m\]"
;;
[bB])
CL="\[\e[93;40m\]"
;;
[cC])
CL="\[\e[94;40m\]"
;;
[dD])
CL="\[\e[95;40m\]"
;;
[eE])
CL="\[\e[96;40m\]"
;;
[fF])
CL="\[\e[97;40m\]"
;;
*)
CL="\[\e[0m\]"
;;
esac
fi
# queue up the current character
VISUAL_SUM="${VISUAL_SUM}${CL}$(echo -e "\u${BASE_UNICODE}${first}${second}")"
done
echo $VISUAL_SUM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment