Class: SSHData::PublicKey::RSA

Inherits:
Base
  • Object
show all
Defined in:
lib/ssh_data/public_key/rsa.rb

Constant Summary collapse

ALGO_DIGESTS =
{
  ALGO_RSA          => OpenSSL::Digest::SHA1,
  ALGO_RSA_SHA2_256 => OpenSSL::Digest::SHA256,
  ALGO_RSA_SHA2_512 => OpenSSL::Digest::SHA512
}

Instance Attribute Summary collapse

Attributes inherited from Base

#algo

Instance Method Summary collapse

Methods inherited from Base

#fingerprint, #openssh, #sign

Constructor Details

#initialize(algo:, e:, n:) ⇒ RSA

Returns a new instance of RSA.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ssh_data/public_key/rsa.rb', line 12

def initialize(algo:, e:, n:)
  unless algo == ALGO_RSA
    raise DecodeError, "bad algorithm: #{algo.inspect}"
  end

  @algo = algo
  @e = e
  @n = n

  @openssl = OpenSSL::PKey::RSA.new(asn1.to_der)

  super(algo: algo)
end

Instance Attribute Details

#eObject (readonly)

Returns the value of attribute e.



4
5
6
# File 'lib/ssh_data/public_key/rsa.rb', line 4

def e
  @e
end

#nObject (readonly)

Returns the value of attribute n.



4
5
6
# File 'lib/ssh_data/public_key/rsa.rb', line 4

def n
  @n
end

#opensslObject (readonly)

Returns the value of attribute openssl.



4
5
6
# File 'lib/ssh_data/public_key/rsa.rb', line 4

def openssl
  @openssl
end

Instance Method Details

#==(other) ⇒ Object

Is this public key equal to another public key?

other - Another SSHData::PublicKey::Base instance to compare with.

Returns boolean.



59
60
61
# File 'lib/ssh_data/public_key/rsa.rb', line 59

def ==(other)
  super && other.e == e && other.n == n
end

#rfc4253Object

RFC4253 binary encoding of the public key.

Returns a binary String.



46
47
48
49
50
51
52
# File 'lib/ssh_data/public_key/rsa.rb', line 46

def rfc4253
  Encoding.encode_fields(
    [:string, algo],
    [:mpint,  e],
    [:mpint,  n]
  )
end

#verify(signed_data, signature) ⇒ Object

Verify an SSH signature.

signed_data - The String message that the signature was calculated over. signature - The binarty String signature with SSH encoding.

Returns boolean.



32
33
34
35
36
37
38
39
40
41
# File 'lib/ssh_data/public_key/rsa.rb', line 32

def verify(signed_data, signature)
  sig_algo, raw_sig, _ = Encoding.decode_signature(signature)
  digest = ALGO_DIGESTS[sig_algo]

  if digest.nil?
    raise DecodeError, "bad signature algorithm: #{sig_algo.inspect}"
  end

  openssl.verify(digest.new, raw_sig, signed_data)
end