Class: EncryptedCredentials::Coder

Inherits:
Object
  • Object
show all
Defined in:
lib/encrypted_credentials/coder.rb

Class Method Summary collapse

Class Method Details

.decrypt(data_bin, key_hex) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/encrypted_credentials/coder.rb', line 12

def self.decrypt(data_bin, key_hex)
  encrypted_data, iv, auth_tag = data_bin.split("--").map { |v| Base64.strict_decode64(v) }
  key = [key_hex].pack('H*')

  cipher_type =
    case key.bytes.length
    when 16
      'aes-128-gcm'
    when 32
      'aes-256-gcm'
    else
      raise "Wrong key length: #{key.bytes.length}"
    end

  raise "Unauthenticated message" if auth_tag.nil? || auth_tag.bytes.length != 16

  cipher = OpenSSL::Cipher.new(cipher_type)
  cipher.decrypt
  cipher.key = key
  cipher.iv = iv
  cipher.auth_tag = auth_tag
  cipher.auth_data = ""

  decrypted_data = cipher.update(encrypted_data)
  decrypted_data << cipher.final

  if decrypted_data.bytes[0..1] == [4, 8]
    Marshal.load(decrypted_data)
  else
    decrypted_data
  end
end

.encrypt(data_bin, key_hex, use_marshal = true) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/encrypted_credentials/coder.rb', line 59

def self.encrypt(data_bin, key_hex, use_marshal = true)
  key = [key_hex].pack('H*')

  cipher_type =
    case key.bytes.length
    when 16
      'aes-128-gcm'
    when 32
      'aes-256-gcm'
    else
      raise "Wrong key length: #{key.bytes.length}"
    end

  cipher = OpenSSL::Cipher.new(cipher_type)
  cipher.encrypt
  cipher.key = key
  iv = cipher.random_iv
  cipher.auth_data = ""

  data = data_bin
  data = Marshal.dump(data) if use_marshal
  encrypted_data = cipher.update(data)
  encrypted_data << cipher.final

  [encrypted_data, iv, cipher.auth_tag].map{ |x| Base64.strict_encode64(x) }.join('--')
end

.generate_key_hex(cipher = 'aes-128-gcm') ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/encrypted_credentials/coder.rb', line 45

def self.generate_key_hex(cipher = 'aes-128-gcm')
  key_length =
    case cipher
    when 'aes-128-gcm'
      16
    when 'aes-256-gcm'
      32
    else
      raise "Unsupported cipher: #{cipher}"
    end

  SecureRandom.hex(key_length)
end