banovski

Readable hashes

Mar 8th, 2020 (edited)
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.66 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # The script generates readable hashes, for example, "opepurosog". It
  4. # receives a path to a file as a positional parameter and outputs
  5. # a sequence consisting of "vowel + consonat" combinations.
  6.  
  7. vowels=(a e i o u)
  8. consonants=(b d f g h k l m n p r s t v z)
  9.  
  10. parity_flag=0
  11. carry=0
  12.  
  13. # Getting the checksum; it consists of digits only
  14. checksum=$(cksum $1 | cut -d " " -f 1)
  15. checksum_length=${#checksum}
  16. max_position_number=$((checksum_length - 1))
  17. # Turning the checksum string into an array
  18. array=($(echo $checksum | sed 's/./& /g'))
  19.  
  20. # Picking items from the array
  21. for position_number in $(seq 0 $max_position_number); do
  22.     # Identifying what type of position is dealt with: odd or even; if
  23.     # the position is odd, a vowel is picked and vice versa
  24.     parity_flag=$((position_number % 2))
  25.     if test $parity_flag -eq 0; then
  26.     position_contents=${array[position_number]}
  27.     # As there are only 5 items in the "vowels" array and the
  28.     # value of each checksum digit may range from 0 to 9, each
  29.     # index value is divided by two
  30.     vowel_array_index=$((position_contents / 2))
  31.     # A vowel from the "vowel" array is picked and output to
  32.     # stdout
  33.     echo -n ${vowels[$vowel_array_index]}
  34.     # The remainder is not wasted: it influences the choice of the
  35.     # following consonant letter
  36.     if test $position_contents -gt 4; then
  37.         carry=$((position_contents - 4))
  38.     fi
  39.     else
  40.     position_contents=${array[position_number]}
  41.     # The remainder is added to an index used to pick a consonant
  42.     consonant_array_index=$((position_contents + carry))
  43.     echo -n ${consonants[$consonant_array_index]}
  44.     carry=0
  45.     fi
  46.     parity_flag=0
  47. done
  48.  
  49. # A newline is added
  50. echo
Add Comment
Please, Sign In to add comment