Sha256: 39edcf430de3163db196a71d6f319d44c6acfbf9b05719a370281f6345c46ccb

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'digest'
require 'highline/import'
require 'psych'

module H
  class Generator
    CONF_PATH = File.join(File.expand_path('~'), '.h')

    def initialize(options = {})
      unless File.exists?(CONF_PATH)
        File.open(CONF_PATH, 'w') do |f|
          Psych.dump({ 'encryption' => 'SHA1',
                       'static_key' => 'foobar',
                       'max_length' => 15,
                       'radix'      => 36 }, f)
        end

        File.chmod(0600, CONF_PATH)
      end

      @params = Psych.load_file(CONF_PATH)
      @params.update(options)
      @params.freeze
    end

    def input(message)
      m = message + @params['static_key']
      d = cryptographic_hash(m)
      truncate codage(d)
    end

    def prompt
      input ask('Message: ') {|q| q.echo = '*' }
    end

    protected

    def codage(hexdigest)
      hexdigest.to_i(16).to_s(@params['radix'])
    end

    def cryptographic_hash(message)
      Digest.class_eval(@params['encryption']).hexdigest(message)
    end

    def truncate(string)
      string[0, @params['max_length']]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
h-1.1.1 lib/h.rb