Module: Gooddata::Crypto

Defined in:
lib/gooddata/crypto.rb,
lib/gooddata/crypto/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.decrypt(database64, key) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gooddata/crypto.rb', line 23

def decrypt(database64, key)
  return '' if key.nil? || key.empty?

  data = Base64.decode64(database64)

  cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
  cipher.decrypt
  cipher.key = cipher_key = Digest::SHA256.digest(key)
  random_iv = data[0..15] # extract iv from first 16 bytes
  data = data[16..data.size - 1]
  cipher.iv = Digest::SHA256.digest(random_iv + cipher_key)[0..15]
  begin
    decrypted = cipher.update(data)
    decrypted << cipher.final
  rescue
    puts 'Error'
    return nil
  end

  decrypted
end

.encrypt(data, key) ⇒ Object

encrypts data with the given key. returns a binary data with the unhashed random iv in the first 16 bytes



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

def encrypt(data, key)
  cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
  cipher.encrypt
  cipher.key = key = Digest::SHA256.digest(key)
  random_iv = cipher.random_iv
  cipher.iv = Digest::SHA256.digest(random_iv + key)[0..15]
  encrypted = cipher.update(data)
  encrypted << cipher.final
  # add unhashed iv to front of encrypted data

  Base64.encode64(random_iv + encrypted)
end