-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdecode.sh
75 lines (60 loc) · 1.53 KB
/
decode.sh
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
#!/bin/bash
EXCLUDE_DIRS=("node_modules" ".idea" ".vagrant")
EXCLUDE_FILES=("Vagrantfile" "Dockerfile" ".gitignore" "decrypt.sh" "encrypt.sh")
should_exclude() {
local item="$1"
local basename=$(basename "$item")
for dir in "${EXCLUDE_DIRS[@]}"; do
if [[ "$basename" == "$dir" ]]; then
return 0
fi
done
for file in "${EXCLUDE_FILES[@]}"; do
if [[ "$basename" == "$file" ]]; then
return 0
fi
done
return 1
}
should_decrypt() {
local file="$1"
local extension="${file##*.}"
case "$extension" in
ts|json|js)
return 0
;;
*)
return 1
;;
esac
}
decrypt_file() {
local file="$1"
local first_char="${file:0:1}"
local ascii=$(printf "%d" "'$first_char")
echo "解密文件: $file"
temp_file="${file}.tmp"
xxd -p "$file" | tr -d '\n' | sed 's/\(..\)/\1 /g' | while read -n 3 hex; do
if [ ! -z "$hex" ]; then
printf "%02x" "$((0x$hex ^ ascii))"
fi
done | xxd -r -p > "$temp_file"
mv "$temp_file" "$file"
}
process_directory() {
local dir="$1"
for item in "$dir"/*; do
if should_exclude "$item"; then
echo "跳过: $item"
continue
fi
if [[ -d "$item" ]]; then
process_directory "$item"
elif [[ -f "$item" ]] && should_decrypt "$item"; then
decrypt_file "$item"
fi
done
}
echo "开始解密..."
process_directory "."
echo "解密完成"