Class: Lark::Cipher
- Inherits:
-
Object
- Object
- Lark::Cipher
- Defined in:
- lib/lark/cipher.rb
Constant Summary collapse
- CIPHER =
'AES-256-CBC'.freeze
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
- #decrypt(message) ⇒ Object
- #encrypt(message) ⇒ Object
-
#initialize(key) ⇒ Cipher
constructor
A new instance of Cipher.
Constructor Details
#initialize(key) ⇒ Cipher
Returns a new instance of Cipher.
10 11 12 |
# File 'lib/lark/cipher.rb', line 10 def initialize(key) @key = Digest::SHA256.digest(key) end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
6 7 8 |
# File 'lib/lark/cipher.rb', line 6 def key @key end |
Instance Method Details
#decrypt(message) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/lark/cipher.rb', line 22 def decrypt() cipher = OpenSSL::Cipher.new(CIPHER) cipher.decrypt decode64 = Base64.urlsafe_decode64() cipher.key = key cipher.iv = decode64[0..15] ecrypted = decode64[16..-1] cipher.update(ecrypted) + cipher.final end |
#encrypt(message) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/lark/cipher.rb', line 14 def encrypt() cipher = OpenSSL::Cipher.new(CIPHER) cipher.encrypt cipher.key = key iv = cipher.random_iv Base64.urlsafe_encode64(iv + cipher.update() + cipher.final) end |