class Mystiko
The Mystiko
class is where all the data encryption code resides.
-
decrypt.rb – Data decryption.
-
encrypt.rb – Data encryption.
-
generator.rb – The internal pseudo-random number generator.
-
inputs.rb – Common input parameter processing.
-
outputs.rb – Common output parameter processing.
Constants
- VERSION
The version of this encryption library.
Attributes
The source of simulated entropy
The array of input bytes
Access to the output instance variable
Endemic Code Smells
:reek: Attribute -- Access needed for testing only.
The sliding window size.
Public Class Methods
Set up an encryption object.
# File lib/mystiko.rb, line 14 def initialize @filler = Random.new @fill_value = nil end
Public Instance Methods
Perform data decryption
# File lib/mystiko/decrypt.rb, line 9 def decrypt(args={}) process_inputs(args) setup_cypher_input do_decryption setup_clear_output process_outputs(args) end
Do the actual decryption work.
Endemic Code Smells
-
:reek:TooManyStatements
# File lib/mystiko/decrypt.rb, line 27 def do_decryption result = Array.new(@input.length + @window, 32) @input.each do | value | index = @generator.rand(@window) result[@offsets[index]] = value ^ @generator.rand(256) @offsets.delete_at(index) @offsets << @offset @offset += 1 end @output = result.pack("C*") end
Do the actual encryption work.
# File lib/mystiko/encrypt.rb, line 28 def do_encryption result, processed = [], 0 while processed < @length index = @generator.rand(@window) result << (@data.delete_at(index) ^ @generator.rand(256)) @data << (@input[@offset] || filler_byte) @offset += 1 processed += 1 if (index + processed) < @length end @output = result.pack("C*") end
Perform data encryption
# File lib/mystiko/encrypt.rb, line 7 def encrypt(args={}) process_inputs(args) setup_clear_input do_encryption setup_cypher_output process_outputs(args) end
Get a decoy filler byte.
# File lib/mystiko.rb, line 20 def filler_byte @fill_value || @filler.rand(256) end
Perform common input argument processing.
# File lib/mystiko/inputs.rb, line 16 def process_inputs(args) @input = ((name = args[:in_file]) && (IO.read(name, mode: "rb"))) || args[:in_str] || fail("An input must be specified.") @generator = args[:generator] || ((key = args[:key]) && Generator.new(key)) || fail("A key or generator must be specified.") @window = args[:window] || 16 #The filler value is for testing purposes only. It should #not be specified when secure operation is desired. @fill_value = args[:filler] end
Perform common output argument processing.
Returns
-
The output string.
# File lib/mystiko/outputs.rb, line 14 def process_outputs(args) @input = @generator = @window = @fill_value = nil # Cover our tracks. if (name = args[:out_file]) IO.write(name, @output, mode: "wb") elsif (out_str = args[:out_str]) out_str << @output end (_, @output = @output, nil)[0] # Return output and erase it. end
Get the clear (aka unencrypted) input data set up.
# File lib/mystiko/encrypt.rb, line 16 def setup_clear_input temp = @input.bytes @input = "#{temp.length.to_s(36)};".bytes + temp @length = @input.length @data = @input[0...@window] (@window - (@offset = @data.length)).times do @data << filler_byte end end
Get the clear (aka unencrypted) output data set up.
# File lib/mystiko/decrypt.rb, line 43 def setup_clear_output @offsets = @offset = nil if /^[0-9a-z]+;/ =~ @output @output = $POSTMATCH[0...($MATCH.to_i(36))] else fail "Unable to recover data." end end
Get the cypher (aka encrypted) input data set up.
# File lib/mystiko/decrypt.rb, line 18 def setup_cypher_input @input = @input.bytes @offsets = (0...@window).to_a @offset = @window end
Get the cypher (aka encrypted) output data set up.
# File lib/mystiko/encrypt.rb, line 45 def setup_cypher_output @data = @offset = nil # Just cleanup. end