Class: TarvitHelpers::SimpleCrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/tarvit-helpers/modules/simple_crypt.rb

Constant Summary collapse

TYPE =
"aes-256-cbc"

Instance Method Summary collapse

Constructor Details

#initialize(secret_key) ⇒ SimpleCrypt

Returns a new instance of SimpleCrypt.



9
10
11
12
# File 'lib/tarvit-helpers/modules/simple_crypt.rb', line 9

def initialize(secret_key)
  @ciper = OpenSSL::Cipher::Cipher.new(TYPE)
  @secret_key = secret_key.to_s
end

Instance Method Details

#decrypt(code) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/tarvit-helpers/modules/simple_crypt.rb', line 22

def decrypt(code)
  hash = Base64.decode64(code)
  @ciper.decrypt
  @ciper.key = Digest::SHA1.hexdigest(@secret_key)
  d = @ciper.update(hash)
  d << @ciper.final
end

#encrypt(string) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/tarvit-helpers/modules/simple_crypt.rb', line 14

def encrypt(string)
  @ciper.encrypt
  @ciper.key = Digest::SHA1.hexdigest(@secret_key)
  res = @ciper.update(string)
  res << @ciper.final
  Base64.encode64(res)
end