This repository has been archived on 2025-05-08. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
ykfde-nix/ykfde.sh
2022-12-29 20:24:55 +01:00

43 lines
1.4 KiB
Bash
Executable file

#!/usr/bin/env bash
KEY_LENGTH=512
SALT_LENGTH=16
ITERATIONS=1000000
rbtohex() {
( od -An -vtx1 | tr -d ' \n' )
}
hextorb() {
( tr '[:lower:]' '[:upper:]' | sed -e 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI'| xargs printf )
}
generate_salt() {
salt="$(dd if=/dev/random bs=1 count=$SALT_LENGTH 2>/dev/null | rbtohex)"
echo -ne "$salt\n$1"
}
derive_key() {
read -s -p "Password: " k_user
challenge="$(echo -n $1 | openssl dgst -binary -sha512 | rbtohex)"
response="$(ykchalresp -$3 -x $challenge 2>/dev/null)"
echo -n "$k_user" | pbkdf2-sha512 $(($KEY_LENGTH / 8)) $2 "$response"
}
if [[ "$1" = "generate-salt" ]] && ( [[ $# -eq 1 ]] || ( [[ $# -eq 2 ]] && [[ "$2" =~ ^[0-9]+$ ]] ) ); then
generate_salt "${2:-$ITERATIONS}"
elif [[ "$1" = "derive-key" ]] && [[ $# -eq 3 ]] && [[ -r "$2" ]] && [[ "$3" =~ ^[12]$ ]]; then
read -d '\n' salt iterations < "$2"
if ! [[ "$salt" =~ ^[0-9a-fA-F]+$ ]] || ! [[ "$iterations" =~ ^[0-9]+$ ]]; then
echo "Invalid salt file"
exit 2
fi
derive_key "$salt" "$iterations" "$3"
elif [[ "$1" = "time" ]] && ( [[ $# -eq 1 ]] || ( [[ $# -eq 2 ]] && [[ "$2" =~ ^[0-9]+$ ]] ) ); then
time echo -n "test password" | pbkdf2-sha512 $(($KEY_LENGTH / 8)) ${2:-$ITERATIONS} "a015def232c3f4318da97aacdec2107a19ced931" > /dev/null
else
echo "Usage: ykfde generate-salt [iterations]"
echo " ykfde derive-key <salt-file> <slot>"
echo " ykfde time [iterations]"
exit 1
fi