-
Notifications
You must be signed in to change notification settings - Fork 1
/
qrfile
217 lines (194 loc) · 6.16 KB
/
qrfile
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash
function ProgressBar {
let progress=(${1}*100/${2}*100)/100
let done=(${progress}*4)/10
let left=40-$done
done=$(printf "%${done}s")
left=$(printf "%${left}s")
printf "\r[${done// /#}${left// /-}] ${progress}%% | ${1}/${2}"
}
if [[ $@ == *"create"* ]]; then
if [[ $@ == *"-b"* ]] || [[ $@ == *"--base64"* ]] || [[ $@ == *"--base"* ]]; then
cipher=base64
elif [[ $@ == *"-a"* ]] || [[ $@ == *"--ascii85"* ]] || [[ $@ == *"--ascii"* ]]; then
cipher=ascii85
else
cipher=ascii85
fi
if ! [ -x "$(command -v split)" ]; then
echo Error: split is not installed
exit 1
fi
if ! [ -x "$(command -v qrencode)" ]; then
echo Error: qrencode is not installed
exit 1
fi
if ! [ -x "$(command -v $cipher)" ]; then
echo Error: $cipher is not installed, please install it fror your system
exit 1
fi
for path; do true; done
if [ ! -f "$path" ]; then
echo File \"$path\" does not exists!
exit 1
fi
file="$(basename "$path")"
cp -f "$path" "${file}_work"
if [ -d "qr_${file}" ]; then
while true; do
read -p "Dir \"qr_${file}\" already exist remove it? Y/N " yn
case $yn in
[Yy]* ) rm -rf "qr_${file}"; break;;
[Nn]* ) exit 130;;
* ) echo "Please answer Y or N.";;
esac
done
fi
printf "Creating ${cipher}... "
mkdir "${file}_${cipher}"
${cipher} -w 0 "${file}_work" > "${file}_${cipher}"/"$file"
printf "Done\n"
printf "Creating split... "
cd "${file}_${cipher}"
split -b 2953 -d "$file" "$file."
rm -rf "$file"
files=$(ls | wc -l)
printf "Done: Created $files files\n"
printf "Creating QR...\n"
n=1
for a in "$file".*
do
qrencode -8 -d 10 -m 0 -s 3 -o "$a".png < "$a"
rm -rf "$a"
ProgressBar $n $files
((n++))
done
printf " Done\n"
printf "Creating info file..."
echo name="\"${file}\"" > "${file}_info.txt"
echo cipher=$cipher >> "${file}_info.txt"
if [[ $@ == *"-256"* ]] || [[ $@ == *"--sha256"* ]]; then
hash=sha256
elif [[ $@ == *"-512"* ]] || [[ $@ == *"--sha512"* ]]; then
hash=sha512
else
qrencode -8 -d 10 -m 1 -o "${file}_info.png" < "${file}_info.txt"
printf " Done\n"
cd ..
cat "${file}_info.txt"
mv "${file}_${cipher}" "qr_${file}"
rm "${file}_work"
if [[ $@ == *"-t"* ]] || [[ $@ == *"--test"* ]]; then
bash qrfile convert "qr_${file}"
rm -rf "converted_${file}"
fi
exit 0
fi
if ! [ -x "$(command -v ${hash}sum)" ]; then
echo Error: ${hash}sum is not installed, skipping hashing...
else
echo hashcipher=$hash >> "${file}_info.txt"
echo hash=$(${hash}sum "../$file" | cut -d " " -f 1 ) >> "${file}_info.txt"
fi
qrencode -8 -d 10 -m 1 -o "${file}_info.png" < "${file}_info.txt"
printf " Done\n"
cat "${file}_info.txt"
cd ..
mv "${file}_${cipher}" "qr_${file}"
rm "${file}_work"
if [[ $@ == *"-t"* ]] || [[ $@ == *"--test"* ]]; then
bash qrfile convert "qr_${file}"
rm -rf "converted_${file}"
fi
elif [[ $1 == "convert" ]]; then
if [ ! -d "$2" ]; then
echo Dir \"$2\" does not exists!
exit 1
fi
dir="$(basename "$2")"
cp -Rf "$2" "${dir}_work"
if [ -d "${dir}_cqr" ]; then
while true; do
read -p "Dir \"${dir}_cqr\" already exist remove it? Y/N " yn
case $yn in
[Yy]* ) rm -rf "${dir}_cqr"; break;;
[Nn]* ) exit 130;;
* ) echo "Please answer Y or N.";;
esac
done
fi
printf "Converting QR... "
if ! [ -x "$(command -v zbarimg)" ]; then
echo Error: zbar is not installed
exit 1
fi
mkdir "${dir}_cqr"
cd "${dir}_work"
if [ -f "ascii85" ]; then
cipher=ascii85
name="$dir"
elif [ -f "base64" ]; then
cipher=base64
name="$dir"
else
if compgen -G "*info.png" > /dev/null; then
zbarimg -q *info.png > d_info.txt
rm *_info.png
sed -r 's/^QR-Code://' d_info.txt > info.txt
rm d_info.txt
source info.txt
else
echo There is no info.png file or cipher name file. Please create one and try again
rm -rf "../${dir}_cqr"
rm -rf "../${dir}_work"
exit 1
fi
fi
if ! [ -x "$(command -v $cipher)" ]; then
echo Error: $cipher is not installed, please install it for your system
exit 1
fi
files=$(ls *.png | wc -l)
printf "Finded $files files\n"
n=1
for a in "${a}"*.png
do
zbarimg -q "$a" > "../${dir}_cqr/${a}.txt"
ProgressBar $n $files
((n++))
done
printf " Done\n"
printf "Connecting files... "
cd "../${dir}_cqr"
cat * > "../${name}_${cipher}.txt"
sed -r 's/^QR-Code://' "../${name}_${cipher}.txt" > "../${name}_s${cipher}.txt"
cd ..
printf "Done\n"
printf "Decoding ${cipher}... "
${cipher} -d "${name}_s${cipher}.txt" > "converted_${name}"
printf "Done\n"
if [ -z ${hashcipher+x} ]; then
echo No hash sum, skipping...
else
printf "Checking ${hashcipher}sum... "
if ! [ -x "$(command -v ${hashcipher}sum)" ]; then
echo Error: ${hashcipher}sum is not installed, skipping hashing...
else
if [[ $(${hashcipher}sum "converted_${name}" | cut -d " " -f 1 ) == $hash ]]; then
echo Hash sum equal
else
echo Warning: Hash sume does not equal!
exitcode=1
fi
fi
fi
printf "Removing working dir and files... "
rm -rf "${dir}_cqr"
rm -rf "${name}_${cipher}.txt"
rm -rf "${name}_s${cipher}.txt"
rm -rf "${dir}_work"
printf "Done\n"
exit $exitcode
else
echo Unkown parametr
fi