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.0.1"

Instance Method Summary collapse

Constructor Details

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

Initialize

algorithm

Algorithm

salt

Salt (8 bytes)

key

Crypt key


12
13
14
15
16
17
18
19
20
# File 'lib/scrambled_eggs.rb', line 12

def initialize( algorithm: 'aes-256-cbc', salt: nil, key: nil )
  @@algorithm = algorithm
  @@salt = salt ? salt : OpenSSL::Random.random_bytes( 8 )
  if key
    @@key = key
  else
    @@key = File.open( '/etc/hostname' ).read
  end
end

Instance Method Details

#descramble(scrambled) ⇒ Object

Descrypt data

scrambled

Data for decrypt

return

Decrypted data


39
40
41
42
43
44
45
46
# File 'lib/scrambled_eggs.rb', line 39

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

#scramble(data) ⇒ Object

Encrypt data

data

Data for encrypt

return

Crypted data


27
28
29
30
31
32
# File 'lib/scrambled_eggs.rb', line 27

def scramble( data )
  cipher = OpenSSL::Cipher::Cipher.new( @@algorithm )
  cipher.encrypt
  cipher.pkcs5_keyivgen( @@key, @@salt )
  @@salt + cipher.update( data ) + cipher.final
end