encryptor 1.1.0 → 1.1.1

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.rdoc CHANGED
@@ -12,16 +12,38 @@ Used by http://github.com/shuber/attr_encrypted to easily encrypt/decrypt attrib
12
12
 
13
13
  == Usage
14
14
 
15
- secret_key = Digest::SHA256.hexdigest('a secret key')
15
+
16
+ === Basic
17
+
16
18
  encrypted_value = Encryptor.encrypt(:value => 'some string to encrypt', :key => secret_key) # '������{)��q�ށ�ܣ��q���Au/�ޜP'
17
19
  decrypted_value = Encryptor.decrypt(:value => encrypted_value, :key => secret_key) # 'some string to encrypt'
18
20
 
21
+ The value to encrypt or decrypt may also be passed as the first argument if you'd like.
22
+
23
+ secret_key = Digest::SHA256.hexdigest('a secret key')
24
+ encrypted_value = Encryptor.encrypt('some string to encrypt', :key => secret_key) # '������{)��q�ށ�ܣ��q���Au/�ޜP'
25
+ decrypted_value = Encryptor.decrypt(encrypted_value, :key => secret_key) # 'some string to encrypt'
26
+
19
27
  You may also pass the <tt>:iv</tt> and <tt>:algorithm</tt> options but they are not required. If an algorithm is not specified, the Encryptor uses
20
28
  the algorithm found at <tt>Encryptor.default_options[:algorithm]</tt> which is <tt>aes-256-cbc</tt> by default. You can change the default options
21
29
  by overwriting or merging this attribute:
22
30
 
23
31
  Encryptor.default_options.merge!(:algorithm => 'bf', :key => 'some default secret key')
24
32
 
33
+
34
+ === Strings
35
+
36
+ <tt>Encryptor</tt> adds <tt>encrypt</tt> and <tt>decrypt</tt> methods to <tt>String</tt> objects for your convenience. These two methods accept the same arguments as the associated ones in the <tt>Encryptor</tt> module. There nice when you set the default options in the <tt>Encryptor.default_options</tt> attribute. For example:
37
+
38
+ Encryptor.default_options.merge!(:key => 'some default secret key')
39
+ credit_card = 'xxxx xxxx xxxx 1234'
40
+ encrypted_credit_card = credit_card.encrypt
41
+
42
+ There's also <tt>encrypt!</tt> and <tt>decrypt!</tt> methods that replace the contents of a string with the encrypted or decrypted version of itself.
43
+
44
+
45
+ === Algorithms
46
+
25
47
  Run <tt>openssl list-cipher-commands</tt> in your terminal to view a list all cipher algorithms that are supported on your platform.
26
48
 
27
49
  aes-128-cbc
@@ -30,7 +52,6 @@ Run <tt>openssl list-cipher-commands</tt> in your terminal to view a list all ci
30
52
  aes-192-ecb
31
53
  aes-256-cbc
32
54
  aes-256-ecb
33
- base64
34
55
  bf
35
56
  bf-cbc
36
57
  bf-cfb
data/lib/encryptor.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'openssl'
2
+ require 'encryptor/string'
2
3
 
4
+ # A simple wrapper for the standard OpenSSL library
3
5
  module Encryptor
4
6
  # The default options to use when calling the <tt>encrypt</tt> and <tt>decrypt</tt> methods
5
7
  #
@@ -16,9 +18,11 @@ module Encryptor
16
18
  #
17
19
  # Example
18
20
  #
19
- # encrypted_value = Huberry::Encryptor.encrypt(:value => 'some string to encrypt', :key => 'some secret key')
20
- def self.encrypt(options)
21
- crypt :encrypt, options
21
+ # encrypted_value = Encryptor.encrypt(:value => 'some string to encrypt', :key => 'some secret key')
22
+ # # or
23
+ # encrypted_value = Encryptor.encrypt('some string to encrypt', :key => 'some secret key')
24
+ def self.encrypt(*args)
25
+ crypt :encrypt, *args
22
26
  end
23
27
 
24
28
  # Decrypts a <tt>:value</tt> with a specified <tt>:key</tt>
@@ -27,15 +31,17 @@ module Encryptor
27
31
  #
28
32
  # Example
29
33
  #
30
- # decrypted_value = Huberry::Encryptor.decrypt(:value => 'some encrypted string', :key => 'some secret key')
31
- def self.decrypt(options)
32
- crypt :decrypt, options
34
+ # decrypted_value = Encryptor.decrypt(:value => 'some encrypted string', :key => 'some secret key')
35
+ # # or
36
+ # decrypted_value = Encryptor.decrypt('some encrypted string', :key => 'some secret key')
37
+ def self.decrypt(*args)
38
+ crypt :decrypt, *args
33
39
  end
34
40
 
35
41
  protected
36
42
 
37
- def self.crypt(cipher_method, options = {})
38
- options = default_options.merge(options)
43
+ def self.crypt(cipher_method, *args) #:nodoc:
44
+ options = default_options.merge(:value => args.first).merge(args.last.is_a?(Hash) ? args.last : {})
39
45
  cipher = OpenSSL::Cipher::Cipher.new(options[:algorithm])
40
46
  cipher.send(cipher_method)
41
47
  if options[:iv]
@@ -47,4 +53,6 @@ module Encryptor
47
53
  result = cipher.update(options[:value])
48
54
  result << cipher.final
49
55
  end
50
- end
56
+ end
57
+
58
+ String.send :include, Encryptor::String
@@ -0,0 +1,24 @@
1
+ module Encryptor #:nodoc:
2
+ # Adds <tt>encrypt</tt> and <tt>decrypt</tt> methods to strings
3
+ module String
4
+ # Returns a new string containing the encrypted version of itself
5
+ def encrypt(options = {})
6
+ Encryptor.encrypt(options.merge(:value => self))
7
+ end
8
+
9
+ # Replaces the contents of a string with the encrypted version of itself
10
+ def encrypt!(options ={})
11
+ replace encrypt(options)
12
+ end
13
+
14
+ # Returns a new string containing the decrypted version of itself
15
+ def decrypt(options = {})
16
+ Encryptor.decrypt(options.merge(:value => self))
17
+ end
18
+
19
+ # Replaces the contents of a string with the decrypted version of itself
20
+ def decrypt!(options ={})
21
+ replace decrypt(options)
22
+ end
23
+ end
24
+ end
@@ -4,40 +4,90 @@ require File.dirname(__FILE__) + '/../lib/encryptor'
4
4
 
5
5
  class EncryptorTest < Test::Unit::TestCase
6
6
 
7
- algorithms = %x(openssl list-cipher-commands).split
8
- original_value = Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join)
7
+ algorithms = %x(openssl list-cipher-commands).split
9
8
  key = Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join)
10
9
  iv = Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join)
11
-
12
- algorithms.each do |algorithm|
13
- define_method "test_should_crypt_with_#{algorithm}_algorithm_with_iv" do
14
- encrypted_value = Encryptor.encrypt(:value => original_value, :key => key, :iv => iv, :algorithm => algorithm)
15
- assert_not_equal original_value, encrypted_value
16
- assert_not_equal Encryptor.encrypt(:value => original_value, :key => key, :algorithm => algorithm), encrypted_value
17
- assert_equal original_value, Encryptor.decrypt(:value => encrypted_value, :key => key, :iv => iv, :algorithm => algorithm)
10
+ original_value = Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join)
11
+
12
+ algorithms.reject { |algorithm| algorithm == 'base64' }.each do |algorithm|
13
+ encrypted_value_with_iv = Encryptor.encrypt(:value => original_value, :key => key, :iv => iv, :algorithm => algorithm)
14
+ encrypted_value_without_iv = Encryptor.encrypt(:value => original_value, :key => key, :algorithm => algorithm)
15
+
16
+ define_method "test_should_crypt_with_the_#{algorithm}_algorithm_with_iv" do
17
+ assert_not_equal original_value, encrypted_value_with_iv
18
+ assert_not_equal encrypted_value_without_iv, encrypted_value_with_iv
19
+ assert_equal original_value, Encryptor.decrypt(:value => encrypted_value_with_iv, :key => key, :iv => iv, :algorithm => algorithm)
18
20
  end
19
21
 
20
- define_method "test_should_crypt_with_#{algorithm}_algorithm_without_iv" do
21
- encrypted_value = Encryptor.encrypt(:value => original_value, :key => key, :algorithm => algorithm)
22
- assert_not_equal original_value, encrypted_value
23
- assert_equal original_value, Encryptor.decrypt(:value => encrypted_value, :key => key, :algorithm => algorithm)
22
+ define_method "test_should_crypt_with_the_#{algorithm}_algorithm_without_iv" do
23
+ assert_not_equal original_value, encrypted_value_without_iv
24
+ assert_equal original_value, Encryptor.decrypt(:value => encrypted_value_without_iv, :key => key, :algorithm => algorithm)
25
+ end
26
+
27
+ define_method "test_should_encrypt_with_the_#{algorithm}_algorithm_with_iv_with_the_first_arg_as_the_value" do
28
+ assert_equal encrypted_value_with_iv, Encryptor.encrypt(original_value, :key => key, :iv => iv, :algorithm => algorithm)
29
+ end
30
+
31
+ define_method "test_should_encrypt_with_the_#{algorithm}_algorithm_without_iv_with_the_first_arg_as_the_value" do
32
+ assert_equal encrypted_value_without_iv, Encryptor.encrypt(original_value, :key => key, :algorithm => algorithm)
33
+ end
34
+
35
+ define_method "test_should_decrypt_with_the_#{algorithm}_algorithm_with_iv_with_the_first_arg_as_the_value" do
36
+ assert_equal original_value, Encryptor.decrypt(encrypted_value_with_iv, :key => key, :iv => iv, :algorithm => algorithm)
37
+ end
38
+
39
+ define_method "test_should_decrypt_with_the_#{algorithm}_algorithm_without_iv_with_the_first_arg_as_the_value" do
40
+ assert_equal original_value, Encryptor.decrypt(encrypted_value_without_iv, :key => key, :algorithm => algorithm)
41
+ end
42
+
43
+ define_method "test_should_call_encrypt_on_a_string_with_the_#{algorithm}_algorithm_with_iv" do
44
+ assert_equal encrypted_value_with_iv, original_value.encrypt(:key => key, :iv => iv, :algorithm => algorithm)
45
+ end
46
+
47
+ define_method "test_should_call_encrypt_on_a_string_with_the_#{algorithm}_algorithm_without_iv" do
48
+ assert_equal encrypted_value_without_iv, original_value.encrypt(:key => key, :algorithm => algorithm)
49
+ end
50
+
51
+ define_method "test_should_call_decrypt_on_a_string_with_the_#{algorithm}_algorithm_with_iv" do
52
+ assert_equal original_value, encrypted_value_with_iv.decrypt(:key => key, :iv => iv, :algorithm => algorithm)
53
+ end
54
+
55
+ define_method "test_should_call_decrypt_on_a_string_with_the_#{algorithm}_algorithm_without_iv" do
56
+ assert_equal original_value, encrypted_value_without_iv.decrypt(:key => key, :algorithm => algorithm)
57
+ end
58
+
59
+ define_method "test_string_encrypt!_on_a_string_with_the_#{algorithm}_algorithm_with_iv" do
60
+ original_value_dup = original_value.dup
61
+ original_value_dup.encrypt!(:key => key, :iv => iv, :algorithm => algorithm)
62
+ assert_equal original_value.encrypt(:key => key, :iv => iv, :algorithm => algorithm), original_value_dup
63
+ end
64
+
65
+ define_method "test_string_encrypt!_on_a_string_with_the_#{algorithm}_algorithm_without_iv" do
66
+ original_value_dup = original_value.dup
67
+ original_value_dup.encrypt!(:key => key, :algorithm => algorithm)
68
+ assert_equal original_value.encrypt(:key => key, :algorithm => algorithm), original_value_dup
69
+ end
70
+
71
+ define_method "test_string_decrypt!_on_a_string_with_the_#{algorithm}_algorithm_with_iv" do
72
+ encrypted_value_with_iv_dup = encrypted_value_with_iv.dup
73
+ encrypted_value_with_iv_dup.decrypt!(:key => key, :iv => iv, :algorithm => algorithm)
74
+ assert_equal original_value, encrypted_value_with_iv_dup
75
+ end
76
+
77
+ define_method "test_string_decrypt!_on_a_string_with_the_#{algorithm}_algorithm_without_iv" do
78
+ encrypted_value_without_iv_dup = encrypted_value_without_iv.dup
79
+ encrypted_value_without_iv_dup.decrypt!(:key => key, :algorithm => algorithm)
80
+ assert_equal original_value, encrypted_value_without_iv_dup
24
81
  end
25
- end
26
-
27
- define_method 'test_should_have_a_default_algorithm' do
28
- assert algorithms.include?(Encryptor.default_options[:algorithm])
29
82
  end
30
83
 
31
84
  define_method 'test_should_use_the_default_algorithm_if_one_is_not_specified' do
32
85
  assert_equal Encryptor.encrypt(:value => original_value, :key => key, :algorithm => Encryptor.default_options[:algorithm]), Encryptor.encrypt(:value => original_value, :key => key)
33
86
  end
34
87
 
35
- def test_should_be_able_to_change_the_default_algorithm
36
- original_algorithm = Encryptor.default_options[:algorithm]
37
- assert_not_equal 'test', original_algorithm
38
- Encryptor.default_options[:algorithm] = 'test'
39
- assert_equal 'test', Encryptor.default_options[:algorithm]
40
- Encryptor.default_options[:algorithm] = original_algorithm
88
+ def test_should_have_a_default_algorithm
89
+ assert !Encryptor.default_options[:algorithm].nil?
90
+ assert !Encryptor.default_options[:algorithm].empty?
41
91
  end
42
92
 
43
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: encryptor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Huber
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-28 00:00:00 -08:00
12
+ date: 2010-01-29 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,6 +23,7 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - lib/encryptor.rb
26
+ - lib/encryptor/string.rb
26
27
  - MIT-LICENSE
27
28
  - Rakefile
28
29
  - README.rdoc