Class: Metafusion::Crypto::PrivateKey
- Inherits:
-
Object
- Object
- Metafusion::Crypto::PrivateKey
- Defined in:
- lib/metafusion/crypto/base.rb
Overview
Metafusion::Crypto::PrivateKey
provides class that wraps the boilerplate private key encryption API from the Ruby standard library OpenSSL implementation.
Usage:
include Metafusion::Crypto
pkey = PrivateKey.new('mypassphrase')
original_text = 'Yo yo yo. What up dog?'
crypted_text = pkey.encrypt(original_text)
plain_text = pkey.decrypt(crypted_text)
puts "Let's roll and celebrate - it worked" if original_text == plain_text
Class Method Summary collapse
-
.from_file(key_file = 'rsa_key') ⇒ Object
Read in private key from file.
Instance Method Summary collapse
-
#decrypt(text) ⇒ Object
Decrypts given
text
using key instance. -
#encrypt(text) ⇒ Object
Encrypts given
text
using key instance. -
#initialize(raw_passphrase, cipher = "aes-256-cbc") ⇒ PrivateKey
constructor
Creates
PrivateKey
instance givenraw_passphrase
andcipher
.
Constructor Details
#initialize(raw_passphrase, cipher = "aes-256-cbc") ⇒ PrivateKey
Creates PrivateKey
instance given raw_passphrase
and cipher
. The default cipher is AES-256-CBC.
110 111 112 113 114 |
# File 'lib/metafusion/crypto/base.rb', line 110 def initialize(raw_passphrase, cipher = "aes-256-cbc") @encrypt_cipher = OpenSSL::Cipher::Cipher.new(cipher) @decrypt_cipher = OpenSSL::Cipher::Cipher.new(cipher) @key = Digest::SHA1.hexdigest(raw_passphrase) end |
Class Method Details
.from_file(key_file = 'rsa_key') ⇒ Object
Read in private key from file
102 103 104 |
# File 'lib/metafusion/crypto/base.rb', line 102 def from_file(key_file = 'rsa_key') return self.new(File.read(key_file)) end |
Instance Method Details
#decrypt(text) ⇒ Object
Decrypts given text
using key instance
125 126 127 128 129 130 |
# File 'lib/metafusion/crypto/base.rb', line 125 def decrypt(text) @decrypt_cipher.decrypt(@key) s = @decrypt_cipher.update(Base64.decode64(text)) s << @decrypt_cipher.final s end |
#encrypt(text) ⇒ Object
Encrypts given text
using key instance
117 118 119 120 121 122 |
# File 'lib/metafusion/crypto/base.rb', line 117 def encrypt(text) @encrypt_cipher.encrypt(@key) s = @encrypt_cipher.update(text) s << @encrypt_cipher.final Base64.encode64(s) end |