scrambled_eggs 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/CHANGELOG.md +8 -0
 - data/lib/scrambled_eggs.rb +27 -3
 - data/lib/scrambled_eggs/version.rb +1 -1
 - data/scrambled_eggs.gemspec +1 -1
 - data/test/scrambled_eggs_test.rb +78 -21
 - metadata +3 -3
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 351118ea6a4fe0d78c3920914c1bf29fcc5ea8e9
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: a9809c8abee09fdd5c3317a663a34061be34c4c4
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: e0627a14c49e8ffe3cd0a9ca580821cee892720b3f039c8fd18194c5d8af488daae1b190b520c48c2ff65a9247ccedd85fcdb067d4045a89116c7003b0eff4b3
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: c91c4d5453be9113a168ad6dc671a6a127941d90502b1a58736e73eccebcd204e2c57c00dedb8cd2920fb4924faad0175ea3c2212dc4e6108fd665f2fb146d5c
         
     | 
    
        data/CHANGELOG.md
    CHANGED
    
    | 
         @@ -1,7 +1,15 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            ### 0.1.0 / 1015-02-11  sanadan <jecy00@gmail.com>
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
              * Add generate key from key file.
         
     | 
| 
      
 4 
     | 
    
         
            +
              * Change default key to generate key from hostname file.
         
     | 
| 
      
 5 
     | 
    
         
            +
              * Add scramble/descramble file function.
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
       1 
8 
     | 
    
         
             
            ### 0.0.2 / 2015-02-07  sanadan <jecy00@gmail.com>
         
     | 
| 
       2 
9 
     | 
    
         | 
| 
       3 
10 
     | 
    
         
             
              * Fixed: Can't require in gemspec file
         
     | 
| 
       4 
11 
     | 
    
         | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
       5 
13 
     | 
    
         
             
            ### 0.0.1 / 2015-02-06  sanadan <jecy00@gmail.com>
         
     | 
| 
       6 
14 
     | 
    
         | 
| 
       7 
15 
     | 
    
         
             
              * First
         
     | 
    
        data/lib/scrambled_eggs.rb
    CHANGED
    
    | 
         @@ -8,15 +8,19 @@ class ScrambledEggs 
     | 
|
| 
       8 
8 
     | 
    
         
             
              # _algorithm_ ::  Algorithm (ex. 'aes-256-cbc')
         
     | 
| 
       9 
9 
     | 
    
         
             
              # _salt_      ::  Salt (8 bytes)
         
     | 
| 
       10 
10 
     | 
    
         
             
              # _key_       ::  Crypt key
         
     | 
| 
      
 11 
     | 
    
         
            +
              # _key_file_  ::  Crypt key file (exclude key)
         
     | 
| 
       11 
12 
     | 
    
         
             
              #
         
     | 
| 
       12 
13 
     | 
    
         
             
              # return      ::  ScrambledEggs object
         
     | 
| 
       13 
     | 
    
         
            -
              def initialize( algorithm: 'aes-256-cbc', salt: nil, key: nil )
         
     | 
| 
      
 14 
     | 
    
         
            +
              def initialize( algorithm: 'aes-256-cbc', salt: nil, key: nil, key_file: nil )
         
     | 
| 
       14 
15 
     | 
    
         
             
                @@algorithm = algorithm
         
     | 
| 
       15 
     | 
    
         
            -
                @@salt = salt ? salt : OpenSSL::Random.random_bytes( 8 )
         
     | 
| 
      
 16 
     | 
    
         
            +
                @@salt = salt != nil ? salt : OpenSSL::Random.random_bytes( 8 )
         
     | 
| 
       16 
17 
     | 
    
         
             
                if key
         
     | 
| 
       17 
18 
     | 
    
         
             
                  @@key = key
         
     | 
| 
       18 
19 
     | 
    
         
             
                else
         
     | 
| 
       19 
     | 
    
         
            -
                   
     | 
| 
      
 20 
     | 
    
         
            +
                  unless key_file
         
     | 
| 
      
 21 
     | 
    
         
            +
                    key_file = '/etc/hostname'
         
     | 
| 
      
 22 
     | 
    
         
            +
                  end
         
     | 
| 
      
 23 
     | 
    
         
            +
                  @@key = OpenSSL::Digest::SHA512.digest( Pathname.new( key_file ).binread )
         
     | 
| 
       20 
24 
     | 
    
         
             
                end
         
     | 
| 
       21 
25 
     | 
    
         
             
              end
         
     | 
| 
       22 
26 
     | 
    
         | 
| 
         @@ -45,5 +49,25 @@ class ScrambledEggs 
     | 
|
| 
       45 
49 
     | 
    
         
             
                cipher.pkcs5_keyivgen( @@key, salt )
         
     | 
| 
       46 
50 
     | 
    
         
             
                cipher.update( data ) + cipher.final
         
     | 
| 
       47 
51 
     | 
    
         
             
              end
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
              # Scramble (encrypt) file
         
     | 
| 
      
 54 
     | 
    
         
            +
              #
         
     | 
| 
      
 55 
     | 
    
         
            +
              # _path_  ::  File for scramble
         
     | 
| 
      
 56 
     | 
    
         
            +
              def scramble_file( path )
         
     | 
| 
      
 57 
     | 
    
         
            +
                pathname = Pathname( path )
         
     | 
| 
      
 58 
     | 
    
         
            +
                # Not exist Pathname#binwrite on Ruby 2.0.0
         
     | 
| 
      
 59 
     | 
    
         
            +
                #pathname.binwrite( scramble( pathname.binread ) )
         
     | 
| 
      
 60 
     | 
    
         
            +
                IO.binwrite( pathname, scramble( pathname.binread ) )
         
     | 
| 
      
 61 
     | 
    
         
            +
              end
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
              # Descramble (decrypt) file
         
     | 
| 
      
 64 
     | 
    
         
            +
              #
         
     | 
| 
      
 65 
     | 
    
         
            +
              # _path ::  File for descramble
         
     | 
| 
      
 66 
     | 
    
         
            +
              def descramble_file( path )
         
     | 
| 
      
 67 
     | 
    
         
            +
                pathname = Pathname.new( path )
         
     | 
| 
      
 68 
     | 
    
         
            +
                # Not exist Pathname#binwrite on Ruby 2.0.0
         
     | 
| 
      
 69 
     | 
    
         
            +
                #pathname.binwrite( descramble( pathname.binread ) )
         
     | 
| 
      
 70 
     | 
    
         
            +
                IO.binwrite( pathname, descramble( pathname.binread ) )
         
     | 
| 
      
 71 
     | 
    
         
            +
              end
         
     | 
| 
       48 
72 
     | 
    
         
             
            end
         
     | 
| 
       49 
73 
     | 
    
         | 
    
        data/scrambled_eggs.gemspec
    CHANGED
    
    | 
         @@ -9,7 +9,7 @@ Gem::Specification.new do |spec| 
     | 
|
| 
       9 
9 
     | 
    
         
             
              spec.authors       = ["sanadan"]
         
     | 
| 
       10 
10 
     | 
    
         
             
              spec.email         = ["jecy00@gmail.com"]
         
     | 
| 
       11 
11 
     | 
    
         
             
              spec.summary       = %q{Easy data scrambler.}
         
     | 
| 
       12 
     | 
    
         
            -
              spec.description   = %q{Easy data  
     | 
| 
      
 12 
     | 
    
         
            +
              spec.description   = %q{Easy data scrambler. Default by hostname.}
         
     | 
| 
       13 
13 
     | 
    
         
             
              spec.homepage      = "https://github.com/sanadan/scrambled_eggs"
         
     | 
| 
       14 
14 
     | 
    
         
             
              spec.license       = "MIT"
         
     | 
| 
       15 
15 
     | 
    
         | 
    
        data/test/scrambled_eggs_test.rb
    CHANGED
    
    | 
         @@ -1,48 +1,105 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
             
     | 
| 
       2 
     | 
    
         
            -
            require  
     | 
| 
       3 
     | 
    
         
            -
            require  
     | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
       7 
     | 
    
         
            -
             
     | 
| 
       8 
     | 
    
         
            -
            		@@tempname_number += 1
         
     | 
| 
       9 
     | 
    
         
            -
            		path = new(dir) + "#{File.basename(base)}.#{$$}.#{@@tempname_number}"
         
     | 
| 
       10 
     | 
    
         
            -
            		at_exit do
         
     | 
| 
       11 
     | 
    
         
            -
            			path.rmtree if path.exist?
         
     | 
| 
       12 
     | 
    
         
            -
            		end
         
     | 
| 
       13 
     | 
    
         
            -
            		path
         
     | 
| 
       14 
     | 
    
         
            -
            	end
         
     | 
| 
      
 1 
     | 
    
         
            +
            require File.dirname(__FILE__) + '/test_helper.rb'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'tempfile'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'pathname'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            def generate_random_data( size = 4096 )
         
     | 
| 
      
 6 
     | 
    
         
            +
              random_size = OpenSSL::BN.rand_range( size ) + 1
         
     | 
| 
      
 7 
     | 
    
         
            +
              return OpenSSL::Random.random_bytes( random_size )
         
     | 
| 
       15 
8 
     | 
    
         
             
            end
         
     | 
| 
       16 
     | 
    
         
            -
            =end
         
     | 
| 
       17 
9 
     | 
    
         | 
| 
       18 
     | 
    
         
            -
             
     | 
| 
      
 10 
     | 
    
         
            +
            def generate_random_file( data )
         
     | 
| 
      
 11 
     | 
    
         
            +
              file = Tempfile.open( 'ScrambledEggs' )
         
     | 
| 
      
 12 
     | 
    
         
            +
              path = Pathname( file.path )
         
     | 
| 
      
 13 
     | 
    
         
            +
              # Not exist Pathname#binwrite on Ruby 2.0.0
         
     | 
| 
      
 14 
     | 
    
         
            +
              #path.binwrite( data )
         
     | 
| 
      
 15 
     | 
    
         
            +
              IO.binwrite( path, data )
         
     | 
| 
      
 16 
     | 
    
         
            +
              return path
         
     | 
| 
      
 17 
     | 
    
         
            +
            end
         
     | 
| 
       19 
18 
     | 
    
         | 
| 
       20 
19 
     | 
    
         
             
            class ScrambledEggsTest < Test::Unit::TestCase
         
     | 
| 
       21 
20 
     | 
    
         
             
              def test_scramble_text
         
     | 
| 
       22 
21 
     | 
    
         
             
                text = 'Test text'
         
     | 
| 
       23 
22 
     | 
    
         
             
                egg = ScrambledEggs.new
         
     | 
| 
       24 
23 
     | 
    
         
             
                scrambled = egg.scramble( text )
         
     | 
| 
      
 24 
     | 
    
         
            +
                assert_not_equal( scrambled, text )
         
     | 
| 
       25 
25 
     | 
    
         
             
                assert_equal( egg.descramble( scrambled ), text )
         
     | 
| 
       26 
26 
     | 
    
         
             
              end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
       27 
28 
     | 
    
         
             
              def test_scramble_binary
         
     | 
| 
       28 
     | 
    
         
            -
                 
     | 
| 
       29 
     | 
    
         
            -
                data = OpenSSL::Random.random_bytes( size )
         
     | 
| 
      
 29 
     | 
    
         
            +
                data = generate_random_data
         
     | 
| 
       30 
30 
     | 
    
         
             
                egg = ScrambledEggs.new
         
     | 
| 
       31 
31 
     | 
    
         
             
                scrambled = egg.scramble( data )
         
     | 
| 
      
 32 
     | 
    
         
            +
                assert_not_equal( scrambled, data )
         
     | 
| 
       32 
33 
     | 
    
         
             
                assert_equal( egg.descramble( scrambled ), data )
         
     | 
| 
       33 
34 
     | 
    
         
             
              end
         
     | 
| 
       34 
     | 
    
         
            -
             
     | 
| 
       35 
     | 
    
         
            -
             
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
              def test_scramble_another_parameter
         
     | 
| 
      
 37 
     | 
    
         
            +
                text = 'Test text 2'
         
     | 
| 
       36 
38 
     | 
    
         
             
                egg = ScrambledEggs.new( algorithm: 'aes-128-cbc', salt: '01234567', key: 'Test key' )
         
     | 
| 
       37 
39 
     | 
    
         
             
                scrambled = egg.scramble( text )
         
     | 
| 
       38 
40 
     | 
    
         
             
            #    puts scrambled.unpack( 'H*' )
         
     | 
| 
       39 
     | 
    
         
            -
                assert_equal( scrambled, [ ' 
     | 
| 
      
 41 
     | 
    
         
            +
                assert_equal( scrambled, [ '30313233343536374d34d042eecb111955e73b3630a399e5' ].pack( 'H*' ) )
         
     | 
| 
       40 
42 
     | 
    
         
             
                assert_equal( egg.descramble( scrambled ), text )
         
     | 
| 
       41 
43 
     | 
    
         
             
              end
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
       42 
45 
     | 
    
         
             
              def test_another_eggs
         
     | 
| 
       43 
46 
     | 
    
         
             
                text = 'Test text 3'
         
     | 
| 
       44 
47 
     | 
    
         
             
                scrambled = ScrambledEggs.new.scramble( text )
         
     | 
| 
       45 
48 
     | 
    
         
             
                assert_equal( ScrambledEggs.new.descramble( scrambled ), text )
         
     | 
| 
      
 49 
     | 
    
         
            +
                assert_raise OpenSSL::Cipher::CipherError do
         
     | 
| 
      
 50 
     | 
    
         
            +
                  ScrambledEggs.new( key: generate_random_data( 256 ) ).descramble( scrambled )
         
     | 
| 
      
 51 
     | 
    
         
            +
                end
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
                algorithm = 'aes-192-cbc'
         
     | 
| 
      
 54 
     | 
    
         
            +
                salt = '98765432'
         
     | 
| 
      
 55 
     | 
    
         
            +
                key = 'Test key 2'
         
     | 
| 
      
 56 
     | 
    
         
            +
                scrambled = ScrambledEggs.new( algorithm: algorithm, salt: salt, key: key ).scramble( text )
         
     | 
| 
      
 57 
     | 
    
         
            +
                assert_equal( ScrambledEggs.new( algorithm: algorithm, salt: salt, key: key ).descramble( scrambled ), text )
         
     | 
| 
      
 58 
     | 
    
         
            +
                assert_raise OpenSSL::Cipher::CipherError do
         
     | 
| 
      
 59 
     | 
    
         
            +
                  ScrambledEggs.new( algorithm: algorithm, salt: salt, key: generate_random_data( 256 ) ).descramble( scrambled )
         
     | 
| 
      
 60 
     | 
    
         
            +
                end
         
     | 
| 
      
 61 
     | 
    
         
            +
              end
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
              def test_salt
         
     | 
| 
      
 64 
     | 
    
         
            +
                text = 'Test text 4'
         
     | 
| 
      
 65 
     | 
    
         
            +
                256.times do |i|
         
     | 
| 
      
 66 
     | 
    
         
            +
                  salt = []
         
     | 
| 
      
 67 
     | 
    
         
            +
                  8.times do
         
     | 
| 
      
 68 
     | 
    
         
            +
                    salt << i
         
     | 
| 
      
 69 
     | 
    
         
            +
                  end
         
     | 
| 
      
 70 
     | 
    
         
            +
                  salt = salt.pack( 'C*' )
         
     | 
| 
      
 71 
     | 
    
         
            +
                  scrambled = ''
         
     | 
| 
      
 72 
     | 
    
         
            +
                  assert_nothing_raised do
         
     | 
| 
      
 73 
     | 
    
         
            +
                    scrambled = ScrambledEggs.new( salt: salt ).scramble( text )
         
     | 
| 
      
 74 
     | 
    
         
            +
                  end
         
     | 
| 
      
 75 
     | 
    
         
            +
                  assert_nothing_raised do
         
     | 
| 
      
 76 
     | 
    
         
            +
                    ScrambledEggs.new.descramble( scrambled )
         
     | 
| 
      
 77 
     | 
    
         
            +
                  end
         
     | 
| 
      
 78 
     | 
    
         
            +
                end
         
     | 
| 
      
 79 
     | 
    
         
            +
              end
         
     | 
| 
      
 80 
     | 
    
         
            +
             
     | 
| 
      
 81 
     | 
    
         
            +
              def test_file
         
     | 
| 
      
 82 
     | 
    
         
            +
                data = generate_random_data
         
     | 
| 
      
 83 
     | 
    
         
            +
                path = generate_random_file( data )
         
     | 
| 
      
 84 
     | 
    
         
            +
                ScrambledEggs.new.scramble_file( path )
         
     | 
| 
      
 85 
     | 
    
         
            +
                assert_not_equal( path.binread, data )
         
     | 
| 
      
 86 
     | 
    
         
            +
                ScrambledEggs.new.descramble_file( path )
         
     | 
| 
      
 87 
     | 
    
         
            +
                assert_equal( path.binread, data )
         
     | 
| 
      
 88 
     | 
    
         
            +
                assert_raise OpenSSL::Cipher::CipherError do
         
     | 
| 
      
 89 
     | 
    
         
            +
                  ScrambledEggs.new( key: generate_random_data( 256 ) ).descramble_file( path )
         
     | 
| 
      
 90 
     | 
    
         
            +
                end
         
     | 
| 
      
 91 
     | 
    
         
            +
              end
         
     | 
| 
      
 92 
     | 
    
         
            +
             
     | 
| 
      
 93 
     | 
    
         
            +
              def test_key_file
         
     | 
| 
      
 94 
     | 
    
         
            +
                text = 'Test text 5'
         
     | 
| 
      
 95 
     | 
    
         
            +
                key_path = generate_random_file( generate_random_data )
         
     | 
| 
      
 96 
     | 
    
         
            +
                scrambled = ScrambledEggs.new( key_file: key_path ).scramble( text )
         
     | 
| 
      
 97 
     | 
    
         
            +
                assert_not_equal( scrambled, text )
         
     | 
| 
      
 98 
     | 
    
         
            +
                assert_equal( ScrambledEggs.new( key_file: key_path ).descramble( scrambled ), text )
         
     | 
| 
      
 99 
     | 
    
         
            +
                key_path2 = generate_random_file( generate_random_data )
         
     | 
| 
      
 100 
     | 
    
         
            +
                assert_raise OpenSSL::Cipher::CipherError do
         
     | 
| 
      
 101 
     | 
    
         
            +
                  ScrambledEggs.new( key_file: key_path2 ).descramble( scrambled )
         
     | 
| 
      
 102 
     | 
    
         
            +
                end
         
     | 
| 
       46 
103 
     | 
    
         
             
              end
         
     | 
| 
       47 
104 
     | 
    
         
             
            end
         
     | 
| 
       48 
105 
     | 
    
         | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: scrambled_eggs
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.0 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - sanadan
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2015-02- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2015-02-11 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: rake
         
     | 
| 
         @@ -38,7 +38,7 @@ dependencies: 
     | 
|
| 
       38 
38 
     | 
    
         
             
                - - ">="
         
     | 
| 
       39 
39 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       40 
40 
     | 
    
         
             
                    version: '0'
         
     | 
| 
       41 
     | 
    
         
            -
            description: Easy data  
     | 
| 
      
 41 
     | 
    
         
            +
            description: Easy data scrambler. Default by hostname.
         
     | 
| 
       42 
42 
     | 
    
         
             
            email:
         
     | 
| 
       43 
43 
     | 
    
         
             
            - jecy00@gmail.com
         
     | 
| 
       44 
44 
     | 
    
         
             
            executables: []
         
     |