Skip to content
Snippets Groups Projects
Select Git revision
  • main
1 result

koregusu.rb

Blame
  • cleemy desu wayo's avatar
    cleemy desu wayo authored
    This is a very sketchy version.
    150ec838
    History
    koregusu.rb 2.80 KiB
    #!/usr/bin/env ruby
    #
    # ==========================================================================
    # koregusu0.1 -- version 0.0.99.20250903
    #
    # written by cleemy desu wayo / Licensed under CC0 1.0
    #
    # official repository: https://gitlab.com/cleemy-desu-wayo/koregusu0.1
    # ==========================================================================
    #
    # * requirements:
    # * Ruby 3.0 or later
    #
    require 'digest'
    require 'optparse'
    class KRGS
    @@debug_mode = false
    def self.debug_mode?
    !!@@debug_mode
    end
    def self.debug_mode=(x)
    @@debug_mode = !!x
    end
    def self.exec_one_step(krgs_code, source)
    if @@debug_mode
    puts krgs_code
    end
    krgs_code_body = krgs_code.partition(":KRGS0.1:")
    checksum, param, later = krgs_code_body[2].partition(/\([^)]+\)/)
    if param == ""
    new_result = checksum
    else
    param_method, param_concat, param_remaining = param.sub(/[\(\)]/, "").split(",")
    if @@debug_mode
    puts "checksum:#{checksum} -- param: #{param} -- later: #{later} -- param_method:#{param_method} -- param_concat: #{param_concat} -- param_remaining : #{param_remaining}"
    end
    # generate a string to pass to the hash function
    key_str = ""
    param_concat.each_char do |tag|
    case tag
    when "k"
    key_str += krgs_code
    when "r"
    if checksum == ""
    key_str += source
    else
    key_str += checksum
    end
    when "s"
    key_str += source
    end
    end
    case param_method
    when "s256"
    new_checksum = Digest::SHA256.hexdigest(key_str)
    when "s512"
    new_checksum = Digest::SHA512.hexdigest(key_str)
    end
    new_param = ""
    new_later = ""
    if param_remaining.to_i > 1
    new_param = "(#{param_method},#{param_concat},#{param_remaining.to_i - 1})"
    new_later = later
    else
    if later.start_with?("(")
    new_later = later
    end
    end
    new_result = "#{krgs_code_body[0]}#{krgs_code_body[1]}#{new_checksum}#{new_param}#{new_later}"
    end
    new_result
    end
    def self.exec(krgs_code, source)
    result = krgs_code
    step = 1
    loop do
    if @@debug_mode
    puts "step:#{step}"
    end
    result = exec_one_step(result, source)
    if @@debug_mode
    puts result
    puts "--"
    end
    break unless result =~ /:KRGS0.1:/
    step += 1
    end
    result
    end
    end
    if $0 == __FILE__
    opts = OptionParser.new
    krgs_code = nil
    source = nil
    opts.on("-e CODE") { |optvalue| krgs_code = optvalue }
    opts.on("--key SOURCE") { |optvalue| source = optvalue }
    opts.on("--debug") { KRGS.debug_mode = true }
    opts.parse!(ARGV)
    if krgs_code
    puts KRGS.exec(krgs_code, source)
    else
    raise "ERROR: lack of KRGS code"
    end
    end