class Sekreti::Crypt

Access helper to call the encryption and decryption actions.

Public Class Methods

new() click to toggle source
# 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

decrypt!() click to toggle source

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
dump() click to toggle source

Returns the configuration @return current instance configuration.

# File lib/sekreti/crypt.rb, line 156
def dump
  @options
end
encrypt!() click to toggle source

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
file() click to toggle source

Returns entry file @return entry file

# File lib/sekreti/crypt.rb, line 36
def file
  @options[:path]
end
file=(path) click to toggle source

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
file?() click to toggle source

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
key() click to toggle source

Returns the encryption key @return encryption key

# File lib/sekreti/crypt.rb, line 92
def key
  @options[:key]
end
key=(key) click to toggle source

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
key?() click to toggle source

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
output_file() click to toggle source

Returns output file parameter @return output file parameter

# File lib/sekreti/crypt.rb, line 64
def output_file
  @options[:output_file]
end
output_file=(output_file) click to toggle source

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
output_file?() click to toggle source

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
protocol() click to toggle source

Returns encryption protocol @return encryption protocol

# File lib/sekreti/crypt.rb, line 18
def protocol
  @options[:protocol]
end
protocol=(protocol) click to toggle source
# File lib/sekreti/crypt.rb, line 30
def protocol=(protocol)
  @options[:protocol] = protocol
end
protocol?() click to toggle source
# File lib/sekreti/crypt.rb, line 22
def protocol?
  unless @options.key?(:protocol)
    return false
  end

  true
end
status?() click to toggle source

Returns the action status.

# File lib/sekreti/crypt.rb, line 150
def status?
  @options[:status]
end