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

Inherits:
HrrRbSsh::Algorithm::Publickey show all
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

Modules: PublicKeyBlob, Signature

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from SubclassWithoutPreferenceListable

#[], #inherited, #list_supported

Constructor Details

#initialize(arg) ⇒ SshRsa

Returns a new instance of SshRsa.



13
14
15
16
17
18
19
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 13

def initialize arg
  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



21
22
23
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 21

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

#new_by_public_key_blob(public_key_blob) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 25

def new_by_public_key_blob public_key_blob
  public_key_blob_h = PublicKeyBlob.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



49
50
51
52
53
54
55
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 49

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

#to_pemObject



36
37
38
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 36

def to_pem
  @publickey.public_key.to_pem
end

#to_public_key_blobObject



40
41
42
43
44
45
46
47
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 40

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.encode(public_key_blob_h)
end

#verify(signature, signature_blob) ⇒ Object



57
58
59
60
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb', line 57

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