Class: Sekreti::Crypt
- Inherits:
-
Object
- Object
- Sekreti::Crypt
- Defined in:
- lib/sekreti/crypt.rb
Overview
Access helper to call the encryption and decryption actions.
Instance Method Summary collapse
-
#decrypt! ⇒ Object
Decrypts a file with parameters set.
-
#dump ⇒ Object
Returns the configuration.
-
#encrypt! ⇒ Object
Encrypts a file with parameters set.
-
#file ⇒ Object
Returns entry file.
-
#file=(path) ⇒ Object
Defines entry file.
-
#file? ⇒ Boolean
Returns true if entry file is defined and exists.
-
#initialize ⇒ Crypt
constructor
A new instance of Crypt.
-
#key ⇒ Object
Returns the encryption key.
-
#key=(key) ⇒ Object
Set the encryption key.
-
#key? ⇒ Boolean
returns true if key is defined.
-
#output_file ⇒ Object
Returns output file parameter.
-
#output_file=(output_file) ⇒ Object
Defines the output file.
-
#output_file? ⇒ Boolean
Returns true if the output file is defined and it doesn’t exist yet.
-
#protocol ⇒ Object
Returns encryption protocol.
- #protocol=(protocol) ⇒ Object
- #protocol? ⇒ Boolean
-
#status? ⇒ Boolean
Returns the action status.
Constructor Details
#initialize ⇒ Crypt
Returns a new instance of Crypt.
7 8 9 10 11 12 13 14 |
# File 'lib/sekreti/crypt.rb', line 7 def initialize() @options = { path: nil, output_file: nil, protocol: "AES-128-CBC", status: false } end |
Instance Method Details
#decrypt! ⇒ Object
Decrypts a file with parameters set
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/sekreti/crypt.rb', line 137 def decrypt! raise StandardError, "No entry file defined." unless file? raise StandardError, "No output file defnined" unless output_file? raise StandardError, "No key defined" unless key? if Core.decrypt!(@options) @options[:status] = true else @options[:status] = false end end |
#dump ⇒ Object
Returns the configuration
156 157 158 |
# File 'lib/sekreti/crypt.rb', line 156 def dump @options end |
#encrypt! ⇒ Object
Encrypts a file with parameters set
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/sekreti/crypt.rb', line 123 def encrypt! raise StandardError, "No entry file defined." unless file? raise StandardError, "No output file defnined" unless output_file? raise StandardError, "No key defined" unless key? if Core.encrypt!(@options) @options[:status] = true else @options[:status] = false end end |
#file ⇒ Object
Returns entry file
36 37 38 |
# File 'lib/sekreti/crypt.rb', line 36 def file @options[:path] end |
#file=(path) ⇒ Object
Defines entry file
43 44 45 |
# File 'lib/sekreti/crypt.rb', line 43 def file=(path) @options[:path] = path end |
#file? ⇒ Boolean
Returns true if entry file is defined and exists.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sekreti/crypt.rb', line 50 def file? unless @options.key?(:path) return false end unless File.file? @options[:path] return false end true end |
#key ⇒ Object
Returns the encryption key
92 93 94 |
# File 'lib/sekreti/crypt.rb', line 92 def key @options[:key] end |
#key=(key) ⇒ Object
Set the encryption key
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/sekreti/crypt.rb', line 99 def key=(key) if @options[:protocol] == "AES-128-CBC" raise(StandardError, "Key must be 16 bytes long.") if key.length != 16 end if @options[:protocol] == "AES-256-CBC" raise(StandardError, "Key must be 32 bytes long.") if key.length != 32 end @options[:key] = key end |
#key? ⇒ Boolean
returns true if key is defined.
113 114 115 116 117 118 119 |
# File 'lib/sekreti/crypt.rb', line 113 def key? unless @options.key?(:key) return false end true end |
#output_file ⇒ Object
Returns output file parameter
64 65 66 |
# File 'lib/sekreti/crypt.rb', line 64 def output_file @options[:output_file] end |
#output_file=(output_file) ⇒ Object
Defines the output file
71 72 73 |
# File 'lib/sekreti/crypt.rb', line 71 def output_file=(output_file) @options[:output_file] = output_file end |
#output_file? ⇒ Boolean
Returns true if the output file is defined and
it doesn't exist yet.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sekreti/crypt.rb', line 78 def output_file? unless @options.key?(:output_file) return false end if File.file? @options[:output_file] return false end true end |
#protocol ⇒ Object
Returns encryption protocol
18 19 20 |
# File 'lib/sekreti/crypt.rb', line 18 def protocol @options[:protocol] end |
#protocol=(protocol) ⇒ Object
30 31 32 |
# File 'lib/sekreti/crypt.rb', line 30 def protocol=(protocol) @options[:protocol] = protocol end |
#protocol? ⇒ Boolean
22 23 24 25 26 27 28 |
# File 'lib/sekreti/crypt.rb', line 22 def protocol? unless @options.key?(:protocol) return false end true end |
#status? ⇒ Boolean
Returns the action status.
150 151 152 |
# File 'lib/sekreti/crypt.rb', line 150 def status? @options[:status] end |