Class: RSA::KeyPair
- Inherits:
-
Object
- Object
- RSA::KeyPair
- Defined in:
- lib/rsa/openssl.rb,
lib/rsa/key_pair.rb
Overview
An RSA key pair.
Refer to PKCS #1 v2.1, section 3, pp. 6-8.
Instance Attribute Summary collapse
-
#private_key ⇒ Key
(also: #private)
readonly
The RSA private key.
-
#public_key ⇒ Key
(also: #public)
readonly
The RSA public key.
Class Method Summary collapse
-
.generate(bits, exponent = 65537) ⇒ KeyPair
Generates a new RSA key pair of length ‘bits`.
Instance Method Summary collapse
-
#bitsize ⇒ Integer
(also: #size)
Returns the bit size of this key pair.
-
#bytesize ⇒ Integer
Returns the byte size of this key pair.
-
#decrypt(ciphertext, options = {}) ⇒ Object
Decrypts the given ‘ciphertext` using the private key from this key pair.
-
#encrypt(plaintext, options = {}) ⇒ Object
Encrypts the given ‘plaintext` using the public key from this key pair.
-
#initialize(private_key, public_key, options = {}) ⇒ KeyPair
constructor
Initializes a new key pair.
-
#modulus ⇒ Integer
(also: #n)
Returns the RSA modulus for this key pair.
-
#private_key? ⇒ Boolean
(also: #private?)
Returns ‘true` if this key pair contains a private key.
-
#public_key? ⇒ Boolean
(also: #public?)
Returns ‘true` if this key pair contains a public key.
-
#sign(plaintext, options = {}) ⇒ Object
Signs the given ‘plaintext` using the private key from this key pair.
-
#to_hash ⇒ Hash
Returns a hash table representation of this key pair.
-
#to_openssl ⇒ OpenSSL::PKey::RSA
Returns this key pair as an ‘OpenSSL::PKey::RSA` instance.
-
#valid? ⇒ Boolean
Returns ‘true` if this is a valid RSA key pair according to PKCS #1.
-
#verify(signature, plaintext, options = {}) ⇒ Boolean
Verifies the given ‘signature` using the public key from this key pair.
Constructor Details
#initialize(private_key, public_key, options = {}) ⇒ KeyPair
Initializes a new key pair.
30 31 32 33 34 |
# File 'lib/rsa/key_pair.rb', line 30 def initialize(private_key, public_key, = {}) @private_key = private_key @public_key = public_key @options = .dup end |
Instance Attribute Details
#private_key ⇒ Key (readonly) Also known as: private
The RSA private key.
14 15 16 |
# File 'lib/rsa/key_pair.rb', line 14 def private_key @private_key end |
#public_key ⇒ Key (readonly) Also known as: public
The RSA public key.
21 22 23 |
# File 'lib/rsa/key_pair.rb', line 21 def public_key @public_key end |
Class Method Details
.generate(bits, exponent = 65537) ⇒ KeyPair
Generates a new RSA key pair of length ‘bits`.
By default, the public exponent will be 65537 (0x10001) as recommended by PKCS #1.
16 17 18 19 20 |
# File 'lib/rsa/openssl.rb', line 16 def self.generate(bits, exponent = 65537) pkey = ::OpenSSL::PKey::RSA.generate(bits.to_i, exponent.to_i) n, d, e = pkey.n.to_i, pkey.d.to_i, pkey.e.to_i self.new(Key.new(n, d), Key.new(n, e)) end |
Instance Method Details
#bitsize ⇒ Integer Also known as: size
Returns the bit size of this key pair.
76 77 78 |
# File 'lib/rsa/key_pair.rb', line 76 def bitsize Math.log2(modulus).ceil end |
#bytesize ⇒ Integer
Returns the byte size of this key pair.
68 69 70 |
# File 'lib/rsa/key_pair.rb', line 68 def bytesize Math.log256(modulus).ceil end |
#decrypt(ciphertext, options = {}) ⇒ Integer #decrypt(ciphertext, options = {}) ⇒ String
Decrypts the given ‘ciphertext` using the private key from this key pair.
144 145 146 147 148 149 150 151 |
# File 'lib/rsa/key_pair.rb', line 144 def decrypt(ciphertext, = {}) case ciphertext when Integer then decrypt_integer(ciphertext, ) when String then PKCS1.i2osp(decrypt_integer(PKCS1.os2ip(ciphertext), )) when StringIO, IO then PKCS1.i2osp(decrypt_integer(PKCS1.os2ip(ciphertext.read), )) else raise ArgumentError, ciphertext.inspect # FIXME end end |
#encrypt(plaintext, options = {}) ⇒ Integer #encrypt(plaintext, options = {}) ⇒ String
Encrypts the given ‘plaintext` using the public key from this key pair.
118 119 120 121 122 123 124 125 |
# File 'lib/rsa/key_pair.rb', line 118 def encrypt(plaintext, = {}) case plaintext when Integer then encrypt_integer(plaintext, ) when String then PKCS1.i2osp(encrypt_integer(PKCS1.os2ip(plaintext), )) when StringIO, IO then PKCS1.i2osp(encrypt_integer(PKCS1.os2ip(plaintext.read), )) else raise ArgumentError, plaintext.inspect # FIXME end end |
#modulus ⇒ Integer Also known as: n
Returns the RSA modulus for this key pair.
85 86 87 |
# File 'lib/rsa/key_pair.rb', line 85 def modulus private_key ? private_key.modulus : public_key.modulus end |
#private_key? ⇒ Boolean Also known as: private?
Returns ‘true` if this key pair contains a private key.
40 41 42 |
# File 'lib/rsa/key_pair.rb', line 40 def private_key? !!private_key end |
#public_key? ⇒ Boolean Also known as: public?
Returns ‘true` if this key pair contains a public key.
49 50 51 |
# File 'lib/rsa/key_pair.rb', line 49 def public_key? !!public_key end |
#sign(plaintext, options = {}) ⇒ Integer #sign(plaintext, options = {}) ⇒ String
Signs the given ‘plaintext` using the private key from this key pair.
169 170 171 172 173 174 175 176 |
# File 'lib/rsa/key_pair.rb', line 169 def sign(plaintext, = {}) case plaintext when Integer then sign_integer(plaintext, ) when String then PKCS1.i2osp(sign_integer(PKCS1.os2ip(plaintext), )) when StringIO, IO then PKCS1.i2osp(sign_integer(PKCS1.os2ip(plaintext.read), )) else raise ArgumentError, plaintext.inspect # FIXME end end |
#to_hash ⇒ Hash
Returns a hash table representation of this key pair.
97 98 99 |
# File 'lib/rsa/key_pair.rb', line 97 def to_hash {:n => modulus, :d => private_key ? private_key.exponent : nil, :e => public_key ? public_key.exponent : nil} end |
#to_openssl ⇒ OpenSSL::PKey::RSA
Returns this key pair as an ‘OpenSSL::PKey::RSA` instance.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rsa/openssl.rb', line 26 def to_openssl @openssl_pkey ||= begin pkey = ::OpenSSL::PKey::RSA.new pkey.n = private_key.modulus if private_key? pkey.e = private_key.exponent if private_key? pkey.n ||= public_key.modulus if public_key? pkey.d = public_key.exponent if public_key? pkey end end |
#valid? ⇒ Boolean
Returns ‘true` if this is a valid RSA key pair according to PKCS #1.
60 61 62 |
# File 'lib/rsa/key_pair.rb', line 60 def valid? private_key.valid? && public_key.valid? end |
#verify(signature, plaintext, options = {}) ⇒ Boolean #verify(signature, plaintext, options = {}) ⇒ Boolean
Verifies the given ‘signature` using the public key from this key pair.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/rsa/key_pair.rb', line 199 def verify(signature, plaintext, = {}) signature = case signature when Integer then signature when String then PKCS1.os2ip(signature) when StringIO, IO then PKCS1.os2ip(signature.read) else raise ArgumentError, signature.inspect # FIXME end plaintext = case plaintext when Integer then plaintext when String then PKCS1.os2ip(plaintext) when StringIO, IO then PKCS1.os2ip(plaintext.read) else raise ArgumentError, plaintext.inspect # FIXME end verify_integer(signature, plaintext, ) end |