-
cleemy desu wayo authoredf4077792
xxxd0.1.py 2.16 KiB
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 python3
#
# ==========================================================================
# xxxd0.1.py -- version 0.0.99.20230927
#
# written by cleemy desu wayo / Licensed under CC0 1.0
#
# official repository: https://gitlab.com/cleemy-desu-wayo/xxxd0.1
# ==========================================================================
import os
import sys
import datetime
import hashlib
import re
now_str = datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S %Z')
argv = sys.argv
convert_type = 'sha2-256'
if len(sys.argv) >= 2:
if sys.argv[1] == '--conv=sha2-256':
convert_type = 'sha2-256'
elif sys.argv[1] == '--conv=base64':
import base64
convert_type = 'base64'
elif re.match(r'.*s.*256', sys.argv[1]):
print('ERROR: invalid option... did you mean sha2-256?', file=sys.stderr)
sys.exit(1)
else:
print('ERROR: invalid option', file=sys.stderr)
sys.exit(1)
header = f'''
#####################################################################
# hash list
#
# {now_str}
#
# format:
# <byte size>,<{convert_type}>
#
#####################################################################
'''[1:-1]
print(header)
def convert_data(data_source):
if convert_type == 'sha2-256':
return hashlib.sha256(data_source).hexdigest()
elif convert_type == 'base64':
return base64.b64encode(data_source).decode()
else:
print('ERROR: unknown convert type', file=sys.stderr)
sys.exit(1)
def get_fileinfo_from_file(file_name):
with open(file_name, mode='rb') as f:
bin_data = f.read()
return len(bin_data), convert_data(bin_data)
file_list = os.listdir('./')
file_list.sort()
for file_name in file_list:
if not os.path.isfile(os.path.join('./', file_name)): # ignore dir
continue
if not os.access(os.path.join('./', file_name), os.R_OK): # ignore unreadable files
continue
if file_name.startswith('.'): # ignore dotfiles
continue
if file_name.startswith('xxxd'): # ignore xxxd*
continue
file_info = get_fileinfo_from_file(file_name)
print(str(file_info[0]) + ',' + file_info[1])