Class: RCrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/rcrypt.rb

Defined Under Namespace

Classes: CryptException

Class Method Summary collapse

Class Method Details

.decrypt(plaintext, priv_key = @private_key) ⇒ Object

Raises:



34
35
36
37
# File 'lib/rcrypt.rb', line 34

def decrypt(plaintext, priv_key = @private_key)
  raise CryptException.new("No private key specified.") unless priv_key
  key(priv_key).private_decrypt Base64.decode64(plaintext)
end

.encrypt(plaintext, pub_key = @public_key) ⇒ Object

Raises:



39
40
41
42
# File 'lib/rcrypt.rb', line 39

def encrypt(plaintext, pub_key = @public_key)
  raise CryptException.new("No public key specified.") unless pub_key
  Base64.encode64 key(pub_key).public_encrypt(plaintext)
end

.generate_key_pairObject



27
28
29
30
31
32
# File 'lib/rcrypt.rb', line 27

def generate_key_pair
  return @key_pair if @key_pair
  pair = OpenSSL::PKey::RSA.generate(2048)
  @private_key, @public_key = [pair.to_pem, pair.public_key.to_pem]
  @key_pair = {:private => @private_key, :public => @public_key}
end

.key(key_text) ⇒ Object



44
45
46
# File 'lib/rcrypt.rb', line 44

def key(key_text)
  OpenSSL::PKey::RSA.new key_text
end

.reset!Object



23
24
25
# File 'lib/rcrypt.rb', line 23

def reset!
  @key_pair = @private_key = @public_key = nil
end