symmetric-encryption 0.8.0 → 0.9.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.
data/README.md CHANGED
@@ -129,6 +129,18 @@ Example: Compress, Encrypt and write data to a file
129
129
  file.write "Keep this safe and secure\n"
130
130
  end
131
131
 
132
+ ### Standalone test
133
+
134
+ Before generating keys we can use SymmetricEncryption in a standalone test environment:
135
+
136
+ # Use test encryption keys
137
+ SymmetricEncryption.cipher = SymmetricEncryption::Cipher.new(
138
+ :key => '1234567890ABCDEF1234567890ABCDEF',
139
+ :iv => '1234567890ABCDEF',
140
+ :cipher => 'aes-128-cbc'
141
+ )
142
+ encrypted = SymmetricEncryption.encrypt('hello world')
143
+ puts SymmetricEncryption.decrypt(encrypted)
132
144
 
133
145
  ### Generating encrypted passwords
134
146
 
@@ -143,11 +155,11 @@ Note: Passwords must be encrypted in the environment in which they will be used.
143
155
  ### Add to an existing Rails project
144
156
  Add the following line to Gemfile
145
157
 
146
- gem 'symmetric-encryption'
158
+ gem 'symmetric-encryption'
147
159
 
148
160
  Install the Gem with bundler
149
161
 
150
- bundle install
162
+ bundle install
151
163
 
152
164
  ## Rails Configuration
153
165
 
@@ -160,7 +172,7 @@ modified as needed.
160
172
 
161
173
  Generate the configuration file:
162
174
 
163
- rails generate symmetric_encryption:config /etc/rails/keys
175
+ rails generate symmetric_encryption:config /etc/rails/keys
164
176
 
165
177
  Note: Ignore the warning about "Symmetric Encryption config not found" since it is
166
178
  being generated
@@ -224,7 +236,7 @@ SymmetricEncryption can also be used in non-Rails environment.
224
236
 
225
237
  Install SymmetricEncryption
226
238
 
227
- gem install symmetric-encryption
239
+ gem install symmetric-encryption
228
240
 
229
241
  Manually create a symmetric-encryption.yml configuration file based on the
230
242
  one supplied in examples/symmetric-encryption.yml.
data/foo.zip ADDED
Binary file
@@ -1,19 +1 @@
1
- require 'symmetric_encryption/version'
2
- require 'symmetric_encryption/cipher'
3
- require 'symmetric_encryption/symmetric_encryption'
4
- require 'symmetric_encryption/reader'
5
- require 'symmetric_encryption/writer'
6
- require 'zlib'
7
- if defined?(Rails)
8
- require 'symmetric_encryption/railtie'
9
- end
10
- # attr_encrypted and Encrypted validator
11
- if defined?(ActiveRecord::Base)
12
- require 'symmetric_encryption/extensions/active_record/base'
13
- require 'symmetric_encryption/railties/symmetric_encryption_validator'
14
- end
15
-
16
- # field encryption for Mongoid
17
- if defined?(Mongoid)
18
- require 'symmetric_encryption/extensions/mongoid/fields'
19
- end
1
+ require 'symmetric_encryption'
@@ -0,0 +1,19 @@
1
+ require 'symmetric_encryption/version'
2
+ require 'symmetric_encryption/cipher'
3
+ require 'symmetric_encryption/symmetric_encryption'
4
+ require 'symmetric_encryption/reader'
5
+ require 'symmetric_encryption/writer'
6
+ require 'zlib'
7
+ if defined?(Rails)
8
+ require 'symmetric_encryption/railtie'
9
+ end
10
+ # attr_encrypted and Encrypted validator
11
+ if defined?(ActiveRecord::Base)
12
+ require 'symmetric_encryption/extensions/active_record/base'
13
+ require 'symmetric_encryption/railties/symmetric_encryption_validator'
14
+ end
15
+
16
+ # field encryption for Mongoid
17
+ if defined?(Mongoid)
18
+ require 'symmetric_encryption/extensions/mongoid/fields'
19
+ end
@@ -32,13 +32,33 @@ module SymmetricEncryption
32
32
  # Create a Symmetric::Key for encryption and decryption purposes
33
33
  #
34
34
  # Parameters:
35
- # :key
35
+ # :key [String]
36
36
  # The Symmetric Key to use for encryption and decryption
37
- # :iv
37
+ #
38
+ # :iv [String]
38
39
  # Optional. The Initialization Vector to use with Symmetric Key
39
- # :cipher
40
+ # Highly Recommended as it is the input into the CBC algorithm
41
+ #
42
+ # :cipher [String]
40
43
  # Optional. Encryption Cipher to use
41
44
  # Default: aes-256-cbc
45
+ #
46
+ # :encoding [Symbol]
47
+ # :base64strict
48
+ # Return as a base64 encoded string that does not include additional newlines
49
+ # This is the recommended format since newlines in the values to
50
+ # SQL queries are cumbersome. Also the newline reformatting is unnecessary
51
+ # It is not the default for backward compatibility
52
+ # :base64
53
+ # Return as a base64 encoded string
54
+ # :binary
55
+ # Return as raw binary data string. Note: String can contain embedded nulls
56
+ # Default: :base64
57
+ # Recommended: :base64strict
58
+ #
59
+ # :version [Fixnum]
60
+ # Optional. The version number of this encryption key
61
+ # Used by SymmetricEncryption to select the correct key when decrypting data
42
62
  def initialize(parms={})
43
63
  raise "Missing mandatory parameter :key" unless @key = parms[:key]
44
64
  @iv = parms[:iv]
@@ -50,34 +70,46 @@ module SymmetricEncryption
50
70
  end
51
71
 
52
72
  # AES Symmetric Encryption of supplied string
73
+ # The String is encoded to UTF-8 prior to encryption
74
+ #
53
75
  # Returns result as a Base64 encoded string
54
76
  # Returns nil if the supplied str is nil
55
77
  # Returns "" if it is a string and it is empty
56
- #
57
- # options:
58
- # :encoding
59
- # :base64 Return as a base64 encoded string
60
- # :binary Return as raw binary data string. Note: String can contain embedded nulls
61
- # Default: :base64
62
- # :compress
63
- # [true|false] Whether or not to compress the data _before_ encrypting
64
- # Default: false
65
- def encrypt(str)
66
- return if str.nil?
67
- buf = str.to_s
68
- return str if buf.empty?
69
- crypt(:encrypt, buf)
78
+ if defined?(Encoding)
79
+ def encrypt(str)
80
+ return if str.nil?
81
+ buf = str.to_s.encode(SymmetricEncryption::UTF8_ENCODING)
82
+ return str if buf.empty?
83
+ crypt(:encrypt, buf)
84
+ end
85
+ else
86
+ def encrypt(str)
87
+ return if str.nil?
88
+ buf = str.to_s
89
+ return str if buf.empty?
90
+ crypt(:encrypt, buf)
91
+ end
70
92
  end
71
93
 
72
94
  # AES Symmetric Decryption of supplied string
73
- # Returns decrypted string
95
+ # The encoding of the supplied string is ignored since it must be binary data
96
+ # Returns a UTF-8 encoded, decrypted string
74
97
  # Returns nil if the supplied str is nil
75
98
  # Returns "" if it is a string and it is empty
76
- def decrypt(str)
77
- return if str.nil?
78
- buf = str.to_s
79
- return str if buf.empty?
80
- crypt(:decrypt, buf)
99
+ if defined?(Encoding)
100
+ def decrypt(str)
101
+ return if str.nil?
102
+ buf = str.to_s.force_encoding(SymmetricEncryption::BINARY_ENCODING)
103
+ return str if buf.empty?
104
+ crypt(:decrypt, buf).force_encoding(SymmetricEncryption::UTF8_ENCODING)
105
+ end
106
+ else
107
+ def decrypt(str)
108
+ return if str.nil?
109
+ buf = str.to_s
110
+ return str if buf.empty?
111
+ crypt(:decrypt, buf)
112
+ end
81
113
  end
82
114
 
83
115
  # Return a new random key using the configured cipher
@@ -104,6 +136,7 @@ module SymmetricEncryption
104
136
 
105
137
  # Creates a new OpenSSL::Cipher with every call so that this call
106
138
  # is thread-safe
139
+ # Return a binary encoded decrypted or encrypted string
107
140
  def crypt(cipher_method, string) #:nodoc:
108
141
  openssl_cipher = ::OpenSSL::Cipher.new(self.cipher)
109
142
  openssl_cipher.send(cipher_method)
@@ -74,7 +74,7 @@ module SymmetricEncryption
74
74
  # end
75
75
  def self.open(filename_or_stream, options={}, &block)
76
76
  raise "options must be a hash" unless options.respond_to?(:each_pair)
77
- mode = options.fetch(:mode, 'r')
77
+ mode = options.fetch(:mode, 'rb')
78
78
  compress = options.fetch(:compress, false)
79
79
  ios = filename_or_stream.is_a?(String) ? ::File.open(filename_or_stream, mode) : filename_or_stream
80
80
 
@@ -160,9 +160,6 @@ module SymmetricEncryption
160
160
  true
161
161
  end
162
162
 
163
- # Future: Generate private key in config file generator
164
- #new_key = OpenSSL::PKey::RSA.generate(2048)
165
-
166
163
  # Generate new random symmetric keys for use with this Encryption library
167
164
  #
168
165
  # Note: Only the current Encryption key settings are used
@@ -310,4 +307,10 @@ module SymmetricEncryption
310
307
  )
311
308
  end
312
309
 
310
+ # With Ruby 1.9 strings have encodings
311
+ if defined?(Encoding)
312
+ BINARY_ENCODING = Encoding.find("binary")
313
+ UTF8_ENCODING = Encoding.find("UTF-8")
314
+ end
315
+
313
316
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module SymmetricEncryption #:nodoc
3
- VERSION = "0.8.0"
3
+ VERSION = "0.9.0"
4
4
  end
@@ -66,7 +66,7 @@ module SymmetricEncryption
66
66
  # end
67
67
  def self.open(filename_or_stream, options={}, &block)
68
68
  raise "options must be a hash" unless options.respond_to?(:each_pair)
69
- mode = options.fetch(:mode, 'w')
69
+ mode = options.fetch(:mode, 'wb')
70
70
  compress = options.fetch(:compress, false)
71
71
  ios = filename_or_stream.is_a?(String) ? ::File.open(filename_or_stream, mode) : filename_or_stream
72
72
 
File without changes
@@ -0,0 +1,3 @@
1
+ file.reference.symmetry-lib=/Users/rmorrison/Sandbox/symmetry/lib
2
+ file.reference.symmetry-test=/Users/rmorrison/Sandbox/symmetry/test
3
+ platform.active=JRuby
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project-private xmlns="http://www.netbeans.org/ns/project-private/1">
3
+ <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1">
4
+ <file>
5
+ <url>lib/symmetric/encryption.rb</url>
6
+ <line>62</line>
7
+ </file>
8
+ <file>
9
+ <url>lib/symmetric_encryption/symmetric_encryption.rb</url>
10
+ <line>75</line>
11
+ </file>
12
+ <file>
13
+ <url>lib/symmetric_encryption/encryption.rb</url>
14
+ <line>60</line>
15
+ </file>
16
+ </editor-bookmarks>
17
+ </project-private>
@@ -0,0 +1,4 @@
1
+ clean=Remove any temporary products.
2
+ clobber=Remove any generated file.
3
+ gem=Build gem
4
+ test=Run Test Suite
@@ -0,0 +1,9 @@
1
+ file.reference.symmetry-lib=lib
2
+ file.reference.symmetry-test=test
3
+ javac.classpath=
4
+ main.file=
5
+ platform.active=JRuby
6
+ source.encoding=UTF-8
7
+ src.examples.dir=examples
8
+ src.lib.dir=lib
9
+ test.test.dir=test
@@ -0,0 +1,16 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project xmlns="http://www.netbeans.org/ns/project/1">
3
+ <type>org.netbeans.modules.ruby.rubyproject</type>
4
+ <configuration>
5
+ <data xmlns="http://www.netbeans.org/ns/ruby-project/1">
6
+ <name>symmetric-encryption</name>
7
+ <source-roots>
8
+ <root id="src.lib.dir" name="Source Files"/>
9
+ <root id="src.examples.dir" name="Examples"/>
10
+ </source-roots>
11
+ <test-roots>
12
+ <root id="test.test.dir" name="Test Files"/>
13
+ </test-roots>
14
+ </data>
15
+ </configuration>
16
+ </project>
data/test/cipher_test.rb CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
4
4
  require 'rubygems'
5
5
  require 'test/unit'
6
6
  require 'shoulda'
7
- require 'symmetric_encryption/cipher'
7
+ require 'symmetric_encryption'
8
8
 
9
9
  # Unit Test for SymmetricEncryption::Cipher
10
10
  #
@@ -24,7 +24,13 @@ class CipherTest < Test::Unit::TestCase
24
24
  cipher = SymmetricEncryption::Cipher.new(
25
25
  :key => '1234567890ABCDEF1234567890ABCDEF'
26
26
  )
27
- assert_equal "\302<\351\227oj\372\3331\310\260V\001\v'\346", cipher.encrypt('Hello World')
27
+ result = "\302<\351\227oj\372\3331\310\260V\001\v'\346"
28
+ # Note: This test fails on JRuby 1.7 RC1 since it's OpenSSL
29
+ # behaves differently when no IV is supplied.
30
+ # It instead encrypts to the following value:
31
+ # result = "0h\x92\x88\xA1\xFE\x8D\xF5\xF3v\x82\xAF(P\x83Y"
32
+ result.force_encoding('binary') if defined?(Encoding)
33
+ assert_equal result, cipher.encrypt('Hello World')
28
34
  end
29
35
 
30
36
  should "throw an exception on bad data" do
@@ -47,7 +53,10 @@ class CipherTest < Test::Unit::TestCase
47
53
  :iv => '1234567890ABCDEF'
48
54
  )
49
55
  @social_security_number = "987654321"
56
+
50
57
  @social_security_number_encrypted = "A\335*\314\336\250V\340\023%\000S\177\305\372\266"
58
+ @social_security_number_encrypted.force_encoding('binary') if defined?(Encoding)
59
+
51
60
  @sample_data = [
52
61
  { :text => '555052345', :encrypted => ''}
53
62
  ]
@@ -65,5 +74,17 @@ class CipherTest < Test::Unit::TestCase
65
74
  assert_equal @social_security_number, @cipher.decrypt(@social_security_number_encrypted)
66
75
  end
67
76
 
77
+ if defined?(Encoding)
78
+ context "on Ruby 1.9" do
79
+ should "encode encrypted data as binary" do
80
+ assert_equal Encoding.find('binary'), @cipher.encrypt(@social_security_number).encoding
81
+ end
82
+
83
+ should "decode encrypted data as utf-8" do
84
+ assert_equal Encoding.find('utf-8'), @cipher.decrypt(@cipher.encrypt(@social_security_number)).encoding
85
+ end
86
+ end
87
+ end
88
+
68
89
  end
69
90
  end
File without changes
@@ -0,0 +1,9 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ hosts:
5
+ - localhost:27017
6
+ # All Dates and Times should be returned in UTC, not the local timezone
7
+ use_utc: true
8
+ persist_in_safe_mode: true
9
+ database: symmetric_encryption_test
@@ -6,13 +6,15 @@ require 'logger'
6
6
  require 'erb'
7
7
  require 'test/unit'
8
8
  require 'shoulda'
9
- # Since we want both the AR and Mongoid extensions loaded we need to require them first
9
+ # Since we want both the Mongoid extensions loaded we need to require it first
10
10
  require 'active_record'
11
11
  require 'mongoid'
12
12
  require 'symmetric-encryption'
13
+ require 'symmetric_encryption/extensions/mongoid/fields'
13
14
 
14
15
  Mongoid.logger = Logger.new($stdout)
15
- Mongoid.load!("test/config/mongoid.yml")
16
+ filename = defined?(Mongoid::VERSION) ? "test/config/mongoid_v3.yml" : "test/config/mongoid_v2.yml"
17
+ Mongoid.load!(filename)
16
18
 
17
19
  class MongoidUser
18
20
  include Mongoid::Document
data/test/reader_test.rb CHANGED
@@ -69,8 +69,8 @@ class ReaderTest < Test::Unit::TestCase
69
69
 
70
70
  context "reading from file" do
71
71
  # With and without header
72
- [{:header => false}, {:header => true}, {:header => true, :compress => true}].each do |options|
73
- context "with#{'out' unless options[:header]} header" do
72
+ [{:header => false}, {:compress => false}, {:compress => true}].each_with_index do |options, i|
73
+ context "with#{'out' unless options[:header]} header #{i}" do
74
74
  setup do
75
75
  @filename = '._test'
76
76
  # Create encrypted file
data/test/writer_test.rb CHANGED
@@ -36,7 +36,9 @@ class EncryptionWriterTest < Test::Unit::TestCase
36
36
  file.close
37
37
 
38
38
  assert_equal @data_len, written_len
39
- assert_equal @data_encrypted, stream.string
39
+ result = stream.string
40
+ result.force_encoding('binary') if defined?(Encoding)
41
+ assert_equal @data_encrypted, result
40
42
  end
41
43
 
42
44
  should "encrypt to string stream using .open" do
@@ -54,7 +56,7 @@ class EncryptionWriterTest < Test::Unit::TestCase
54
56
  written_len = @data.inject(0) {|sum,str| sum + file.write(str)}
55
57
  end
56
58
  assert_equal @data_len, written_len
57
- assert_equal @data_encrypted, File.read(@filename)
59
+ assert_equal @data_encrypted, File.open(@filename, 'rb') {|f| f.read }
58
60
  end
59
61
  end
60
62
  end
metadata CHANGED
@@ -1,37 +1,27 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: symmetric-encryption
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 8
8
- - 0
9
- version: 0.8.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Reid Morrison
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2012-08-28 00:00:00 -04:00
18
- default_executable:
12
+ date: 2012-09-26 00:00:00.000000000 Z
19
13
  dependencies: []
20
-
21
- description: SymmetricEncryption supports encrypting ActiveRecord data, Mongoid data, passwords in configuration files, encrypting and decrypting of large files through streaming
22
- email:
14
+ description: SymmetricEncryption supports encrypting ActiveRecord data, Mongoid data,
15
+ passwords in configuration files, encrypting and decrypting of large files through
16
+ streaming
17
+ email:
23
18
  - reidmo@gmail.com
24
19
  executables: []
25
-
26
20
  extensions: []
27
-
28
21
  extra_rdoc_files: []
29
-
30
- files:
31
- - csv.zip
32
- - csv_encrypted
33
- - csv_encrypted_zip
22
+ files:
34
23
  - examples/symmetric-encryption.yml
24
+ - foo.zip
35
25
  - lib/rails/generators/symmetric_encryption/config/config_generator.rb
36
26
  - lib/rails/generators/symmetric_encryption/config/templates/symmetric-encryption.yml
37
27
  - lib/rails/generators/symmetric_encryption/new_keys/new_keys_generator.rb
@@ -46,13 +36,21 @@ files:
46
36
  - lib/symmetric_encryption/symmetric_encryption.rb
47
37
  - lib/symmetric_encryption/version.rb
48
38
  - lib/symmetric_encryption/writer.rb
39
+ - lib/symmetric_encryption.rb
49
40
  - LICENSE.txt
41
+ - nbproject/private/config.properties
42
+ - nbproject/private/private.properties
43
+ - nbproject/private/private.xml
44
+ - nbproject/private/rake-d.txt
45
+ - nbproject/project.properties
46
+ - nbproject/project.xml
50
47
  - Rakefile
51
48
  - README.md
52
49
  - test/attr_encrypted_test.rb
53
50
  - test/cipher_test.rb
54
51
  - test/config/database.yml
55
- - test/config/mongoid.yml
52
+ - test/config/mongoid_v2.yml
53
+ - test/config/mongoid_v3.yml
56
54
  - test/config/symmetric-encryption.yml
57
55
  - test/config/test_new.iv
58
56
  - test/config/test_new.key
@@ -62,35 +60,28 @@ files:
62
60
  - test/reader_test.rb
63
61
  - test/symmetric_encryption_test.rb
64
62
  - test/writer_test.rb
65
- has_rdoc: true
66
63
  homepage: https://github.com/ClarityServices/symmetric-encryption
67
64
  licenses: []
68
-
69
65
  post_install_message:
70
66
  rdoc_options: []
71
-
72
- require_paths:
67
+ require_paths:
73
68
  - lib
74
- required_ruby_version: !ruby/object:Gem::Requirement
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- segments:
79
- - 0
80
- version: "0"
81
- required_rubygems_version: !ruby/object:Gem::Requirement
82
- requirements:
83
- - - ">="
84
- - !ruby/object:Gem::Version
85
- segments:
86
- - 0
87
- version: "0"
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
88
81
  requirements: []
89
-
90
82
  rubyforge_project:
91
- rubygems_version: 1.3.6
83
+ rubygems_version: 1.8.24
92
84
  signing_key:
93
85
  specification_version: 3
94
86
  summary: Symmetric Encryption for Ruby, and Ruby on Rails
95
87
  test_files: []
96
-
data/csv.zip DELETED
Binary file
data/csv_encrypted DELETED
Binary file
data/csv_encrypted_zip DELETED
Binary file