Class: Lark::Cipher

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

Constant Summary collapse

CIPHER =
'AES-256-CBC'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#keyObject (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(message)
  cipher = OpenSSL::Cipher.new(CIPHER)
  cipher.decrypt

  decode64 = Base64.urlsafe_decode64(message)
  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(message)
  cipher = OpenSSL::Cipher.new(CIPHER)
  cipher.encrypt
  cipher.key = key
  iv = cipher.random_iv
  Base64.urlsafe_encode64(iv + cipher.update(message) + cipher.final)
end