Module: Tinypass::SecurityUtils

Extended by:
SecurityUtils
Included in:
SecurityUtils
Defined in:
lib/tinypass/builder/security_utils.rb

Constant Summary collapse

DELIM =
'~~~'

Instance Method Summary collapse

Instance Method Details

#decrypt(key, data) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tinypass/builder/security_utils.rb', line 23

def decrypt(key, data)
  cipher_text, hmac_text = data.split(DELIM)
  check_hmac!(key, cipher_text, hmac_text) if hmac_text
  key = prepare_key(key)
  cipher_text = url_desafe(cipher_text)

  cipher = OpenSSL::Cipher.new('AES-256-ECB')
  cipher.decrypt
  cipher.key = key
  cipher.update(cipher_text) + cipher.final
end

#encrypt(key, data) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tinypass/builder/security_utils.rb', line 10

def encrypt(key, data)
  original_key = key
  key = prepare_key(key)

  cipher = OpenSSL::Cipher.new('AES-256-ECB')
  cipher.encrypt
  cipher.key = key
  encrypted = cipher.update(data) + cipher.final

  safe = url_ensafe(encrypted)
  safe + DELIM + hash_hmac_sha256(original_key, safe)
end

#hash_hmac_sha256(key, data) ⇒ Object



35
36
37
38
39
# File 'lib/tinypass/builder/security_utils.rb', line 35

def hash_hmac_sha256(key, data)
  digest = OpenSSL::Digest::Digest.new('sha256')
  hmac = OpenSSL::HMAC.digest(digest, key, data)
  url_ensafe(hmac)
end