Class: SSHData::PublicKey::RSA
- 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
-
#e ⇒ Object
readonly
Returns the value of attribute e.
-
#n ⇒ Object
readonly
Returns the value of attribute n.
-
#openssl ⇒ Object
readonly
Returns the value of attribute openssl.
Attributes inherited from Base
Instance Method Summary collapse
-
#==(other) ⇒ Object
Is this public key equal to another public key?.
-
#initialize(algo:, e:, n:) ⇒ RSA
constructor
A new instance of RSA.
-
#rfc4253 ⇒ Object
RFC4253 binary encoding of the public key.
-
#verify(signed_data, signature) ⇒ Object
Verify an SSH signature.
Methods inherited from Base
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
#e ⇒ Object (readonly)
Returns the value of attribute e.
4 5 6 |
# File 'lib/ssh_data/public_key/rsa.rb', line 4 def e @e end |
#n ⇒ Object (readonly)
Returns the value of attribute n.
4 5 6 |
# File 'lib/ssh_data/public_key/rsa.rb', line 4 def n @n end |
#openssl ⇒ Object (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.
65 66 67 |
# File 'lib/ssh_data/public_key/rsa.rb', line 65 def ==(other) super && other.e == e && other.n == n end |
#rfc4253 ⇒ Object
RFC4253 binary encoding of the public key.
Returns a binary String.
52 53 54 55 56 57 58 |
# File 'lib/ssh_data/public_key/rsa.rb', line 52 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 binary String signature with SSH encoding.
Returns boolean.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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 # OpenSSH compatibility: if a the number of bytes in the signature is less than the number of bytes of the RSA # modulus, prepend the signature with zeros. # See https://github.com/openssh/openssh-portable/blob/ac383f3a5c6f529a2e8a5bc44af79a08c7da294e/ssh-rsa.c#L531 difference = n.num_bytes - raw_sig.bytesize raw_sig = "\0" * difference + raw_sig if difference.positive? openssl.verify(digest.new, raw_sig, signed_data) end |