Class: ScrambledEggs

Inherits:
Object
  • Object
show all
Defined in:
lib/scrambled_eggs.rb,
lib/scrambled_eggs/version.rb

Overview

Easy data scrambler by OpenSSL.

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(algorithm: 'aes-256-cbc', salt: nil, key: nil, key_file: nil) ⇒ ScrambledEggs

Initialize

algorithm

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

salt

Salt (8 bytes)

key

Crypt key

key_file

Crypt key file (exclude key)

return

ScrambledEggs object



14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Method Details

#descramble(scrambled) ⇒ Object

Descramble (descrypt) data

scrambled

Data for descramble

return

descrambled data



44
45
46
47
48
49
50
51
# 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) ⇒ Object

Descramble (decrypt) file

_path

File for descramble



66
67
68
69
70
71
# 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) ⇒ Object

Scramble (encrypt) data

data

Data for scramble

return

Scrambled data



32
33
34
35
36
37
# 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) ⇒ Object

Scramble (encrypt) file

path

File for scramble



56
57
58
59
60
61
# 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