Class: SecureDataBag::Item::Decryptor
- Inherits:
-
Object
- Object
- SecureDataBag::Item::Decryptor
show all
- Defined in:
- lib/secure_data_bag/decryptor.rb
Defined Under Namespace
Classes: DecryptionFailure
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(encrypted_hash, encryption, key) ⇒ Decryptor
Returns a new instance of Decryptor.
14
15
16
17
18
19
|
# File 'lib/secure_data_bag/decryptor.rb', line 14
def initialize(encrypted_hash, encryption, key)
@encryption = encryption
@encrypted_hash = encrypted_hash
@key = key
@iv = nil
end
|
Instance Attribute Details
#encrypted_hash ⇒ Object
Returns the value of attribute encrypted_hash.
25
26
27
|
# File 'lib/secure_data_bag/decryptor.rb', line 25
def encrypted_hash
@encrypted_hash
end
|
#encryption ⇒ Object
Returns the value of attribute encryption.
26
27
28
|
# File 'lib/secure_data_bag/decryptor.rb', line 26
def encryption
@encryption
end
|
#key ⇒ Object
Returns the value of attribute key.
27
28
29
|
# File 'lib/secure_data_bag/decryptor.rb', line 27
def key
@key
end
|
Instance Method Details
#decrypt_hash(hash) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/secure_data_bag/decryptor.rb', line 42
def decrypt_hash(hash)
hash.each do |k,v|
if encryption[:encoded_fields].include?(k)
begin
v = decrypt_value(v)
rescue Yajl::ParseError
raise DecryptionFailure,
"Error decrypting data bag value for #{k}."
rescue OpenSSL::Cipher::CipherError => e
raise DecryptionFailure,
"Error decrypting data bag value for #{k}: #{e.message}"
end
elsif v.is_a? Hash
v = decrypt_hash(v)
end
hash[k] = v
end
hash
end
|
#decrypt_value(value) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/secure_data_bag/decryptor.rb', line 62
def decrypt_value(value)
if value.is_a? String and not value.empty?
value = Base64.decode64(value)
value = openssl_decryptor.update(value)
value << openssl_decryptor.final
if value.include? "json_wrapper"
value = Yajl::Parser.parse(value)["json_wrapper"]
end
@openssl_decryptor = nil
end
value
end
|
#decrypted_hash ⇒ Object
36
37
38
39
40
|
# File 'lib/secure_data_bag/decryptor.rb', line 36
def decrypted_hash
@decrypted_hash ||= begin
decrypt_hash(encrypted_hash.dup)
end
end
|
#for_decrypted_item ⇒ Object
21
22
23
|
# File 'lib/secure_data_bag/decryptor.rb', line 21
def for_decrypted_item
decrypted_hash
end
|
#iv ⇒ Object
29
30
31
32
33
34
|
# File 'lib/secure_data_bag/decryptor.rb', line 29
def iv
@iv ||= begin
iv_string = encryption[:iv]
Base64.decode64(iv_string)
end
end
|
#openssl_decryptor ⇒ Object
76
77
78
79
80
81
82
83
84
|
# File 'lib/secure_data_bag/decryptor.rb', line 76
def openssl_decryptor
@openssl_decryptor ||= begin
d = OpenSSL::Cipher::Cipher.new(encryption[:cipher])
d.decrypt
d.key = Digest::SHA256.digest(key)
d.iv = iv
d
end
end
|