Skip to content

Instantly share code, notes, and snippets.

@mschmitt
Created October 24, 2020 10:28
Show Gist options
  • Save mschmitt/59770fc582b9b47c517c2a6a2573b4eb to your computer and use it in GitHub Desktop.
Save mschmitt/59770fc582b9b47c517c2a6a2573b4eb to your computer and use it in GitHub Desktop.
Programmatically changing a cryptsetup LUKS key
#!/bin/bash
luksobject='crypto.dat'
read -r -s -p 'Old passphrase: ' old
printf "\n"
read -r -s -p 'New passphrase: ' new
printf "\n"
read -r -s -p 'New passphrase again: ' new2
printf "\n"
if [[ "${new}" != "${new2}" ]]
then
printf "New passphrase mismatch. Aborting.\n"
exit 1
fi
if command -v pwscore >/dev/null && ! pwscore >/dev/null <<< "${new}"
then
printf "Weak passphrase. Aborting.\n"
exit 2
fi
printf "Passing control to cryptsetup:\n"
exec cryptsetup luksChangeKey --verbose \
--key-file <(tr -d '\n' <<< "${old}") "${luksobject}" <<< "${new}"
# Further Reading:
#
# pwscore(1)
# cryptsetup(8) "Notes on passphrase processing" (regarding the \n substition)
# bash(1) "Process Substitution" (regarding the <())
# bash(1) "Here Strings" (regarding the <<<)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment