-
cleemy desu wayo authoredf4077792
xxxd0.1.rb 1.64 KiB
#!/usr/bin/env ruby
#
# ==========================================================================
# xxxd0.1.rb -- 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
# ==========================================================================
require 'digest'
now_str = Time.now.utc.strftime('%Y-%m-%d %H:%M:%S %Z')
convert_type = 'sha2-256'
case ARGV[0]
when '--conv=base64'
require 'base64'
convert_type = 'base64'
when '--conv=sha2-256'
convert_type = 'sha2-256'
when /s.*256/
raise 'ERROR: invalid option... did you mean sha2-256?'
when nil
nil
else
raise 'ERROR: invalid option'
end
header = "
#####################################################################
# hash list
#
# #{now_str}
#
# format:
# <byte size>,<#{convert_type}>
#
#####################################################################
".strip
puts header
def convert_data(bin_data, convert_type)
if convert_type == 'sha2-256'
Digest::SHA256.hexdigest(bin_data)
elsif convert_type == 'base64'
Base64.strict_encode64(bin_data)
end
end
def get_fileinfo_from_file(file_name, convert_type)
bin_data = File.read(file_name)
[bin_data.bytesize, convert_data(bin_data, convert_type)]
end
Dir.glob('*') do |file_name|
next unless File.file?(file_name) # ignore dir
next unless File.readable?(file_name) # ignore unreadable files
next if file_name.start_with?('.') # ignore dotfiles
next if file_name.start_with?('xxxd') # ignore xxxd*
file_info = get_fileinfo_from_file(file_name, convert_type)
puts "#{file_info[0]},#{file_info[1]}"
end