Class: SecureDataBag::SecureDataBagItem::Encryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/secure_data_bag/encryptor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unencrypted_hash, encryption, key) ⇒ Encryptor

Returns a new instance of Encryptor.



16
17
18
19
20
21
# File 'lib/secure_data_bag/encryptor.rb', line 16

def initialize(unencrypted_hash, encryption, key)
  @encryption = encryption
  @unencrypted_hash = unencrypted_hash
  @encoded_fields = []
  @key = key
end

Instance Attribute Details

#encoded_fieldsObject (readonly)

Returns the value of attribute encoded_fields.



13
14
15
# File 'lib/secure_data_bag/encryptor.rb', line 13

def encoded_fields
  @encoded_fields
end

#encryptionObject (readonly)

Returns the value of attribute encryption.



11
12
13
# File 'lib/secure_data_bag/encryptor.rb', line 11

def encryption
  @encryption
end

#keyObject (readonly)

Returns the value of attribute key.



14
15
16
# File 'lib/secure_data_bag/encryptor.rb', line 14

def key
  @key
end

#unencrypted_hashObject (readonly)

Returns the value of attribute unencrypted_hash.



12
13
14
# File 'lib/secure_data_bag/encryptor.rb', line 12

def unencrypted_hash
  @unencrypted_hash
end

Instance Method Details

#encrypt_hash(hash) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/secure_data_bag/encryptor.rb', line 38

def encrypt_hash(hash)
  hash.each do |k,v|
    if encryption[:encoded_fields].include?(k)
      v = encrypt_value(v)
      encoded_fields << k
    elsif v.is_a? Hash
      v = encrypt_hash(v)
    end
    hash[k] = v
  end
  hash
end

#encrypt_value(value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/secure_data_bag/encryptor.rb', line 51

def encrypt_value(value)
  value = normalize_value(value)

  if not value.nil? and not value.empty?
    value = openssl_encryptor.update(value)
    value << openssl_encryptor.final
    @openssl_encryptor = nil
    value = Base64.encode64(value)
  end

  value
end

#encrypted_hashObject



31
32
33
34
35
36
# File 'lib/secure_data_bag/encryptor.rb', line 31

def encrypted_hash
  pp "encrypted_hash"
  @encrypted_data ||= begin
    encrypt_hash(unencrypted_hash.dup) 
  end
end

#for_encrypted_itemObject



23
24
25
26
27
28
29
# File 'lib/secure_data_bag/encryptor.rb', line 23

def for_encrypted_item
  data = encrypted_hash
  encryption_hash = encryption.dup
  encryption_hash[:iv] = Base64.encode64(encryption_hash[:iv] || "")
  encryption_hash[:encoded_fields] = encoded_fields.uniq
  data.merge({encryption:encryption_hash})
end

#normalize_value(value) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/secure_data_bag/encryptor.rb', line 64

def normalize_value(value)
  if [Hash,Array].any? {|c| value.is_a? c}
    serialize_value(value)
  else 
    value.to_s
  end
end

#openssl_encryptorObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/secure_data_bag/encryptor.rb', line 76

def openssl_encryptor
  @openssl_encryptor ||= begin
    encryptor = OpenSSL::Cipher::Cipher.new(encryption[:cipher])
    encryptor.encrypt
    encryption[:iv] ||= encryptor.random_iv
    encryptor.iv = encryption[:iv]
    encryptor.key = Digest::SHA256.digest(key)
    encryptor
  end
end

#serialize_value(value) ⇒ Object



72
73
74
# File 'lib/secure_data_bag/encryptor.rb', line 72

def serialize_value(value)
  Yajl::Encoder.encode(:json_wrapper => value)
end