#!/usr/bin/env ruby lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'core' require 'colorize' Signal.trap("INT"){} ARGV << '-h' if ARGV.empty? options = {} options[:outfile] = '' options[:raw] = false OptionParser.new do |opts| opts.banner = "A command-line tool to encrypt and decrypt data in multiple formats.\n\nUsage: #{File.basename($PROGRAM_NAME)} [options] [task] \"data\" [\"key\"]\nKrypton version: #{Paint[VERSION, '#2ecc71']}" opts.separator Paint["\nGlobal Options: ", '#95a5a6'] opts.on('-s', '--std', 'Print output suitable for piping.') do options[:std] = true end opts.on('-o OUTFILE', '--outfile OUTFILE', String, 'A file to save the output in') do |v| options[:outfile] = v end opts.on('-r', '--raw', 'Set the input and output type to the raw bytes, if available (Default: false)') do options[:raw] = true end opts.on('-u', '--urlsafe', 'Make output data URL safe (base64) if applicable') do options[:urlsafe] = true end opts.on('--verbose', 'Run verbosely') do $verbose = true end opts.on('-v', '--version', 'Show the krypton version and exit') do puts "Krypton version: #{Paint[VERSION, '#2ecc71']}" exit 0 end opts.on('-h', '--help', 'Show this help message') do puts opts puts TASKS_HELP exit end opts.separator Paint["\nTasks: ", '#95a5a6'] end.parse!(ARGV) # Tasks loop while (opt = ARGV.shift) do case opt # AESING when 'encrypt' result = Base64.encode64(Krypton::AESCrypt.encrypt(ARGV[ARGV.length - 2], ARGV[ARGV.length - 1], options[:outfile])) if options[:std] puts result.strip else puts "#{ARGV[ARGV.length - 2] + ' => '}#{Paint[result.strip, '#2ecc71']}" end exit 0 when 'decrypt' result = Krypton::AESCrypt.decrypt(Base64.decode64(ARGV[ARGV.length - 2]), ARGV[ARGV.length - 1], options[:outfile]) if options[:std] puts result.strip else puts "#{ARGV[ARGV.length - 2] + ' => '}#{Paint[result.strip, '#2ecc71']}" end exit 0 # HASHING when 'hash' data = ARGV[ARGV.length - 1] || gets result = Krypton::SHA.hash(data, options[:raw]) if options[:std] puts result.strip else puts "#{data + ' => '}#{Paint[result.strip, '#2ecc71']}" end exit 0 when 'uuid' if options[:std] puts SecureRandom.uuid else puts Paint[SecureRandom.uuid, '#2ecc71'] end when 'totp' if ARGV.length == 2 puts "You need a secret to generate a totp." exit 1 else secret = ARGV[ARGV.length - 1] || gets if options[:std] puts ROTP::TOTP.new(secret).now else puts "#{secret + ' => ' + Paint[ROTP::TOTP.new(secret).now,'#2ecc71']}" end exit 0 end when 'b64e' data = ARGV[ARGV.length - 1] || gets if options[:std] puts Krypton::B64.encode(data, options[:urlsafe]) else puts "#{data + ' => ' + Paint[Krypton::B64.encode(data, options[:urlsafe]),'#2ecc71']}" end exit 0 when 'b64d' data = ARGV[ARGV.length - 1] || gets if options[:std] puts Krypton::B64.decode(data, options[:urlsafe]) else puts "#{data + ' => ' + Paint[Krypton::B64.decode(data, options[:urlsafe]),'#2ecc71']}" end exit 0 when 'sum' filename = ARGV[ARGV.length - 1] || gets if options[:std] puts Krypton::SHA.checksum(filename, options[:raw]) else puts "#{filename} => #{Paint[Krypton::SHA.checksum(filename, options[:raw]), '#2ecc71']}" end exit 0 when 'lookup' hashes_key = ENV['HASHES_API_KEY'] hash = ARGV[ARGV.length - 1] if hashes_key.nil? puts "A valid hashes.org API key is required to use this feature." puts "Please set the environment variable HASHES_API_KEY to your key." exit 1 end begin Krypton::Lookup.lookup(hash, hashes_key) rescue StandardError => e puts "ERROR: #{e.message}" exit 1 end else puts "#{opt} is not a valid action!" exit 1 end end