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 commit adds new param_method "copy", "rev", and "REV".
    
    It may be possible to express Bitcoin double-hash using the
    "REV" introduced this time and the "R" introduced in commit
    057fa359.
    
    :KRGS0.1:(s256,s,1)(s256,R,1)(REV,r,1)
    
    The new three param_method "copy", "rev", and "REV" require
    caution as they destroy the structure of the KRGS code.
    For example, the following will not only cause an infinite
    loop, but will also cause the KRGS code to grow larger and
    larger.
    
    $ ./koregusu.rb -e ':KRGS0.1:(copy,s,1)' --source="(copy,s,2)"
    
    This commit is related to 057fa359.
    649bfec0
    History
    koregusu.rb 3.80 KiB
    #!/usr/bin/env ruby
    #
    # ==========================================================================
    # koregusu0.1 -- version 0.0.99.20250906.4
    #
    # 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 krgs_code.is_a?(String)
    krgs_code.force_encoding("ASCII-8BIT")
    else
    raise "ERROR: krgs_code is not a String"
    end
    if source.is_a?(String)
    source.force_encoding("ASCII-8BIT")
    else
    raise "ERROR: source is not a String"
    end
    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 "R"
    if checksum == ""
    if source =~ /\A[0-9a-f]+\z/
    key_str += [source].pack("H*")
    end
    elsif checksum =~ /\A[0-9a-f]+\z/
    key_str += [checksum].pack("H*")
    end
    when "s"
    key_str += source
    when "S"
    if source =~ /\A[0-9a-f]+\z/
    key_str += [source].pack("H*")
    end
    end
    end
    case param_method
    when "s256"
    new_checksum = Digest::SHA256.hexdigest(key_str)
    when "s512"
    new_checksum = Digest::SHA512.hexdigest(key_str)
    when "copy"
    new_checksum = key_str
    when "rev"
    new_checksum = key_str.reverse
    when "REV"
    new_checksum = key_str.scan(/.{1,2}/).reverse.join
    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
    source_file = nil
    opts.on("--debug") { KRGS.debug_mode = true }
    opts.on("-e CODE") { |optvalue| krgs_code = optvalue }
    opts.on("--source SOURCE", "--key SOURCE") { |optvalue| source = optvalue }
    opts.on("--source-file SOURCE_FILE") { |optvalue| source_file = optvalue ; source = File.binread(source_file) }
    opts.parse!(ARGV)
    if krgs_code
    puts KRGS.exec(krgs_code, source)
    else
    raise "ERROR: lack of KRGS code"
    end
    end