Skip to content

Commit 20263c8

Browse files
committed
Rebase my patch onto main in 2022
1 parent fdf81c5 commit 20263c8

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

transcrypt

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,39 +120,60 @@ die() {
120120
# then use the last 16 bytes of that HMAC for the file's unique salt.
121121

122122
git_clean() {
123+
124+
# The clean script encrypts files before git sends them to the remote.
125+
# Note the "Salted" check is part of openssl and not anything we do here.
126+
# It allows anyone (including us) to check if a file was already encrypted
127+
# but this does compromise the encrypted stream of data (which starts on
128+
# the 17th byte).
129+
# References: https://crypto.stackexchange.com/questions/8776/what-is-u2fsdgvkx1
123130
filename=$1
124131
# ignore empty files
125132
if [[ ! -s $filename ]]; then
126133
return
127134
fi
128135
# cache STDIN to test if it's already encrypted
136+
# First, create the tempfile, then
137+
# set a trap to remove the tempfile when we exit or if anything goes wrong
138+
# finally write the stdin of this script to the tempfile
129139
tempfile=$(mktemp 2>/dev/null || mktemp -t tmp)
130140
trap 'rm -f "$tempfile"' EXIT
131141
tee "$tempfile" &>/dev/null
132142
# the first bytes of an encrypted file are always "Salted" in Base64
133143
# The `head + LC_ALL=C tr` command handles binary data in old and new Bash (#116)
144+
# this is an openssl standard. The actual encrypted stream starts on the 17th byte.
134145
firstbytes=$(head -c8 "$tempfile" | LC_ALL=C tr -d '\0')
135146
if [[ $firstbytes == "U2FsdGVk" ]]; then
147+
# The file is already encrypted, so just pass it back
136148
cat "$tempfile"
137149
else
138150
cipher=$(git config --get --local transcrypt.cipher)
139151
password=$(git config --get --local transcrypt.password)
140152
openssl_path=$(git config --get --local transcrypt.openssl-path)
141-
salt=$("${openssl_path}" dgst -hmac "${filename}:${password}" -sha256 "$tempfile" | tr -d '\r\n' | tail -c16)
142-
ENC_PASS=$password "$openssl_path" enc "-${cipher}" -md MD5 -pass env:ENC_PASS -e -a -S "$salt" -in "$tempfile"
153+
#salt=$("${openssl_path}" dgst -hmac "${filename}:${password}" -sha256 "$tempfile" | tr -d '\r\n' | tail -c16)
154+
#ENC_PASS=$password "$openssl_path" enc "-${cipher}" -md MD5 -pass env:ENC_PASS -e -a -S "$salt" -in "$tempfile"
155+
# NOTE: salt must be 16 bytes, its openssl standard
156+
salt=$("$openssl_path" dgst -hmac "${filename}:${password}" -sha512 "$filename" | tr -d '\r\n' | tail -c 16)
157+
ENC_PASS=$password "$openssl_path" enc "-${cipher}" -md SHA512 -pass env:ENC_PASS -pbkdf2 -e -a -S "$salt" -in "$tempfile"
143158
fi
144159
}
145160

146161
git_smudge() {
162+
# The smudge script decrypts files when they are checked out by an authenticated repository.
163+
# the file contents are passed via stdin
147164
tempfile=$(mktemp 2>/dev/null || mktemp -t tmp)
148165
trap 'rm -f "$tempfile"' EXIT
149166
cipher=$(git config --get --local transcrypt.cipher)
150167
password=$(git config --get --local transcrypt.password)
151168
openssl_path=$(git config --get --local transcrypt.openssl-path)
152-
tee "$tempfile" | ENC_PASS=$password "$openssl_path" enc "-${cipher}" -md MD5 -pass env:ENC_PASS -d -a 2>/dev/null || cat "$tempfile"
169+
#tee "$tempfile" | ENC_PASS=$password "$openssl_path" enc "-${cipher}" -md MD5 -pass env:ENC_PASS -d -a 2>/dev/null || cat "$tempfile"
170+
tee "$tempfile" | ENC_PASS=$password "$openssl_path" enc "-${cipher}" -md SHA512 -pass env:ENC_PASS -pbkdf2 -d -a 2>/dev/null || cat "$tempfile"
153171
}
154172

155173
git_textconv() {
174+
# The textconv script allows users to see git diffs in plaintext.
175+
# It does this by decrypting the encrypted git globs into plain text before
176+
# passing them to the diff command.
156177
filename=$1
157178
# ignore empty files
158179
if [[ ! -s $filename ]]; then
@@ -161,7 +182,8 @@ git_textconv() {
161182
cipher=$(git config --get --local transcrypt.cipher)
162183
password=$(git config --get --local transcrypt.password)
163184
openssl_path=$(git config --get --local transcrypt.openssl-path)
164-
ENC_PASS=$password "$openssl_path" enc "-${cipher}" -md MD5 -pass env:ENC_PASS -d -a -in "$filename" 2>/dev/null || cat "$filename"
185+
#ENC_PASS=$password "$openssl_path" enc "-${cipher}" -md MD5 -pass env:ENC_PASS -d -a -in "$filename" 2>/dev/null || cat "$filename"
186+
ENC_PASS=$password "$openssl_path" enc "-${cipher}" -md SHA512 -pass env:ENC_PASS -pbkdf2 -d -a -in "$filename" 2>/dev/null || cat "$filename"
165187
}
166188

167189
# shellcheck disable=SC2005,SC2002,SC2181

0 commit comments

Comments
 (0)