Class: SdbDal::Crypto
- Inherits:
-
Object
- Object
- SdbDal::Crypto
- Defined in:
- lib/sdb_dal/crypto.rb
Instance Attribute Summary collapse
-
#iv ⇒ Object
readonly
Returns the value of attribute iv.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
- #decrypt(text) ⇒ Object
- #encrypt(text) ⇒ Object
-
#initialize(options = {}) ⇒ Crypto
constructor
A new instance of Crypto.
Constructor Details
#initialize(options = {}) ⇒ Crypto
Returns a new instance of Crypto.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sdb_dal/crypto.rb', line 8 def initialize(={}) @cipher = OpenSSL::Cipher::Cipher.new("AES-256-CBC") @key=[:cipher_key] if .has_key?(:cipher_key) @iv=[:cipher_iv] if .has_key?(:cipher_iv) unless @key key_dir=[:key_dir] if .has_key?(:key_dir) key_dir ||="/tmp" if File.exists?(key_dir+'/.cipher_key') @key=File.open(key_dir+'/.cipher_key').read else @key = @cipher.random_key() File.open(key_dir+"/.cipher_key",'w').write(@key) end end unless @iv if File.exists?(key_dir+'/.cipher_iv') @iv=File.open(key_dir+'/.cipher_iv').read else @iv = @cipher.random_iv() File.open(key_dir+"/.cipher_iv",'w').write(@iv) end end end |
Instance Attribute Details
#iv ⇒ Object (readonly)
Returns the value of attribute iv.
7 8 9 |
# File 'lib/sdb_dal/crypto.rb', line 7 def iv @iv end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
6 7 8 |
# File 'lib/sdb_dal/crypto.rb', line 6 def key @key end |
Instance Method Details
#decrypt(text) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/sdb_dal/crypto.rb', line 48 def decrypt(text) x=Base64.decode64(text) @cipher.decrypt(@key,@iv) @cipher.key = @key @cipher.iv = @iv d = @cipher.update(x) d << @cipher.final() return d end |
#encrypt(text) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sdb_dal/crypto.rb', line 37 def encrypt(text) @cipher.encrypt(@key,@iv) @cipher.key=@key @cipher.iv = @iv e = @cipher.update(text) e << @cipher.final() Base64.encode64(e)#.chomp end |