class Sekreti::Crypt
Access helper to call the encryption and decryption actions.
Public Class Methods
# File lib/sekreti/crypt.rb, line 7 def initialize() @options = { path: nil, output_file: nil, protocol: "AES-128-CBC", status: false } end
Public Instance Methods
Decrypts a file with parameters set @see Core.decrypt!
# 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
Returns the configuration @return current instance configuration.
# File lib/sekreti/crypt.rb, line 156 def dump @options end
Encrypts a file with parameters set @see Core.encrypt!
# 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
Returns entry file @return entry file
# File lib/sekreti/crypt.rb, line 36 def file @options[:path] end
Defines entry file @param path [String] relative or absolute path to
entry file.
# File lib/sekreti/crypt.rb, line 43 def file=(path) @options[:path] = path end
Returns true if entry file is defined and exists. @return boolean
# 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
Returns the encryption key @return encryption key
# File lib/sekreti/crypt.rb, line 92 def key @options[:key] end
Set the encryption key @param key [String] 16 bytes string @return boolean
# 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
returns true if key is defined. @return boolean
# File lib/sekreti/crypt.rb, line 113 def key? unless @options.key?(:key) return false end true end
Returns output file parameter @return output file parameter
# File lib/sekreti/crypt.rb, line 64 def output_file @options[:output_file] end
Defines the output file @param output_file
[String] relative or absolute path
to output file.
# File lib/sekreti/crypt.rb, line 71 def output_file=(output_file) @options[:output_file] = output_file end
Returns true if the output file is defined and
it doesn't exist yet.
@return bool
# 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
Returns encryption protocol @return encryption protocol
# File lib/sekreti/crypt.rb, line 18 def protocol @options[:protocol] end
# File lib/sekreti/crypt.rb, line 30 def protocol=(protocol) @options[:protocol] = protocol end
# File lib/sekreti/crypt.rb, line 22 def protocol? unless @options.key?(:protocol) return false end true end
Returns the action status.
# File lib/sekreti/crypt.rb, line 150 def status? @options[:status] end