Class: Gibberish::RSA
- Inherits:
-
Object
- Object
- Gibberish::RSA
- Defined in:
- lib/gibberish/rsa.rb
Overview
This wraps the OpenSSL RSA functions Simply instantiate with a public key or private key
cipher = Gibberish::RSA.new(private_key)
enc = cipher.encrypt(data)
dec = cipher.decrypt(enc)
cipher = Gibberish::RSA(public_key)
cipher.decrypt(enc)
You can also generate a keypair using Gibberish::RSA.generate_keypair
kp = Gibberish::RSA.generate_keypair(4096)
kp.public_key #=> Outputs a Base64 encoded public key
kp.private_key #=> Outputs the Base64 pem
KeyPair will hand back the private key when passed to the RSA class.
cipher = Gibberish::RSA.new(kp)
OpenSSL CLI Interop
openssl rsautl -decrypt -inkey [pem_file] -in [BinaryEncodedCryptedFile]
or if you're using the default base64 output, you'll need to decode that first
openssl enc -d -base64 -in [gibberish.crypted] | \
openssl rsautl -decrypt -inkey [pem_file]
Defined Under Namespace
Classes: KeyPair
Class Method Summary collapse
-
.generate_keypair(bits = 2048) ⇒ Object
Generate an RSA keypair - defaults to 2048 bits.
Instance Method Summary collapse
-
#decrypt(data, opts = {}) ⇒ Object
Decrypt data using the key.
-
#encrypt(data, opts = {}) ⇒ Object
Encrypt data using the key.
-
#initialize(key, passphrase = nil) ⇒ RSA
constructor
Expects a public key at the minumum.
Constructor Details
#initialize(key, passphrase = nil) ⇒ RSA
Expects a public key at the minumum
78 79 80 |
# File 'lib/gibberish/rsa.rb', line 78 def initialize(key, passphrase=nil) @key = OpenSSL::PKey::RSA.new(key.to_s, passphrase) end |
Class Method Details
Instance Method Details
#decrypt(data, opts = {}) ⇒ Object
Decrypt data using the key
102 103 104 105 106 107 108 109 |
# File 'lib/gibberish/rsa.rb', line 102 def decrypt(data, opts={}) data = data.to_s raise "No private key set!" unless @key.private? unless opts[:binary] data = Base64.decode64(data) end @key.private_decrypt(data) end |
#encrypt(data, opts = {}) ⇒ Object
Encrypt data using the key
87 88 89 90 91 92 93 94 95 |
# File 'lib/gibberish/rsa.rb', line 87 def encrypt(data, opts={}) data = data.to_s enc = @key.public_encrypt(data) if opts[:binary] enc else Base64.encode64(enc) end end |