lib_string_encryption 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -1,9 +1,9 @@
1
1
  String.class_eval do
2
- def encrypt
3
- StringEncryption.encrypt(self)
2
+ def encrypt key=ENV['LIB_STRING_ENCRYPTION_KEY']
3
+ StringEncryption.encrypt(self, key)
4
4
  end
5
5
 
6
- def decrypt
7
- StringEncryption.decrypt(self)
6
+ def decrypt key=ENV['LIB_STRING_ENCRYPTION_KEY']
7
+ StringEncryption.decrypt(self, key)
8
8
  end
9
9
  end
@@ -2,10 +2,10 @@ require 'base64'
2
2
  require 'uri'
3
3
 
4
4
  module StringEncryption
5
- def self.decrypt(encrypted_data)
5
+ def self.decrypt(encrypted_data, decryption_key=ENV['LIB_STRING_ENCRYPTION_KEY'])
6
6
  des = OpenSSL::Cipher::Cipher.new("des-ede3-cbc")
7
7
  des.decrypt
8
- des.key = ENV['LIB_STRING_ENCRYPTION_KEY']
8
+ des.key = decryption_key
9
9
  encrypted_data = URI.unescape(encrypted_data)
10
10
  encrypted_data = Base64.decode64(encrypted_data)
11
11
 
@@ -14,10 +14,10 @@ module StringEncryption
14
14
  des.update(encrypted_data) + des.final
15
15
  end
16
16
 
17
- def self.encrypt(string)
17
+ def self.encrypt(string, encryption_key=ENV['LIB_STRING_ENCRYPTION_KEY'])
18
18
  des = OpenSSL::Cipher::Cipher.new("des-ede3-cbc")
19
19
  des.encrypt
20
- des.key = ENV['LIB_STRING_ENCRYPTION_KEY']
20
+ des.key = encryption_key
21
21
 
22
22
  des.iv = iv = SecureRandom.hex(4)
23
23
 
@@ -2,10 +2,28 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe 'String Encryption' do
4
4
  it "should encrypt strings" do
5
- "Hello World".should_not == "Hello World".encrypt
5
+ string_value.should_not == string_value.encrypt
6
6
  end
7
-
7
+
8
8
  it "should decrypt a string that's been encrypted" do
9
- "Hello World".encrypt.decrypt.should == "Hello World"
10
- end
9
+ string_value.encrypt.decrypt.should == string_value
10
+ end
11
+
12
+ it "should be able to encrypt with a given key" do
13
+ key = StringEncryption::SecureRandom.base64(60)
14
+ string_value.encrypt(key).decrypt(key).should == string_value
15
+ end
16
+
17
+ it "should fail to decrypt a string encrypted with the wrong key" do
18
+ value = string_value.encrypt
19
+ original_encryption_string = ENV['LIB_STRING_ENCRYPTION_KEY']
20
+ new_key = StringEncryption::SecureRandom.random_bytes(95)
21
+ string_value.encrypt(new_key).decrypt(new_key).should == string_value
22
+ lambda {string_value.encrypt(original_encryption_string).decrypt(new_key)}.should raise_error(OpenSSL::Cipher::CipherError)
23
+ lambda {string_value.encrypt(new_key).decrypt(original_encryption_string)}.should raise_error(OpenSSL::Cipher::CipherError)
24
+ end
25
+
26
+ def string_value
27
+ "Hello World"
28
+ end
11
29
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jason Rogers