class ScrambledEggs

Easy data scrambler by OpenSSL.

Constants

VERSION

Public Class Methods

new( algorithm: 'aes-256-cbc', salt: nil, key: nil, key_file: nil ) click to toggle source

Initialize

algorithm

Algorithm (ex. ‘aes-256-cbc’)

salt

Salt (8 bytes)

key

Crypt key

key_file

Crypt key file (exclude key)

return

ScrambledEggs object

# File lib/scrambled_eggs.rb, line 14
def initialize( algorithm: 'aes-256-cbc', salt: nil, key: nil, key_file: nil )
  @@algorithm = algorithm
  @@salt = salt != nil ? salt : OpenSSL::Random.random_bytes( 8 )
  if key
    @@key = key
  else
    unless key_file
      key_file = '/etc/hostname'
    end
    @@key = OpenSSL::Digest::SHA512.digest( Pathname.new( key_file ).binread )
  end
end

Public Instance Methods

descramble( scrambled ) click to toggle source

Descramble (descrypt) data

scrambled

Data for descramble

return

descrambled data

# File lib/scrambled_eggs.rb, line 44
def descramble( scrambled )
  cipher = OpenSSL::Cipher::Cipher.new( @@algorithm )
  cipher.decrypt
  salt = scrambled[ 0, 8 ]
  data = scrambled[ 8, scrambled.size ]
  cipher.pkcs5_keyivgen( @@key, salt )
  cipher.update( data ) + cipher.final
end
descramble_file( path ) click to toggle source

Descramble (decrypt) file

_path

File for descramble

# File lib/scrambled_eggs.rb, line 66
def descramble_file( path )
  pathname = Pathname.new( path )
  # Not exist Pathname#binwrite on Ruby 2.0.0
  #pathname.binwrite( descramble( pathname.binread ) )
  IO.binwrite( pathname, descramble( pathname.binread ) )
end
scramble( data ) click to toggle source

Scramble (encrypt) data

data

Data for scramble

return

Scrambled data

# File lib/scrambled_eggs.rb, line 32
def scramble( data )
  cipher = OpenSSL::Cipher::Cipher.new( @@algorithm )
  cipher.encrypt
  cipher.pkcs5_keyivgen( @@key, @@salt )
  @@salt + cipher.update( data ) + cipher.final
end
scramble_file( path ) click to toggle source

Scramble (encrypt) file

path

File for scramble

# File lib/scrambled_eggs.rb, line 56
def scramble_file( path )
  pathname = Pathname( path )
  # Not exist Pathname#binwrite on Ruby 2.0.0
  #pathname.binwrite( scramble( pathname.binread ) )
  IO.binwrite( pathname, scramble( pathname.binread ) )
end