Skip to content

Commit 93f9d4c

Browse files
authored
Centralise load and save of password into functions #141
Read and write password with new `load_password()` and `save_password()` functions. This makes it easier for others to override password handling with minimal changes.
1 parent ac99a93 commit 93f9d4c

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

transcrypt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ readonly DEFAULT_CIPHER='aes-256-cbc'
2323

2424
##### FUNCTIONS
2525

26+
# load encryption password
27+
# by default is stored in git config, modify this function to move elsewhere
28+
load_password() {
29+
local password
30+
password=$(git config --get --local transcrypt.password)
31+
echo "$password"
32+
}
33+
34+
# save encryption password
35+
# by default is stored in git config, modify this function to move elsewhere
36+
save_password() {
37+
local password=$1
38+
git config transcrypt.password "$password"
39+
}
40+
2641
# print a canonicalized absolute pathname
2742
realpath() {
2843
local path=$1
@@ -136,7 +151,7 @@ git_clean() {
136151
cat "$tempfile"
137152
else
138153
cipher=$(git config --get --local transcrypt.cipher)
139-
password=$(git config --get --local transcrypt.password)
154+
password=$(load_password)
140155
openssl_path=$(git config --get --local transcrypt.openssl-path)
141156
salt=$("${openssl_path}" dgst -hmac "${filename}:${password}" -sha256 "$tempfile" | tr -d '\r\n' | tail -c16)
142157

@@ -160,7 +175,7 @@ git_smudge() {
160175
tempfile=$(mktemp 2>/dev/null || mktemp -t tmp)
161176
trap 'rm -f "$tempfile"' EXIT
162177
cipher=$(git config --get --local transcrypt.cipher)
163-
password=$(git config --get --local transcrypt.password)
178+
password=$(load_password)
164179
openssl_path=$(git config --get --local transcrypt.openssl-path)
165180
tee "$tempfile" | ENC_PASS=$password "$openssl_path" enc -d "-${cipher}" -md MD5 -pass env:ENC_PASS -a 2>/dev/null || cat "$tempfile"
166181
}
@@ -172,7 +187,7 @@ git_textconv() {
172187
return
173188
fi
174189
cipher=$(git config --get --local transcrypt.cipher)
175-
password=$(git config --get --local transcrypt.password)
190+
password=$(load_password)
176191
openssl_path=$(git config --get --local transcrypt.openssl-path)
177192
ENC_PASS=$password "$openssl_path" enc -d "-${cipher}" -md MD5 -pass env:ENC_PASS -a -in "$filename" 2>/dev/null || cat "$filename"
178193
}
@@ -511,7 +526,7 @@ save_configuration() {
511526
# write the encryption info
512527
git config transcrypt.version "$VERSION"
513528
git config transcrypt.cipher "$cipher"
514-
git config transcrypt.password "$password"
529+
save_password "$password"
515530
git config transcrypt.openssl-path "$openssl_path"
516531

517532
# write the filter settings. Sorry for the horrific quote escaping below...
@@ -538,7 +553,7 @@ display_configuration() {
538553
local current_cipher
539554
current_cipher=$(git config --get --local transcrypt.cipher)
540555
local current_password
541-
current_password=$(git config --get --local transcrypt.password)
556+
current_password=$(load_password)
542557
local escaped_password=${current_password//\'/\'\\\'\'}
543558

544559
printf 'The current repository was configured using transcrypt version %s\n' "$CONFIGURED"
@@ -743,7 +758,7 @@ upgrade_transcrypt() {
743758

744759
# Keep current cipher and password
745760
cipher=$(git config --get --local transcrypt.cipher)
746-
password=$(git config --get --local transcrypt.password)
761+
password=$(load_password)
747762
# Keep current openssl-path, or set to default if no existing value
748763
openssl_path=$(git config --get --local transcrypt.openssl-path 2>/dev/null || printf '%s' "$openssl_path")
749764

@@ -822,7 +837,7 @@ export_gpg() {
822837
local current_cipher
823838
current_cipher=$(git config --get --local transcrypt.cipher)
824839
local current_password
825-
current_password=$(git config --get --local transcrypt.password)
840+
current_password=$(load_password)
826841
mkdir -p "${CRYPT_DIR}"
827842

828843
local gpg_encrypt_cmd="gpg --batch --recipient $gpg_recipient --trust-model always --yes --armor --quiet --encrypt -"

0 commit comments

Comments
 (0)