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 +1 -1
- data/lib/string_encryption/core_ext.rb +4 -4
- data/lib/string_encryption/encryption.rb +4 -4
- data/spec/lib_string_encryption_spec.rb +22 -4
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
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 =
|
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 =
|
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
|
-
|
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
|
-
|
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
|