Class: Nexus::Cipher
- Inherits:
-
Object
- Object
- Nexus::Cipher
- Defined in:
- lib/nexus/cipher.rb
Instance Method Summary collapse
- #decrypt(data) ⇒ Object
- #encrypt(data) ⇒ Object
-
#initialize(pass, token = nil) ⇒ Cipher
constructor
A new instance of Cipher.
- #iv ⇒ Object
- #iv=(iv) ⇒ Object
- #token ⇒ Object
Constructor Details
#initialize(pass, token = nil) ⇒ Cipher
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/nexus/cipher.rb', line 7 def initialize(pass, token = nil) @token = Base64.strict_decode64(token) if token @token ||= OpenSSL::Random.random_bytes(32) iter = 20_000 key_len = 32 @key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(pass, @token, iter, key_len) end |
Instance Method Details
#decrypt(data) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/nexus/cipher.rb', line 45 def decrypt(data) c = cipher c.decrypt c.key = @key c.iv = @iv c.update(Base64.strict_decode64(data)) + c.final end |
#encrypt(data) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/nexus/cipher.rb', line 37 def encrypt(data) c = cipher c.encrypt c.key = @key @iv = c.random_iv Base64.strict_encode64(c.update(data) + c.final) end |
#iv ⇒ Object
29 30 31 |
# File 'lib/nexus/cipher.rb', line 29 def iv Base64.strict_encode64(@iv) if @iv end |
#iv=(iv) ⇒ Object
33 34 35 |
# File 'lib/nexus/cipher.rb', line 33 def iv=(iv) @iv = Base64.strict_decode64(iv) end |
#token ⇒ Object
25 26 27 |
# File 'lib/nexus/cipher.rb', line 25 def token Base64.strict_encode64(@token) end |