class Mystiko

The Mystiko class is where all the data encryption code resides.

Constants

VERSION

The version of this encryption library.

Attributes

generator[R]

The source of simulated entropy

input[R]

The array of input bytes

output[RW]

Access to the output instance variable
Endemic Code Smells

:reek: Attribute -- Access needed for testing only.
window[R]

The sliding window size.

Public Class Methods

new() click to toggle source

Set up an encryption object.

# File lib/mystiko.rb, line 14
def initialize
  @filler = Random.new
  @fill_value = nil
end

Public Instance Methods

decrypt(args={}) click to toggle source

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

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

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
encrypt(args={}) click to toggle source

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

Get a decoy filler byte.

# File lib/mystiko.rb, line 20
def filler_byte
  @fill_value || @filler.rand(256)
end
process_inputs(args) click to toggle source

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

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

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

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

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

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