Skip to main content Snippets Groups Projects
xxxd0.1.py 2.16 KiB
#!/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])