class SSHData::PublicKey::RSA

Constants

ALGO_DIGESTS

Attributes

e[R]
n[R]
openssl[R]

Public Class Methods

new(algo:, e:, n:) click to toggle source
Calls superclass method SSHData::PublicKey::Base::new
# 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

Public Instance Methods

==(other) click to toggle source

Is this public key equal to another public key?

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

Returns boolean.

Calls superclass method SSHData::PublicKey::Base#==
# File lib/ssh_data/public_key/rsa.rb, line 59
def ==(other)
  super && other.e == e && other.n == n
end
rfc4253() click to toggle source

RFC4253 binary encoding of the public key.

Returns a binary String.

# 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) click to toggle source

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.

# 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

Private Instance Methods

asn1() click to toggle source
# File lib/ssh_data/public_key/rsa.rb, line 65
def asn1
  OpenSSL::ASN1::Sequence.new([
    OpenSSL::ASN1::Sequence.new([
      OpenSSL::ASN1::ObjectId.new("rsaEncryption"),
      OpenSSL::ASN1::Null.new(nil),
    ]),
    OpenSSL::ASN1::BitString.new(OpenSSL::ASN1::Sequence.new([
      OpenSSL::ASN1::Integer.new(n),
      OpenSSL::ASN1::Integer.new(e),
    ]).to_der),
  ])
end