Class: HrrRbSsh::Algorithm::Publickey::SshRsa

Inherits:
HrrRbSsh::Algorithm::Publickey show all
Includes:
Loggable
Defined in:
lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb,
lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa/signature.rb,
lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa/public_key_blob.rb

Defined Under Namespace

Classes: PublicKeyBlob, Signature

Constant Summary collapse

NAME =
'ssh-rsa'
DIGEST =
'sha1'

Instance Attribute Summary

Attributes included from Loggable

#log_key, #logger

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn

Methods included from SubclassWithoutPreferenceListable

#[], #inherited, #list_supported

Constructor Details

#initialize(arg, logger: nil) ⇒ SshRsa

Returns a new instance of SshRsa.



15
16
17
18
19
20
21
22
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 15

def initialize arg, logger: nil
  self.logger = logger
  begin
    new_by_key_str arg
  rescue OpenSSL::PKey::RSAError
    new_by_public_key_blob arg
  end
end

Instance Method Details

#new_by_key_str(key_str) ⇒ Object



24
25
26
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 24

def new_by_key_str key_str
  @publickey = OpenSSL::PKey::RSA.new(key_str)
end

#new_by_public_key_blob(public_key_blob) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 28

def new_by_public_key_blob public_key_blob
  public_key_blob_h = PublicKeyBlob.new(logger: logger).decode public_key_blob
  @publickey = OpenSSL::PKey::RSA.new
  if @publickey.respond_to?(:set_key)
    @publickey.set_key public_key_blob_h[:'n'], public_key_blob_h[:'e'], nil
  else
    @publickey.n = public_key_blob_h[:'n']
    @publickey.e = public_key_blob_h[:'e']
  end
end

#sign(signature_blob) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 52

def sign signature_blob
  signature_h = {
    :'public key algorithm name' => self.class::NAME,
    :'signature blob'            => @publickey.sign(self.class::DIGEST, signature_blob),
  }
  Signature.new(logger: logger).encode signature_h
end

#to_pemObject



39
40
41
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 39

def to_pem
  @publickey.public_key.to_pem
end

#to_public_key_blobObject



43
44
45
46
47
48
49
50
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 43

def to_public_key_blob
  public_key_blob_h = {
    :'public key algorithm name' => self.class::NAME,
    :'e'                         => @publickey.e.to_i,
    :'n'                         => @publickey.n.to_i,
  }
  PublicKeyBlob.new(logger: logger).encode public_key_blob_h
end

#verify(signature, signature_blob) ⇒ Object



60
61
62
63
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 60

def verify signature, signature_blob
  signature_h = Signature.new(logger: logger).decode signature
  signature_h[:'public key algorithm name'] == self.class::NAME && @publickey.verify(self.class::DIGEST, signature_h[:'signature blob'], signature_blob)
end