Class: Sekreti::Crypt

Inherits:
Object
  • Object
show all
Defined in:
lib/sekreti/crypt.rb

Overview

Access helper to call the encryption and decryption actions.

Instance Method Summary collapse

Constructor Details

#initializeCrypt

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

Raises:

  • (StandardError)

See Also:

  • Sekreti::Core.decrypt!


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

#dumpObject

Returns the configuration

Returns:

  • current instance configuration.



156
157
158
# File 'lib/sekreti/crypt.rb', line 156

def dump
  @options
end

#encrypt!Object

Encrypts a file with parameters set

Raises:

  • (StandardError)

See Also:

  • Sekreti::Core.encrypt!


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

#fileObject

Returns entry file

Returns:

  • entry file



36
37
38
# File 'lib/sekreti/crypt.rb', line 36

def file
  @options[:path]
end

#file=(path) ⇒ Object

Defines entry file

Parameters:

  • path (String)

    relative or absolute path to 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.

Returns:

  • (Boolean)

    boolean



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

#keyObject

Returns the encryption key

Returns:

  • encryption key



92
93
94
# File 'lib/sekreti/crypt.rb', line 92

def key
  @options[:key]
end

#key=(key) ⇒ Object

Set the encryption key

Parameters:

  • key (String)

    16 bytes string

Returns:

  • boolean



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.

Returns:

  • (Boolean)

    boolean



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_fileObject

Returns output file parameter

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

Parameters:

  • output_file (String)

    relative or absolute path to 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.

Returns:

  • (Boolean)

    bool



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

#protocolObject

Returns encryption protocol

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

Returns:

  • (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.

Returns:

  • (Boolean)


150
151
152
# File 'lib/sekreti/crypt.rb', line 150

def status?
  @options[:status]
end