@@ -14,7 +14,10 @@ def encrypted?
1414 to_yaml [ 'cipher' ]
1515 end
1616
17+ # Determines the cipher to use for encryption/decryption
1718 def cipher
19+ return 'aes-128-gcm' if format == 'activesupport'
20+
1821 to_yaml [ 'cipher' ] || Encryptor ::DEFAULT_CIPHER
1922 end
2023
@@ -23,11 +26,33 @@ def exists?
2326 ::File . exist? ( @path )
2427 end
2528
29+ # Determines the format to be used for encryption
30+ # @return [String] diffcrypt|activesupport
31+ def format
32+ return 'diffcrypt' if read == ''
33+ return 'diffcrypt' if read . index ( '---' ) &.zero?
34+
35+ 'activesupport'
36+ end
37+
2638 # @return [String] Raw contents of the file
2739 def read
40+ return '' unless ::File . exist? ( @path )
41+
2842 @read ||= ::File . read ( @path )
43+ @read
44+ end
45+
46+ # Save the encrypted contents back to disk
47+ # @return [Boolean] True is file save was successful
48+ def write ( key , data , cipher : nil )
49+ cipher ||= self . cipher
50+ yaml = ::YAML . dump ( data )
51+ contents = Encryptor . new ( key , cipher : cipher ) . encrypt ( yaml )
52+ ::File . write ( @path , contents )
2953 end
3054
55+ # TODO: This seems useless, figure out what's up
3156 def encrypt ( key , cipher : DEFAULT_CIPHER )
3257 return read if encrypted?
3358
@@ -42,7 +67,7 @@ def decrypt(key)
4267 end
4368
4469 def to_yaml
45- @to_yaml ||= YAML . safe_load ( read )
70+ @to_yaml ||= YAML . safe_load ( read ) || { }
4671 end
4772 end
4873end
0 commit comments