Class: SafeCredentials::HashEncryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/safe_credentials/hash_encryptor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, cipher) ⇒ HashEncryptor

Returns a new instance of HashEncryptor.



5
6
7
8
# File 'lib/safe_credentials/hash_encryptor.rb', line 5

def initialize(attributes, cipher)
  @attributes, @cipher = attributes, cipher
  @attributes.extend SafeCredentials::HashQuery
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/safe_credentials/hash_encryptor.rb', line 3

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/safe_credentials/hash_encryptor.rb', line 3

def name
  @name
end

Instance Method Details

#decrypt!(path) ⇒ Object



52
53
54
55
56
57
# File 'lib/safe_credentials/hash_encryptor.rb', line 52

def decrypt!(path)
  last_key, subhash = subhash_for(path)
  new_key = last_key.sub(/^encrypted_/,'')

  subhash[new_key] = decrypt_val(subhash.delete(last_key))
end

#decrypt_hash(h) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/safe_credentials/hash_encryptor.rb', line 32

def decrypt_hash(h)
  Hash.new.tap do |decrypted|
    h.each do |k, v|
      key = k.sub(/^encrypted_/, '')
      decrypted[key] = decrypt_val(v)
    end
  end
end

#decrypt_paths!(query) ⇒ Object



63
64
65
# File 'lib/safe_credentials/hash_encryptor.rb', line 63

def decrypt_paths!(query)
  @attributes.query(query).each { |path| decrypt!(path) }
end

#decrypt_val(val) ⇒ Object



25
26
27
28
29
30
# File 'lib/safe_credentials/hash_encryptor.rb', line 25

def decrypt_val(val)
  case val
  when String then @cipher.dec(val).strip
  when Hash then decrypt_hash(val)
  end
end

#encrypt(path) ⇒ Object



41
42
43
# File 'lib/safe_credentials/hash_encryptor.rb', line 41

def encrypt(path)
  encrypt_val(value_for(path))
end

#encrypt!(path) ⇒ Object



45
46
47
48
49
50
# File 'lib/safe_credentials/hash_encryptor.rb', line 45

def encrypt!(path)
  last_key, subhash = subhash_for(path)
  new_key = "encrypted_#{last_key}"

  subhash[new_key] = encrypt_val(subhash.delete(last_key))
end

#encrypt_hash(h) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/safe_credentials/hash_encryptor.rb', line 17

def encrypt_hash(h)
  Hash.new.tap do |encrypted|
    h.each do |k, v|
      encrypted["encrypted_#{k}"] = encrypt_val(v)
    end
  end
end

#encrypt_paths!(query) ⇒ Object



59
60
61
# File 'lib/safe_credentials/hash_encryptor.rb', line 59

def encrypt_paths!(query)
  @attributes.query(query).each { |path| encrypt!(path) }
end

#encrypt_val(val) ⇒ Object



10
11
12
13
14
15
# File 'lib/safe_credentials/hash_encryptor.rb', line 10

def encrypt_val(val)
  case val
  when String then @cipher.enc(val).strip
  when Hash then encrypt_hash(val)
  end
end

#subhash_for(path) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/safe_credentials/hash_encryptor.rb', line 72

def subhash_for(path)
  parts    = path.split('.')
  last_key = parts.pop

  subhash = parts.inject(attributes) { |attrs, key| attrs[key] }

  [last_key, subhash]
end

#value_for(path) ⇒ Object



67
68
69
70
# File 'lib/safe_credentials/hash_encryptor.rb', line 67

def value_for(path)
  parts = path.split('.')
  parts.inject(attributes) { |attrs, key| attrs[key] }
end