-
Notifications
You must be signed in to change notification settings - Fork 125
Description
What were you trying to do
Passphrase encrypt my identity file, but still also use it to encrypt
While I imagine a passphrase encrypted file we won't be able to use the read the public key from the comments, but ergonomically it would be nice to do. I thought perhaps a combination of --armor and adding the missing metadata might help.
# created: 2025-06-21T11:29:12Z
# public key: age1rnv4r73emdmpygaqaec8lcse4vs0wdhgzr04a7y7vsz9c8w6vp6qdg2cs4
-----BEGIN AGE ENCRYPTED FILE-----
ENCRYPTED KEY HERE
-----END AGE ENCRYPTED FILE-----
What happened
Error: identity file contains non-identity data on line 3
I imagine this is because it's expecting it to not be an encrypted key file.
Swapping and putting the comments after gives
Type passphrase for encrypted identity '/home/user/.config/age/key.txt'
Passphrase:
Error: invalid armor (non-whitespace characters after end marker)
[ Did rage not do what you expected? Could an error be more useful? ]
From reading the trackers, I can understand this isn't really the normal case for rage/age, but in the purpose of my scripts it would be great to read the users pinentry once, and then update a file.
The current work around would be to maintain 2x files, like ssh does, but it starts getting a bit custom, so was trying to make the ergonomics of my script nice, but protect against someone accidentally leaking or having no security around the key files.
Eg I can write key.txt and key.pub, and read the different keys that way, but would be nicer if there was an understood combined format.
An emulated way of having the 2x files is below. There might be a better way of doing the recipients, but it works in my program.
set -e
AGE_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/age"
PUB_FILE="$AGE_DIR/key.txt.pub"
PRIV_FILE="$AGE_DIR/key.txt"
TMP_KEY=$(mktemp)
trap 'rm -f "$TMP_KEY"' EXIT INT TERM HUP
mkdir -p "$AGE_DIR"
# Generate key and save to temp file
rage-keygen > "$TMP_KEY"
# Extract public key as single line (handles comment prefix)
grep '^# public key:' "$TMP_KEY" | sed 's/^# public key: //' > "$PUB_FILE"
# Encrypt private key with passphrase
rage -a -p -o "$PRIV_FILE" -e "$TMP_KEY"
echo "Public key saved to $PUB_FILE"
echo "Encrypted private key saved to $PRIV_FILE"