Class: Encrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/ttoken/encrypt.rb

Constant Summary collapse

CIPHER =
OpenSSL::Cipher.new('aes-256-cbc')

Instance Method Summary collapse

Instance Method Details

#decrypt_pin(pin) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ttoken/encrypt.rb', line 14

def decrypt_pin(pin)
  if is_decryptable?(pin)
    cipher = CIPHER.decrypt
    cipher.key = encryption_key
    s = [pin].pack("H*").unpack("C*").pack("c*")
    begin
      cipher.update(s) + cipher.final
    rescue StandardError => e
    raise "\n Problem in decrypting password. To resolve the issue encrypt and save the password again using command # ttoken --encrypt. \n Error: #{e.message}"
    end
  end
end

#encrypt_pin(pin) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/ttoken/encrypt.rb', line 5

def encrypt_pin(pin)
  if is_encryptable?(pin)
    cipher = CIPHER.encrypt
    cipher.key = encryption_key
    s = cipher.update(pin) + cipher.final
    s.unpack('H*')[0].upcase
  end
end

#encryption_keyObject



35
36
37
38
39
40
41
42
43
# File 'lib/ttoken/encrypt.rb', line 35

def encryption_key
  if File.exist?(ENCRYPTION_KEY_FILE)
    Store.retrive_encryption_key
  else
    key = CIPHER.random_key
    Store.save_encryption_key(key)
    key
  end
end

#is_decryptable?(str) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/ttoken/encrypt.rb', line 31

def is_decryptable?(str)
  !encryption_key.nil? && !str.nil?
end

#is_encryptable?(str) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/ttoken/encrypt.rb', line 27

def is_encryptable?(str)
  !encryption_key.nil? && !str.nil?
end