-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpg-sign-file.zsh
executable file
·78 lines (71 loc) · 1.78 KB
/
gpg-sign-file.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env bash
# Parse arguments
while getopts "f:o:k:s:h" opt; do
case $opt in
f)
file=$OPTARG
;;
o)
output=$OPTARG
;;
k)
key=$OPTARG
;;
s)
sign=$OPTARG
;;
h)
echo "Usage: gpg-sign-file.zsh -f <file> -o <output> -k <key>"
echo "-f: The file to sign"
echo "-o: The output file / When left blank, the signed file will be saved as <file>.sig"
echo "-k: The key (keyID) to use for signing / When left blank, all keys will be listed and you will be prompted to choose one"
echo "-s: Signing option, either clearsign or detach-sign"
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
# Check if the file exists
if [ ! -f "$file" ]; then
echo "File not found: $file"
exit 1
fi
# List keys if no key is provided
if [ -z "$key" ]; then
gpg --list-secret-keys
echo "Please enter the key to use for signing:"
read key
fi
# Set the output file
if [ -z "$output" ]; then
output="$file.sig"
fi
# Set the sign option
if [ -z "$sign" ]; then
sign="detach-sign"
fi
# Perform the signing
if [ "$sign" = "clearsign" ]; then
# Clear-sign the file (includes file contents in output)
gpg --clearsign \
--local-user "$key" \
--output "$output" \
"$file"
else
# Create detached signature (default)
gpg --detach-sign \
--local-user "$key" \
--output "$output" \
"$file"
fi
# Check if signing was successful
if [ $? -eq 0 ]; then
echo "File signed successfully"
echo "Signature saved to: $output"
else
echo "Signing failed"
exit 1
fi