password.sh 579 B

12345678910111213141516171819202122232425262728293031
  1. #!/bin/sh
  2. encode() {
  3. if [ -z "$1" ]
  4. then
  5. salt=$(dd if=/dev/urandom count=9 bs=1 2>/dev/null | base64 | tr -d '=')
  6. else
  7. salt=$1
  8. fi
  9. cat - | argon2 "$salt" -id -t 3 -m 12 -p 1 -l 32 -e
  10. }
  11. verify() {
  12. stored=$1
  13. salt=$(echo "$stored" | cut -d '$' -f 5 | base64 -d)
  14. tested=$(cat - | encode "$salt")
  15. test "$stored" = "$tested"
  16. }
  17. if [ "$(basename "$0")" = 'password.sh' ]
  18. then
  19. # dependency verification
  20. for cmd in argon2 base64
  21. do
  22. command -v $cmd >/dev/null || { echo "$0: \`$cmd\` not found"; exit 2; }
  23. done
  24. stty -echo
  25. cat - | encode "$1" > password
  26. stty echo
  27. fi