secrit 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/secrit.rb +37 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6faa44d62bd340907b101bf6ba6e56837587d51638f8741452fcc27b5c12095
|
4
|
+
data.tar.gz: ce5893a67b02b928ac95a214151cf1bd3bd2daf7332ed2813f4d89798e4d6124
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f34ba5b85f46dd496c32a388aea79e29737748cead405b268f64c4923ebe91cb51ca58a48b476f8befdd012d2a7a2694be94d193412c502728d457e24f82d30
|
7
|
+
data.tar.gz: 2d476217fc7b85149af601e19d6af6471ff7155c8788ffa413e2b2196310d3f795eed4d68623d24f3b6515062c34e8ec6afd861e57538fcc9ecf6c483cbb5024
|
data/lib/secrit.rb
CHANGED
@@ -1,38 +1,63 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require 'gpgme'
|
3
3
|
|
4
|
-
#
|
4
|
+
# Decryptor provides an interface for decryption. Classes that extend Decryptor
|
5
|
+
# must implement the decrypt method.
|
5
6
|
class Decryptor
|
7
|
+
# Decrypts a file at the given path.
|
8
|
+
#
|
9
|
+
# @param file_path [String] the path to the file to be decrypted.
|
10
|
+
# @raise [NotImplementedError] if the method is called on the Decryptor class
|
11
|
+
# itself, rather than a subclass.
|
6
12
|
def decrypt(file_path)
|
7
13
|
raise NotImplementedError
|
8
14
|
end
|
9
15
|
end
|
10
16
|
|
11
|
-
#
|
17
|
+
# GPGDecryptor is a default implementation of the Decryptor interface using GPG.
|
12
18
|
class GPGDecryptor < Decryptor
|
19
|
+
# Decrypts a file at the given path using GPG.
|
20
|
+
#
|
21
|
+
# @param file_path [String] the path to the file to be decrypted.
|
22
|
+
# @return [GPGME::Data] the decrypted data.
|
13
23
|
def decrypt(file_path)
|
14
24
|
crypto = GPGME::Crypto.new
|
15
25
|
decrypted_data = crypto.decrypt(File.open(file_path))
|
16
|
-
#decrypted_data.to_s.strip
|
17
26
|
decrypted_data
|
18
27
|
end
|
19
28
|
end
|
20
29
|
|
30
|
+
# Secrit provides methods to get encrypted data, allowing optional customization
|
31
|
+
# of the decryption process.
|
21
32
|
class Secrit
|
22
33
|
attr_accessor :storage
|
23
34
|
|
35
|
+
# Retrieves decrypted data for a given entry path.
|
36
|
+
#
|
37
|
+
# @param entry_path [String] the entry path to the encrypted data.
|
38
|
+
# @param storage [String] the storage path for the encrypted files (optional).
|
39
|
+
# @return [String] the decrypted content as a string.
|
24
40
|
def self.get(entry_path, storage: nil)
|
25
41
|
new(
|
26
42
|
storage: storage
|
27
43
|
).get(entry_path).to_s.strip
|
28
44
|
end
|
29
45
|
|
30
|
-
#
|
46
|
+
# Initializes a new Secrit object with optional customization.
|
47
|
+
#
|
48
|
+
# @param decryptor [Decryptor] an object that responds to the decrypt method
|
49
|
+
# (optional, defaults to GPGDecryptor.new).
|
50
|
+
# @param storage [String] the storage path for the encrypted files (optional).
|
31
51
|
def initialize(decryptor: GPGDecryptor.new, storage: nil)
|
32
52
|
@decryptor = decryptor
|
33
53
|
@storage = storage || File.expand_path("~/.password-store/")
|
34
54
|
end
|
35
55
|
|
56
|
+
# Retrieves decrypted data for a given entry path.
|
57
|
+
#
|
58
|
+
# @param entry_path [String] the entry path to the encrypted data.
|
59
|
+
# @return [GPGME::Data] the decrypted data.
|
60
|
+
# @raise [RuntimeError] if the file does not exist at the constructed path.
|
36
61
|
def get(entry_path)
|
37
62
|
full_path = construct_path(entry_path)
|
38
63
|
validate_file_existence(full_path)
|
@@ -41,10 +66,18 @@ class Secrit
|
|
41
66
|
|
42
67
|
private
|
43
68
|
|
69
|
+
# Constructs the full path to an encrypted file based on the entry path.
|
70
|
+
#
|
71
|
+
# @param entry_path [String] the entry path.
|
72
|
+
# @return [String] the full path to the encrypted file.
|
44
73
|
def construct_path(entry_path)
|
45
74
|
File.join(storage, "#{entry_path}.gpg")
|
46
75
|
end
|
47
76
|
|
77
|
+
# Validates that a file exists at the given path.
|
78
|
+
#
|
79
|
+
# @param full_path [String] the full path to the file.
|
80
|
+
# @raise [RuntimeError] if the file does not exist at the given path.
|
48
81
|
def validate_file_existence(full_path)
|
49
82
|
raise "File not found: #{full_path}" unless File.exist?(full_path)
|
50
83
|
end
|