-
Notifications
You must be signed in to change notification settings - Fork 9
/
checksums.sh
executable file
·1317 lines (1213 loc) · 44.9 KB
/
checksums.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Checksums v1.6.0
# Checksums (shell script version)
# minimum compatibility: native macOS checksum algorithms
# for the extended algorithms, e.g. SHA-3, please install the rhash CLI
# for Bencode support, please install the transmission CLI
LANG=en_US.UTF-8
export PATH=/usr/local/bin:$PATH
ACCOUNT=$(/usr/bin/id -un)
CURRENT_VERSION="1.60"
# set -x
# PS4=':$LINENO+'
# clipboard checksum parsing
cscn () {
case $1 in
( *[!0-9A-Fa-f]* | "" ) echo "false" ;;
( * )
case ${#1} in
( 4 ) echo "crc" ;;
( 5 ) echo "crc" ;;
( 6 ) echo "crc" ;;
( 7 ) echo "crc" ;;
( 8 ) echo "crc" ;;
( 9 ) echo "crc" ;;
( 10 ) echo "crc" ;;
( 32 ) echo "md" ;;
( 40 ) echo "sha" ;;
( 56 ) echo "224" ;;
( 64 ) echo "256" ;;
( 96 ) echo "384" ;;
( 128 ) echo "512" ;;
( * ) echo "false" ;;
esac
esac
}
cscx () {
case $1 in
( *[!0-9A-Fa-f]* | "" ) echo "false" ;;
( * )
case ${#1} in
( 4 ) echo "crc" ;;
( 5 ) echo "crc" ;;
( 6 ) echo "crc" ;;
( 7 ) echo "crc" ;;
( 8 ) echo "crc" ;;
( 9 ) echo "crc" ;;
( 10 ) echo "crc" ;;
( 32 ) echo "md" ;;
( 40 ) echo "sha" ;;
( 48 ) echo "tiger" ;;
( 56 ) echo "224" ;;
( 64 ) echo "256" ;;
( 96 ) echo "384" ;;
( 128 ) echo "512" ;;
( * ) echo "false" ;;
esac
esac
}
# notify function
notify () {
if [[ "$NOTESTATUS" == "osa" ]] ; then
/usr/bin/osascript &>/dev/null << EOT
tell application "System Events"
display notification "$2" with title "Checksums [" & "$ACCOUNT" & "]" subtitle "$1"
end tell
EOT
elif [[ "$NOTESTATUS" == "tn" ]] ; then
"$TERMNOTE_LOC/Contents/MacOS/terminal-notifier" \
-title "Checksums [$ACCOUNT]" \
-subtitle "$1" \
-message "$2" \
-appIcon "$ICON_LOC" \
>/dev/null
fi
}
# check for update
updater () {
echo "Checking for update..."
NEWEST_VERSION=$(/usr/bin/curl --silent https://api.github.com/repos/JayBrown/Checksums/releases/latest | /usr/bin/awk '/tag_name/ {print $2}' | xargs)
if [[ "$NEWEST_VERSION" == "" ]] ; then
NEWEST_VERSION="0"
fi
NEWEST_VERSION=${NEWEST_VERSION//,}
if (( $(echo "$NEWEST_VERSION > $CURRENT_VERSION" | /usr/bin/bc -l) )) ; then
notify "⚠️ Update available" "Checksums v$NEWEST_VERSION"
/usr/bin/open "https://github.com/JayBrown/Checksums/releases/latest"
fi
}
# check compatibility
MACOS2NO=$(/usr/bin/sw_vers -productVersion | /usr/bin/awk -F. '{print $2}')
if [[ "$MACOS2NO" -le 7 ]] ; then
echo "Error! Exiting…"
echo "Checksums needs at least OS X 10.8 (Mountain Lion)"
INFO=$(/usr/bin/osascript << EOT
tell application "System Events"
activate
set userChoice to button returned of (display alert "Error! Minimum OS requirement:" & return & "OS X 10.8 (Mountain Lion)" ¬
as critical ¬
buttons {"Quit"} ¬
default button 1 ¬
giving up after 60)
end tell
EOT)
exit
fi
# icon & cache dir
ICON64="iVBORw0KGgoAAAANSUhEUgAAAIwAAACMEAYAAAD+UJ19AAACYElEQVR4nOzUsW1T
URxH4fcQSyBGSPWQrDRZIGUq2IAmJWyRMgWRWCCuDAWrGDwAkjsk3F/MBm6OYlnf
19zqSj/9i/N6jKenaRpjunhXV/f30zTPNzePj/N86q9fHx4evi9j/P202/3+WO47
D2++3N4uyzS9/Xp3d319+p3W6+fncfTnqNx3Lpbl3bf/72q1+jHPp99pu91sfr4f
43DY7w+fu33n4tVLDwAul8AAGYEBMgIDZAQGyAgMkBEYICMwQEZggIzAABmBATIC
A2QEBsgIDJARGCAjMEBGYICMwAAZgQEyAgNkBAbICAyQERggIzBARmCAjMAAGYEB
MgIDZAQGyAgMkBEYICMwQEZggIzAABmBATICA2QEBsgIDJARGCAjMEBGYICMwAAZ
gQEyAgNkBAbICAyQERggIzBARmCAjMAAGYEBMgIDZAQGyAgMkBEYICMwQEZggIzA
ABmBATICA2QEBsgIDJARGCAjMEBGYICMwAAZgQEyAgNkBAbICAyQERggIzBARmCA
jMAAGYEBMgIDZAQGyAgMkBEYICMwQEZggIzAABmBATICA2QEBsgIDJARGCAjMEBG
YICMwAAZgQEyAgNkBAbICAyQERggIzBARmCAjMAAGYEBMgIDZAQGyAgMkBEYICMw
QEZggIzAABmBATICA2QEBsgIDJARGCAjMEBGYICMwAAZgQEyAgNkBAbICAyQERgg
IzBARmCAjMAAGYEBMgIDZAQGyAgMkBEYICMwQEZggIzAABmBATICA2QEBsgIDJAR
GCAjMEBGYICMwAAZgQEy/wIAAP//nmUueblZmDIAAAAASUVORK5CYII="
CACHE_DIR="$HOME/Library/Caches/local.lcars.Checksums"
if [[ ! -e "$CACHE_DIR" ]] ; then
mkdir -p "$CACHE_DIR"
fi
ICON_LOC="$CACHE_DIR/lcars.png"
if [[ ! -e "$ICON_LOC" ]] ; then
echo "$ICON64" > "$CACHE_DIR/lcars.base64"
/usr/bin/base64 -D -i "$CACHE_DIR/lcars.base64" -o "$ICON_LOC" && rm -rf "$CACHE_DIR/lcars.base64"
fi
if [[ -e "$CACHE_DIR/lcars.base64" ]] ; then
rm -rf "$CACHE_DIR/lcars.base64"
fi
# bin directory for scripted checksum calculations
BIN_DIR="$CACHE_DIR/bin"
if [[ ! -e "$BIN_DIR" ]] ; then
mkdir -p "$BIN_DIR"
fi
# tmp directory / remove old temp files
if [[ ! -e "$CACHE_DIR/tmp" ]] ; then
mkdir -p "$CACHE_DIR/tmp"
else
rm -rf "$CACHE_DIR/tmp/"*
fi
# check if Adler-32 script exists; create & chmod, if necessary
if [[ ! -f "$BIN_DIR/adler32.py" ]] ; then
ADLERSCRIPT=$(/bin/cat << 'EOT'
#!/usr/bin/env python
'''Calculate Adler-32 checksum for given file'''
'''Origin: https://gist.github.com/kofemann/2303046'''
BLOCKSIZE=256*1024*1024
import sys
from zlib import adler32
for fname in sys.argv[1:]:
asum = 1
with open(fname) as f:
while True:
data = f.read(BLOCKSIZE)
if not data:
break
asum = adler32(data, asum)
if asum < 0:
asum += 2**32
print hex(asum)[2:10].zfill(8).lower(), fname
EOT)
echo "$ADLERSCRIPT" > "$BIN_DIR/adler32.py" && /bin/chmod +x "$BIN_DIR/adler32.py"
fi
# look for terminal-notifier
TERMNOTE_LOC=$(/usr/bin/mdfind "kMDItemCFBundleIdentifier == 'nl.superalloy.oss.terminal-notifier'" 2>/dev/null | /usr/bin/awk 'NR==1')
if [[ "$TERMNOTE_LOC" == "" ]] ; then
NOTESTATUS="osa"
else
NOTESTATUS="tn"
fi
# check for preferences file
PREFS="local.lcars.Checksums"
if [[ ! -f "$HOME/Library/Preferences/$PREFS.plist" ]] ; then
touch "$HOME/Library/Preferences/$PREFS.plist"
/usr/bin/defaults write $PREFS prevCS ""
fi
# check if present: rhash, transmission-show, gsutil
RHASH=$(which rhash 2>/dev/null)
if [[ "$RHASH" != "/"*"/rhash" ]] ; then
RHASH_STATUS="false"
else
RHASH_STATUS="true"
fi
TRANSM=$(which transmission-show 2>/dev/null)
if [[ "$TRANSM" != "/"*"/transmission-show" ]] ; then
BENCODE="false"
else
BENCODE="true"
fi
GSUTIL=$(which gsutil 2>/dev/null)
if [[ "$GSUTIL" != "/"*"/gsutil" ]] ; then
CASTA="false"
else
CASTA="true"
fi
if [[ "$RHASH_STATUS" == "false" ]] && [[ "$BENCODE" == "false" ]] && [[ "$CASTA" == "false" ]] ; then
EXTD="native"
elif [[ "$RHASH_STATUS" == "true" ]] && [[ "$BENCODE" == "false" ]] && [[ "$CASTA" == "false" ]] ; then
EXTD="rh"
elif [[ "$RHASH_STATUS" == "true" ]] && [[ "$BENCODE" == "true" ]] && [[ "$CASTA" == "false" ]] ; then
EXTD="rh-bc"
elif [[ "$RHASH_STATUS" == "true" ]] && [[ "$BENCODE" == "false" ]] && [[ "$CASTA" == "true" ]] ; then
EXTD="rh-ca"
elif [[ "$RHASH_STATUS" == "true" ]] && [[ "$BENCODE" == "true" ]] && [[ "$CASTA" == "true" ]] ; then
EXTD="rh-bc-ca"
elif [[ "$RHASH_STATUS" == "false" ]] && [[ "$BENCODE" == "true" ]] && [[ "$CASTA" == "false" ]] ; then
EXTD="bc"
elif [[ "$RHASH_STATUS" == "false" ]] && [[ "$BENCODE" == "true" ]] && [[ "$CASTA" == "true" ]] ; then
EXTD="bc-ca"
elif [[ "$RHASH_STATUS" == "false" ]] && [[ "$BENCODE" == "false" ]] && [[ "$CASTA" == "true" ]] ; then
EXTD="ca"
fi
FILEPATH="$1" # ALT: delete for workflow
# ALT: for FILEPATH in "$@"
# ALT: do
FILE=$(/usr/bin/basename "$FILEPATH")
METHOD=""
# verify .sfv digest
if [[ "$FILE" == *".sfv" ]] ; then
METHOD="digest"
notify "⚠️ Please wait!" "Verifying checksums in SFV file…"
PARENT=$(/usr/bin/dirname "$FILEPATH")
SFV_CONTENT=$(/bin/cat "$FILEPATH")
ERROR=""
ERROR_COUNT="0"
while read -r LINE
do
SFV_HASH=$(echo "$LINE" | /usr/bin/rev | /usr/bin/awk '{print $1}' | /usr/bin/rev)
SFV_NAME=$(echo "$LINE" | /usr/bin/rev | /usr/bin/awk '{print substr($0, index($0,$2))}' | /usr/bin/rev)
if [[ "$SFV_HASH" == "" ]] || [[ "$SFV_NAME" == "" ]] ; then
continue
fi
if [[ ! -f "$PARENT/$SFV_NAME" ]] ; then
echo "Missing file: $SFV_NAME"
ERROR="true"
((ERROR_COUNT++))
continue
else
SFV_HEX_ALL=$(/usr/bin/cksum -o 3 "$PARENT/$SFV_NAME" 2>/dev/null | while read CK SIZE FILE; do printf "%s %08X\n" "$FILE" "$CK"; done)
SFV_HEX=$(echo "$SFV_HEX_ALL" | /usr/bin/rev | /usr/bin/awk '{print $1}' | /usr/bin/rev)
if [[ "$SFV_HEX" != "$SFV_HASH" ]] ; then
echo "Checksum mismatch: $SFV_NAME"
ERROR="true"
((ERROR_COUNT++))
fi
fi
done < <(echo "$SFV_CONTENT") > "$CACHE_DIR/tmp/csDig"
if [[ "$ERROR" == "true" ]] ; then
if [[ "$ERROR_COUNT" -gt 1 ]] ; then
FILE_SIG="files"
else
FILE_SIG="file"
fi
/usr/bin/sort "$CACHE_DIR/tmp/csDig" -o "$CACHE_DIR/tmp/csDig"
notify "❌ Failed [SFV]" "$ERROR_COUNT $FILE_SIG had errors!"
echo "Failed [SFV]: $ERROR_COUNT $FILE_SIG had errors!"
/usr/bin/open -a TextEdit "$CACHE_DIR/tmp/csDig"
else
notify "✅ Success [SFV]" "All checksums match!"
echo "Success [SFV]: all checksums match!"
fi
exit # ALT: continue
# verify .md5 digest
elif [[ "$FILE" == *".md5" ]] ; then
METHOD="digest"
notify "⚠️ Please wait!" "Verifying checksums in MD5 file…"
PARENT=$(/usr/bin/dirname "$FILEPATH")
MD5_CONTENT=$(/bin/cat "$FILEPATH")
ERROR=""
ERROR_COUNT="0"
while read -r LINE
do
MD5_HASH=$(echo "$LINE" | /usr/bin/awk '{print $1}' | /usr/bin/tr '[:upper:]' '[:lower:]')
MD5_NAME=$(echo "$LINE" | /usr/bin/awk '{print substr($0, index($0,$2))}')
if [[ "$MD5_NAME" == "" ]] || [[ "$MD5_HASH" == "" ]] ; then
continue
fi
if [[ ! -f "$PARENT/$MD5_NAME" ]] ; then
echo "Missing file: $MD5_NAME"
ERROR="true"
((ERROR_COUNT++))
continue
else
MD5_CALC=$(/sbin/md5 -q "$PARENT/$MD5_NAME")
if [[ "$MD5_CALC" != "$MD5_HASH" ]] ; then
echo "Checksum mismatch: $MD5_NAME"
ERROR="true"
((ERROR_COUNT++))
fi
fi
done < <(echo "$MD5_CONTENT") > "$CACHE_DIR/tmp/csDig"
if [[ "$ERROR" == "true" ]] ; then
if [[ "$ERROR_COUNT" -gt 1 ]] ; then
FILE_SIG="files"
else
FILE_SIG="file"
fi
/usr/bin/sort "$CACHE_DIR/tmp/csDig" -o "$CACHE_DIR/tmp/csDig"
notify "❌ Failed [MD5]" "$ERROR_COUNT $FILE_SIG had errors!"
echo "Failed [MD5]: $ERROR_COUNT $FILE_SIG had errors!"
/usr/bin/open -a TextEdit "$CACHE_DIR/tmp/csDig"
else
notify "✅ Success [MD5]" "All checksums match!"
echo "Success [MD5]: all checksums match!"
fi
exit # ALT: continue
# verify .sha1, .sha256 or .sha512 digest
elif [[ "$FILE" == *".sha1" ]] || [[ "$FILE" == *".sha512" ]] || [[ "$FILE" == *".sha256" ]] ; then
METHOD="digest"
EXTENSION="${FILE##*.}"
SHA_OPTION=$(echo "$EXTENSION" | /usr/bin/awk -F"sha" '{print $2}')
if [[ "$SHA_OPTION" != "512" ]] && [[ "$SHA_OPTION" != "256" ]] && [[ "$SHA_OPTION" != "1" ]] ; then
notify "☠️ Internal error" "Something went wrong"
exit # ALT: continue
fi
notify "⚠️ Please wait!" "Verifying checksums in SHA$SHA_OPTION file…"
PARENT=$(/usr/bin/dirname "$FILEPATH")
SHA_CONTENT=$(/bin/cat "$FILEPATH")
ERROR=""
ERROR_COUNT="0"
while read -r LINE
do
SHA_HASH=$(echo "$LINE" | /usr/bin/awk '{print $1}' | /usr/bin/tr '[:upper:]' '[:lower:]')
SHA_NAME=$(echo "$LINE" | /usr/bin/awk '{print substr($0, index($0,$2))}')
if [[ "$SHA_NAME" == "" ]] || [[ "$SHA_HASH" == "" ]] ; then
continue
fi
if [[ ! -f "$PARENT/$SHA_NAME" ]] ; then
echo "Missing file: $SHA_NAME"
ERROR="true"
((ERROR_COUNT++))
continue
else
SHA_CALC=$(/usr/bin/shasum -a "$SHA_OPTION" "$PARENT/$SHA_NAME" | /usr/bin/awk '{print $1}')
if [[ "$SHA_CALC" != "$SHA_HASH" ]] ; then
echo "Checksum mismatch: $SHA_NAME"
ERROR="true"
((ERROR_COUNT++))
fi
fi
done < <(echo "$SHA_CONTENT") > "$CACHE_DIR/tmp/csDig"
if [[ "$ERROR" == "true" ]] ; then
if [[ "$ERROR_COUNT" -gt 1 ]] ; then
FILE_SIG="files"
else
FILE_SIG="file"
fi
/usr/bin/sort "$CACHE_DIR/tmp/csDig" -o "$CACHE_DIR/tmp/csDig"
notify "❌ Failed [SHA-$SHA_OPTION]" "$ERROR_COUNT $FILE_SIG had errors!"
echo "Failed [SHA-$SHA_OPTION]: $ERROR_COUNT $FILE_SIG had errors!"
/usr/bin/open -a TextEdit "$CACHE_DIR/tmp/csDig"
else
notify "✅ Success [SHA-$SHA_OPTION]" "All checksums match!"
echo "Success [SHA-$SHA_OPTION]: all checksums match!"
fi
exit # ALT: continue
else
# check if bundle/directory
if [[ ! -f "$FILEPATH" ]] ; then
PATH_TYPE=$(/usr/bin/mdls -name kMDItemContentTypeTree "$FILEPATH" | /usr/bin/grep -e "bundle")
if [[ "$PATH_TYPE" != "" ]] ; then
notify "💣 Error: target is a bundle" "$FILE"
exit # ALT: continue
fi
if [[ -d "$FILEPATH" ]] ; then
CONTENT_LIST=$(find "$FILEPATH" -type f -not -path '*/\.*')
if [[ "$CONTENT_LIST" == "" ]] ; then
notify "💣 Error: empty directory" "$FILE"
exit # ALT: continue
fi
METHOD="digest"
# ask for checksums file creation
SFV_CHOICE=$(/usr/bin/osascript << EOT
tell application "System Events"
activate
set theLogoPath to ((path to library folder from user domain) as text) & "Caches:local.lcars.Checksums:lcars.png"
set theButton to button returned of (display dialog "You have selected the directory \"" & "$FILE" & "\". How do you want to proceed?" ¬
buttons {"Compare Folders", "Calculate Other", "Calculate SFV"} ¬
default button 3 ¬
with title "Checksums" ¬
with icon file theLogoPath ¬
giving up after 180)
end tell
theButton
EOT)
if [[ "$SFV_CHOICE" == "" ]] || [[ "$SFV_CHOICE" == "false" ]] ; then
exit # ALT: continue
fi
# compare target folder to second one
if [[ "$SFV_CHOICE" == "Compare Folders" ]] ; then
CFOLDERPATH=$(/usr/bin/osascript << EOT
tell application "System Events"
activate
set theStartDirectory to ((path to home folder from user domain) as text) as alias
set theCFolder to choose folder with prompt "Please select the second folder for comparison…" default location theStartDirectory with invisibles
set theCFolderPath to (POSIX path of theCFolder)
end tell
theCFolderPath
EOT)
if [[ "$CFOLDERPATH" == "" ]] || [[ "$CFOLDERPATH" == "false" ]] ; then
exit # ALT: continue
fi
if [[ "$CFOLDERPATH" == "$FILEPATH" ]] ; then
notify "💣 Error: same directory" "${FILEPATH/$HOME/~}"
exit # ALT: continue
fi
notify "⚠️ Please wait!" "Scanning original directory…"
cd "$FILEPATH"
FILES=$(find * -type f -not -path '*/\.DS_Store')
echo "$FILES" | while read -r LINE
do
if [[ "$LINE" == *".sfv" ]] || [[ "$LINE" == *".md5" ]] || [[ "$LINE" == *".sha1" ]] || [[ "$LINE" == *".sha256" ]] || [[ "$LINE" == *".sha512" ]] || [[ "$LINE" == "checksums.crc32" ]] ; then
continue
fi
FCRC=$(/usr/bin/crc32 "$LINE")
echo "$FCRC $LINE"
done > "$FILEPATH/checksums.crc32"
notify "⚠️ Please wait!" "Comparing directories…"
cd /
CRCDIG=$(/bin/cat "$FILEPATH/checksums.crc32")
ERROR=""
ERROR_COUNT="0"
while read -r LINE
do
COUNTERPART=$(echo "$LINE" | /usr/bin/awk '{print substr($0, index($0,$2))}')
if [[ ! -f "$CFOLDERPATH/$COUNTERPART" ]] ; then
ERROR="true"
((ERROR_COUNT++))
echo "Missing file: $COUNTERPART"
else
ORIGINAL_HASH=$(echo "$LINE" | /usr/bin/awk '{print $1}')
COUNTERHASH=$(/usr/bin/crc32 "$CFOLDERPATH/$COUNTERPART")
if [[ "$COUNTERHASH" != "$ORIGINAL_HASH" ]] ; then
ERROR="true"
((ERROR_COUNT++))
echo "Checksum mismatch: $COUNTERPART"
fi
fi
done < <(echo "$CRCDIG") > "$CACHE_DIR/tmp/csDirCompare"
rm -rf "$FILEPATH/checksums.crc32"
if [[ "$ERROR" == "true" ]] ; then
if [[ "$ERROR_COUNT" -gt 1 ]] ; then
FILE_SIG="files"
else
FILE_SIG="file"
fi
notify "❌ Failed [CRC-32]" "$ERROR_COUNT $FILE_SIG had errors!"
echo "Failed [CRC-32]: $ERROR_COUNT $FILE_SIG had errors!"
/usr/bin/sort "$CACHE_DIR/tmp/csDirCompare" -o "$CACHE_DIR/tmp/csDirCompare"
/usr/bin/open -a TextEdit "$CACHE_DIR/tmp/csDirCompare"
else
notify "✅ Success [CRC-32]" "All checksums match!"
echo "Success [CRC-32]: all checksums match!"
fi
exit # ALT: continue
# create SFV checksums file
elif [[ "$SFV_CHOICE" == "Calculate SFV" ]] ; then
notify "⚠️ Please wait!" "Creating SFV checksums file…"
cd "$FILEPATH"
FILES=$(find * -type f -not -path '*/\.*')
echo "$FILES" | while read -r FILE
do
if [[ "$FILE" == *".sfv" ]] || [[ "$FILE" == *".md5" ]] || [[ "$FILE" == *".sha1" ]] || [[ "$FILE" == *".sha256" ]] || [[ "$FILE" == *".sha512" ]] || [[ "$FILE" == "checksums.crc32" ]] ; then
continue
fi
/usr/bin/cksum -o 3 "$FILE" 2>/dev/null | while read CK SIZE FILE; do printf "%s %08X\n" "$FILE" "$CK"; done
done > "$FILEPATH/checksums.sfv"
cd $HOME
notify "✅ Done [SFV]" "checksums.sfv"
echo "Done [SFV]: checksums.sfv"
exit # ALT: continue
# select other digest algorithm
elif [[ "$SFV_CHOICE" == "Calculate Other" ]] ; then
DIG_CHOICE=$(/usr/bin/osascript 2>/dev/null << EOT
tell application "System Events"
set theList to {"MD5","SHA-1","SHA-256","SHA-512"}
set theResult to choose from list theList with prompt "Please select the algorithm." with title "Checksums" OK button name "Select" cancel button name "Cancel" without multiple selections allowed
end tell
theResult
EOT)
if [[ "$DIG_CHOICE" == "" ]] || [[ "$DIG_CHOICE" == "false" ]] ; then
exit # ALT: continue
fi
notify "⚠️ Please wait!" "Creating $DIG_CHOICE checksums file…"
cd "$FILEPATH"
FILES=$(find * -type f -not -path '*/\.*')
if [[ "$DIG_CHOICE" == "MD5" ]] ; then
echo "$FILES" | while read -r FILE
do
if [[ "$FILE" == *".sfv" ]] || [[ "$FILE" == *".md5" ]] || [[ "$FILE" == *".sha1" ]] || [[ "$FILE" == *".sha256" ]] || [[ "$FILE" == *".sha512" ]] || [[ "$FILE" == "checksums.crc32" ]] ; then
continue
fi
/sbin/md5 -r "$FILE"
done > "$FILEPATH/checksums.md5"
notify "✅ Done [MD5]" "checksums.md5"
echo "Done [MD5]: checksums.md5"
elif [[ "$DIG_CHOICE" == "SHA-"* ]] ; then
DIG_SHAOPT=$(echo "$DIG_CHOICE" | /usr/bin/awk -F"-" '{print $2}')
echo "$FILES" | while read -r FILE
do
if [[ "$FILE" == *".sfv" ]] || [[ "$FILE" == *".md5" ]] || [[ "$FILE" == *".sha1" ]] || [[ "$FILE" == *".sha256" ]] || [[ "$FILE" == *".sha512" ]] || [[ "$FILE" == "checksums.crc32" ]] ; then
continue
fi
/usr/bin/shasum -a "$DIG_SHAOPT" "$FILE"
done > "$FILEPATH/checksums.sha$DIG_SHAOPT"
notify "✅ Done [SHA-$DIG_SHAOPT]" "checksums.sha$DIG_SHAOPT"
echo "Done [SHA-$DIG_SHAOPT]: checksums.sha$DIG_SHAOPT"
fi
cd $HOME
exit # ALT: continue
else
exit # ALT: continue
fi
fi
fi
fi
if [[ "$METHOD" != "digest" ]] ; then
CHECKSUM=$(/usr/bin/pbpaste | /usr/bin/xargs 2>/dev/null)
PREV_CS=$(/usr/bin/defaults read $PREFS prevCS)
if [[ "$CHECKSUM" != "$PREV_CS" ]] ; then
if [[ "$EXTD" == "native" ]] || [[ "$EXTD" == "bc" ]] || [[ "$EXTD" == "bc-ca" ]] || [[ "$EXTD" == "ca" ]] ; then
CS_TYPE=$(cscn "$CHECKSUM")
elif [[ "$EXTD" == "rh" ]] || [[ "$EXTD" == "rh-bc" ]] || [[ "$EXTD" == "rh-bc-ca" ]] || [[ "$EXTD" == "rh-ca" ]] ; then
CS_TYPE=$(cscx "$CHECKSUM")
fi
if [[ "$CS_TYPE" != "false" ]] ; then
PREFSUM=$(echo "$CHECKSUM" | /usr/bin/tr '[:upper:]' '[:lower:]')
/usr/bin/defaults write $PREFS prevCS "$PREFSUM"
fi
else
CS_TYPE="false"
CHECKSUM=""
/usr/bin/defaults write $PREFS prevCS ""
fi
# calculate file size
BYTES=$(/usr/bin/stat -f%z "$FILEPATH")
MEGABYTES=$(bc -l <<< "scale=6; $BYTES/1000000")
if [[ ($MEGABYTES<1) ]] ; then
SIZE="0$MEGABYTES"
else
SIZE="$MEGABYTES"
fi
MIB_RAW=$(/usr/bin/bc -l <<< "scale=6; $BYTES/1048576")
if [[ ($MIB_RAW<1) ]] ; then
MIB="0$MIB_RAW"
else
MIB="$MIB_RAW"
fi
# ask for checksum algorithm, if clipboard is empty
if [[ "$CS_TYPE" == "false" ]] || [[ "$CS_TYPE" == "" ]] ; then
METHOD="new"
CS_CHOICE=$(/usr/bin/osascript << EOT
tell application "System Events"
activate
set theLogoPath to ((path to library folder from user domain) as text) & "Caches:local.lcars.Checksums:lcars.png"
set theButton to button returned of (display dialog "You have selected the target \"" & "$FILE" & "\". How do you want to proceed?" ¬
buttons {"Compare Files", "Calculate Other", "Calculate SHA-256"} ¬
default button 3 ¬
with title "Checksums" ¬
with icon file theLogoPath ¬
giving up after 180)
end tell
theButton
EOT)
if [[ "$CS_CHOICE" == "" ]] || [[ "$CS_CHOICE" == "false" ]] ; then
exit # ALT: continue
fi
if [[ "$CS_CHOICE" == "Calculate SHA-256" ]] || [[ "$CS_CHOICE" == "Calculate Other" ]] ; then
METHOD="new"
elif [[ "$CS_CHOICE" == "Compare Files" ]] ; then
METHOD="fcompare"
fi
# default choice: calculate SHA-256
if [[ "$CS_CHOICE" == "Calculate SHA-256" ]] ; then
FILESUM=$(/usr/bin/shasum -a 256 "$FILEPATH" | /usr/bin/awk '{print $1}')
CS_CHOICE="SHA-256"
# MD5-compare with a second file
elif [[ "$CS_CHOICE" == "Compare Files" ]] ; then
CFILEPATH=$(/usr/bin/osascript 2>/dev/null << EOT
tell application "System Events"
activate
set theStartDirectory to ((path to home folder from user domain) as text) as alias
set theCFile to choose file with prompt "Please select the second file for comparison…" default location theStartDirectory with invisibles
set theCFilePath to (POSIX path of theCFile)
end tell
theCFilePath
EOT)
if [[ "$CFILEPATH" == "" ]] || [[ "$CFILEPATH" == "false" ]] ; then
/usr/bin/defaults write $PREFS prevCS "$CHECKSUM"
exit # ALT: continue
fi
if [[ "$CFILEPATH" == "$FILEPATH" ]] ; then
notify "💣 Error: same file" "${FILEPATH/$HOME/~}"
/usr/bin/defaults write $PREFS prevCS "$CHECKSUM"
exit # ALT: continue
fi
CPATH_TYPE=$(/usr/bin/mdls -name kMDItemContentTypeTree "$CFILEPATH" | /usr/bin/grep -e "bundle")
if [[ "$CPATH_TYPE" != "" ]] ; then
notify "💣 Error: target is a bundle" "$CFILEPATH"
/usr/bin/defaults write $PREFS prevCS "$CHECKSUM"
exit # ALT: continue
fi
if [[ -d "$CFILEPATH" ]] ; then
notify "💣 Error: target is a directory" "$CFILEPATH"
/usr/bin/defaults write $PREFS prevCS "$CHECKSUM"
exit # ALT: continue
fi
notify "⚠️ Please wait!" "Comparing files…"
ALGORITHM="MD5"
CFILE=$(/usr/bin/basename "$CFILEPATH")
CBYTES=$(/usr/bin/stat -f%z "$CFILEPATH")
OHASH=$(/sbin/md5 -q "$FILEPATH")
CHASH=$(/sbin/md5 -q "$CFILEPATH")
if [[ "$OHASH" == "$CHASH" ]] ; then
notify "✅ Success [MD5]" "Checksums match"
STATUS="✅ Success"
else
notify "❌ Failed [MD5]" "Checksum mismatch"
STATUS="❌ Failed"
fi
HOMEPATH=$(echo $HOME)
echo "Files:"
echo "$FILE"
echo "$CFILE"
echo "Paths:"
echo "${FILEPATH/$HOME/~}"
echo "${CFILEPATH/$HOME/~}"
echo "Sizes:"
echo "$BYTES B"
echo "$CBYTES B"
echo "Checksums ($ALGORITHM):"
echo "$OHASH"
echo "$CHASH"
echo "Result: $STATUS"
CPROMPT="■■■ Files ■■■
[1] $FILE
[2] $CFILE
■■■ Paths ■■■
[1] ${FILEPATH/$HOME/~}
[2] ${CFILEPATH/$HOME/~}
■■■ Sizes ■■■
[1] $BYTES B
[2] $CBYTES B
■■■ Verification ■■■
$STATUS
■■■ Checksums [$ALGORITHM] ■■■
[1] $OHASH
[2] $CHASH"
# create prompt
CINFO=$(/usr/bin/osascript 2>/dev/null<< EOT
tell application "System Events"
activate
set theLogoPath to ((path to library folder from user domain) as text) & "Caches:local.lcars.Checksums:lcars.png"
set userChoice to button returned of (display dialog "$CPROMPT" ¬
buttons {"OK"} ¬
default button 1 ¬
with title "Checksums" ¬
with icon file theLogoPath ¬
giving up after 180)
end tell
EOT)
/usr/bin/defaults write $PREFS prevCS "$CHECKSUM"
exit # ALT: continue
# ask for other choice from list
elif [[ "$CS_CHOICE" == "Calculate Other" ]] ; then
if [[ "$EXTD" == "native" ]] ; then
HA_CHOICE=$(/usr/bin/osascript 2>/dev/null << EOT
tell application "System Events"
set theList to {"Adler-32","CRC Hashes…","MD4","MD5","MDC-2","RIPEMD-160","SHA-0","SHA-1","SHA-224","SHA-256","SHA-384","SHA-512"}
set theResult to choose from list theList with prompt "Please select the algorithm." with title "Checksums" OK button name "Select" cancel button name "Cancel" without multiple selections allowed
end tell
theResult
EOT)
elif [[ "$EXTD" == "rh" ]] ; then
HA_CHOICE=$(/usr/bin/osascript 2>/dev/null << EOT
tell application "System Events"
set theList to {"Adler-32","AICH","BTIH","CRC Hashes…","DC++ TTH","ED2K","EDON-R 256","EDON-R 512","GOST","GOST CryptoPro","HAS-160","MD4","MD5","MDC-2","RIPEMD-160","SHA-0","SHA-1","SHA-224","SHA-256","SHA-384","SHA-512","SHA3-224","SHA3-256","SHA3-384","SHA3-512","SNEFRU-128","SNEFRU-256","Tiger","Whirlpool"}
set theResult to choose from list theList with prompt "Please select the algorithm." with title "Checksums" OK button name "Select" cancel button name "Cancel" without multiple selections allowed
end tell
theResult
EOT)
elif [[ "$EXTD" == "bc" ]] ; then
HA_CHOICE=$(/usr/bin/osascript 2>/dev/null << EOT
tell application "System Events"
set theList to {"Adler-32","Bencode","CRC (BSD)","CRC Hashes…","MD4","MD5","MDC-2","RIPEMD-160","SHA-0","SHA-1","SHA-224","SHA-256","SHA-384","SHA-512"}
set theResult to choose from list theList with prompt "Please select the algorithm." with title "Checksums" OK button name "Select" cancel button name "Cancel" without multiple selections allowed
end tell
theResult
EOT)
elif [[ "$EXTD" == "ca" ]] ; then
HA_CHOICE=$(/usr/bin/osascript 2>/dev/null << EOT
tell application "System Events"
set theList to {"Adler-32","CRC (BSD)","CRC Hashes…","MD4","MD5","MDC-2","RIPEMD-160","SHA-0","SHA-1","SHA-224","SHA-256","SHA-384","SHA-512"}
set theResult to choose from list theList with prompt "Please select the algorithm." with title "Checksums" OK button name "Select" cancel button name "Cancel" without multiple selections allowed
end tell
theResult
EOT)
elif [[ "$EXTD" == "rh-bc" ]] ; then
HA_CHOICE=$(/usr/bin/osascript 2>/dev/null << EOT
tell application "System Events"
set theList to {"Adler-32","AICH","Bencode","BTIH","CRC Hashes…","DC++ TTH","ED2K","EDON-R 256","EDON-R 512","GOST","GOST CryptoPro","HAS-160","MD4","MD5","MDC-2","RIPEMD-160","SHA-0","SHA-1","SHA-224","SHA-256","SHA-384","SHA-512","SHA3-224","SHA3-256","SHA3-384","SHA3-512","SNEFRU-128","SNEFRU-256","Tiger","Whirlpool"}
set theResult to choose from list theList with prompt "Please select the algorithm." with title "Checksums" OK button name "Select" cancel button name "Cancel" without multiple selections allowed
end tell
theResult
EOT)
elif [[ "$EXTD" == "rh-bc-ca" ]] ; then
HA_CHOICE=$(/usr/bin/osascript 2>/dev/null << EOT
tell application "System Events"
set theList to {"Adler-32","AICH","Bencode","BTIH","CRC Hashes…","DC++ TTH","ED2K","EDON-R 256","EDON-R 512","GOST","GOST CryptoPro","HAS-160","MD4","MD5","MDC-2","RIPEMD-160","SHA-0","SHA-1","SHA-224","SHA-256","SHA-384","SHA-512","SHA3-224","SHA3-256","SHA3-384","SHA3-512","SNEFRU-128","SNEFRU-256","Tiger","Whirlpool"}
set theResult to choose from list theList with prompt "Please select the algorithm." with title "Checksums" OK button name "Select" cancel button name "Cancel" without multiple selections allowed
end tell
theResult
EOT)
elif [[ "$EXTD" == "rh-ca" ]] ; then
HA_CHOICE=$(/usr/bin/osascript 2>/dev/null << EOT
tell application "System Events"
set theList to {"Adler-32","AICH","BTIH","CRC Hashes…","DC++ TTH","ED2K","EDON-R 256","EDON-R 512","GOST","GOST CryptoPro","HAS-160","MD4","MD5","MDC-2","RIPEMD-160","SHA-0","SHA-1","SHA-224","SHA-256","SHA-384","SHA-512","SHA3-224","SHA3-256","SHA3-384","SHA3-512","SNEFRU-128","SNEFRU-256","Tiger","Whirlpool"}
set theResult to choose from list theList with prompt "Please select the algorithm." with title "Checksums" OK button name "Select" cancel button name "Cancel" without multiple selections allowed
end tell
theResult
EOT)
elif [[ "$EXTD" == "bc-ca" ]] ; then
HA_CHOICE=$(/usr/bin/osascript 2>/dev/null << EOT
tell application "System Events"
set theList to {"Adler-32","Bencode","CRC Hashes…","MD4","MD5","MDC-2","RIPEMD-160","SHA-0","SHA-1","SHA-224","SHA-256","SHA-384","SHA-512"}
set theResult to choose from list theList with prompt "Please select the algorithm." with title "Checksums" OK button name "Select" cancel button name "Cancel" without multiple selections allowed
end tell
theResult
EOT)
fi
if [[ "$HA_CHOICE" == "" ]] || [[ "$HA_CHOICE" == "false" ]] ; then
exit # ALT: continue
fi
if [[ "$HA_CHOICE" == "CRC Hashes…" ]] ; then
if [[ $(echo "$EXTD" | /usr/bin/grep "ca") != "" ]] ; then
HA_CHOICE=$(/usr/bin/osascript 2>/dev/null << EOT
tell application "System Events"
set theList to {"CRC (BSD)","CRC (System V)","CRC (legacy 32bit)","CRC (ISO/IEC 8802-3)","CRC-32","CRC-32C (Hex)"}
set theResult to choose from list theList with prompt "Please select the CRC hash." with title "Checksums" OK button name "Select" cancel button name "Cancel" without multiple selections allowed
end tell
theResult
EOT)
else
HA_CHOICE=$(/usr/bin/osascript 2>/dev/null << EOT
tell application "System Events"
set theList to {"CRC (BSD)","CRC (System V)","CRC (legacy 32bit)","CRC (ISO/IEC 8802-3)","CRC-32"}
set theResult to choose from list theList with prompt "Please select the CRC hash." with title "Checksums" OK button name "Select" cancel button name "Cancel" without multiple selections allowed
end tell
theResult
EOT)
fi
if [[ "$HA_CHOICE" == "" ]] || [[ "$HA_CHOICE" == "false" ]] ; then
exit # ALT: continue
fi
fi
# calculate checksum based on user choice (native)
if [[ "$HA_CHOICE" == "SHA-"* ]] && [[ "$HA_CHOICE" != "SHA-0" ]] ; then
OPTION=$(echo "$HA_CHOICE" | /usr/bin/awk -F"-" '{print $2}')
FILESUM=$(/usr/bin/shasum -a "$OPTION" "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "MD5" ]] ; then
FILESUM=$(/sbin/md5 -q "$FILEPATH")
elif [[ "$HA_CHOICE" == "MDC-2" ]] ; then
FILESUM=$(/usr/bin/openssl dgst -mdc2 "$FILEPATH" | /usr/bin/awk -F"= " '{print $2}')
elif [[ "$HA_CHOICE" == "RIPEMD-160" ]] ; then
FILESUM=$(/usr/bin/openssl dgst -ripemd160 "$FILEPATH" | /usr/bin/awk -F"= " '{print $2}')
elif [[ "$HA_CHOICE" == "CRC-32" ]] ; then
FILESUM=$(/usr/bin/crc32 "$FILEPATH")
elif [[ "$HA_CHOICE" == "Adler-32" ]] ; then
FILESUM=$(/usr/bin/python "$BIN_DIR/adler32.py" "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "SHA-0" ]] ; then
FILESUM=$(/usr/bin/openssl dgst -sha "$FILEPATH" | /usr/bin/awk -F"= " '{print $2}')
elif [[ "$HA_CHOICE" == "MD2" ]] ; then # MD2 option currently not included in osascript list due to openssl bug on macOS
FILESUM=$(/usr/bin/openssl dgst -md2 "$FILEPATH" | /usr/bin/awk -F"= " '{print $2}')
elif [[ "$HA_CHOICE" == "MD4" ]] ; then
FILESUM=$(/usr/bin/openssl dgst -md4 "$FILEPATH" | /usr/bin/awk -F"= " '{print $2}')
elif [[ "$HA_CHOICE" == "CRC (ISO/IEC 8802-3)" ]] ; then
FILESUM=$(/usr/bin/cksum "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "CRC (legacy 32bit)" ]] ; then
FILESUM=$(/usr/bin/cksum -o 3 "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "CRC (BSD)" ]] ; then
FILESUM=$(/usr/bin/cksum -o 1 "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "CRC (System V)" ]] ; then
FILESUM=$(/usr/bin/cksum -o 2 "$FILEPATH" | /usr/bin/awk '{print $1}')
fi
# calculate checksum based on user choice (transmission-show)
if [[ "$HA_CHOICE" == "Bencode" ]] ; then
FILESUM=$("$TRANSM" "$FILEPATH" 2>&1 | /usr/bin/grep "Hash: " | /usr/bin/awk '{print $2}')
if [[ "$FILESUM" == "Error"* ]] || [[ "$FILESUM" == "" ]] ; then
notify "💣 Error [Bencode]" "Target possibly not a torrent file"
exit # ALT: continue
fi
fi
# calculate checksum based on user choice (gsutil)
if [[ "$HA_CHOICE" == "CRC-32C (Hex)" ]] ; then
FILESUM=$("$GSUTIL" hash -c -h "$FILEPATH" 2>&1 | /usr/bin/grep "Hash (crc32c)" | /usr/bin/awk -F: '{print $2}' | /usr/bin/xargs)
if [[ "$FILESUM" == "CommandException"* ]] || [[ "$FILESUM" == "" ]] ; then
notify "☠️ Internal error" "gsutil calculation error"
exit # ALT: continue
fi
fi
# calculate checksum based on user choice (rhash or rhash plus transmission-show)
if [[ $(echo "$EXTD" | /usr/bin/grep "rh") != "" ]] ; then
if [[ "$HA_CHOICE" == "SHA3-"* ]] ; then
OPTION=$(echo "$HA_CHOICE" | /usr/bin/awk -F"-" '{print $2}')
FILESUM=$("$RHASH" --sha3-$OPTION "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "AICH" ]] ; then
FILESUM=$("$RHASH" -A "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "BTIH" ]] ; then
FILESUM=$("$RHASH" --btih "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "DC++ TTH" ]] ; then
FILESUM=$("$RHASH" -T "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "ED2K" ]] ; then
FILESUM=$("$RHASH" -E "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "EDON-R 256" ]] ; then
FILESUM=$("$RHASH" --edonr256 "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "EDON-R 512" ]] ; then
FILESUM=$("$RHASH" --edonr512 "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "GOST" ]] ; then
FILESUM=$("$RHASH" -G "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "GOST CryptoPro" ]] ; then
FILESUM=$("$RHASH" --gost-cryptopro "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "HAS-160" ]] ; then
FILESUM=$("$RHASH" --has160 "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "SNEFRU-128" ]] ; then
FILESUM=$("$RHASH" --snefru128 "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "SNEFRU-256" ]] ; then
FILESUM=$("$RHASH" --snefru256 "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "Tiger" ]] ; then
FILESUM=$("$RHASH" --tiger "$FILEPATH" | /usr/bin/awk '{print $1}')
elif [[ "$HA_CHOICE" == "Whirlpool" ]] ; then
FILESUM=$("$RHASH" -W "$FILEPATH" | /usr/bin/awk '{print $1}')
fi
fi
CS_CHOICE="$HA_CHOICE"
fi
FILESUM=$(echo "$FILESUM" | /usr/bin/tr '[:upper:]' '[:lower:]')
# output for new calculation
echo "File: $FILE"
echo "Size: $BYTES B [$SIZE MB] [$MIB MiB]"
echo "Checksum [$CS_CHOICE]: $FILESUM"
OSA_PROMPT="■■■ File ■■■
$FILE
■■■ Size ■■■
$BYTES B
$SIZE MB
$MIB MiB
■■■ Checksum [$CS_CHOICE] ■■■
$FILESUM
The information has also been copied to your clipboard."
# set copy info for clipboard & copy
COPY_INFO="Name: $FILE
Size: $BYTES B [$SIZE MB] [$MIB MiB]
Checksum [$CS_CHOICE]: $FILESUM"
echo "$COPY_INFO" | /usr/bin/pbcopy
else
METHOD="compare"
fi
if [[ "$METHOD" == "compare" ]] ; then
# clipboard has an apparent checksum
CHECKSUM=$(echo "$CHECKSUM" | /usr/bin/tr '[:upper:]' '[:lower:]')
# calculate BSD/Unix & legacy CRC checksums
if [[ "$CS_TYPE" == "cksum-unix" ]] ; then
FILESUM=$(/usr/bin/cksum -o 1 "$FILEPATH" | /usr/bin/awk '{print $1}')
if [[ "$FILESUM" == "$CHECKSUM" ]] ; then
STATUS="✅ Success"
ALGORITHM="CRC (BSD)"
else
FILESUM=$(/usr/bin/cksum -o 2 "$FILEPATH" | /usr/bin/awk '{print $1}')
if [[ "$FILESUM" == "$CHECKSUM" ]] ; then
STATUS="✅ Success"
ALGORITHM="CRC (System V)"
else
STATUS="❌ Failed"
ALGORITHM="Legacy Unix CRC class"
FILESUM="💣 n/a"
fi
fi
elif [[ "$CS_TYPE" == "cksum3" ]] ; then
ALGORITHM="CRC (legacy 32bit)"
FILESUM=$(/usr/bin/cksum -o 3 "$FILEPATH" | /usr/bin/awk '{print $1}')
if [[ "$FILESUM" == "$CHECKSUM" ]] ; then
STATUS="✅ Success"
else
STATUS="❌ Failed"
FILESUM="💣 n/a"
fi
# calculate CRCs & Adler-32
elif [[ "$CS_TYPE" == "crc" ]] ; then
FILESUM=$(/usr/bin/crc32 "$FILEPATH")
if [[ "$FILESUM" == "$CHECKSUM" ]] ; then
ALGORITHM="CRC-32"
STATUS="✅ Success"
else
FILESUM=$(/usr/bin/python "$BIN_DIR/adler32.py" "$FILEPATH" | /usr/bin/awk '{print $1}')
if [[ "$FILESUM" == "$CHECKSUM" ]] ; then
ALGORITHM="Adler-32"
STATUS="✅ Success"
else
FILESUM=$(/usr/bin/cksum "$FILEPATH" | /usr/bin/awk '{print $1}')
if [[ "$FILESUM" == "$CHECKSUM" ]] ; then
ALGORITHM="CRC (ISO/IEC 8802-3)"
STATUS="✅ Success"
else
FILESUM=$(/usr/bin/cksum -o 3 "$FILEPATH" | /usr/bin/awk '{print $1}')
if [[ "$FILESUM" == "$CHECKSUM" ]] ; then
ALGORITHM="CRC (legacy 32bit)"
STATUS="✅ Success"
else
FILESUM=$(/usr/bin/cksum -o 1 "$FILEPATH" | /usr/bin/awk '{print $1}')
if [[ "$FILESUM" == "$CHECKSUM" ]] ; then