Class: Zold::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/key.rb

Overview

A key

Constant Summary collapse

ROOT =

Public key of the root wallet

Key.new(file: File.expand_path(File.join(File.dirname(__FILE__), '../../resources/root.pub')))

Instance Method Summary collapse

Constructor Details

#initialize(file: nil, text: nil) ⇒ Key

Returns a new instance of Key.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/zold/key.rb', line 18

def initialize(file: nil, text: nil)
  @body = lambda do
    unless file.nil?
      path = File.expand_path(file)
      raise "Can't find RSA key at #{file} (#{path})" unless File.exist?(path)
      return File.read(path)
    end
    unless text.nil?
      return text if text.start_with?('-----')
      return [
        '-----BEGIN PUBLIC KEY-----',
        text.gsub(/(?<=\G.{64})/, "\n"),
        '-----END PUBLIC KEY-----'
      ].join("\n")
    end
    raise 'Either file or text must be set'
  end
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
# File 'lib/zold/key.rb', line 44

def ==(other)
  to_s == other.to_s
end

#root?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/zold/key.rb', line 40

def root?
  to_s == ROOT.to_s
end

#sign(text) ⇒ Object



56
57
58
# File 'lib/zold/key.rb', line 56

def sign(text)
  Base64.encode64(rsa.sign(OpenSSL::Digest.new('SHA256'), text)).delete("\n")
end

#to_pubObject



52
53
54
# File 'lib/zold/key.rb', line 52

def to_pub
  to_s.delete("\n").gsub(/-{5}[ A-Z]+-{5}/, '')
end

#to_sObject



48
49
50
# File 'lib/zold/key.rb', line 48

def to_s
  rsa.to_s.strip
end

#verify(signature, text) ⇒ Object



60
61
62
# File 'lib/zold/key.rb', line 60

def verify(signature, text)
  rsa.verify(OpenSSL::Digest.new('SHA256'), Base64.decode64(signature), text)
end