Skip to content

Commit ef725e2

Browse files
committed
Use Array#pack/String#unpack instead of Base64 to be compatible with Ruby 1.9+
1 parent 3f7b064 commit ef725e2

6 files changed

Lines changed: 11 additions & 9 deletions

File tree

‎CHANGELOG.rdoc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
== master
22

3+
* Use pack/unpack instead of Base64 to be compatible with Ruby 1.9+
4+
35
== 0.3.1 / 2008-12-4
46

57
* Fix symmetric ciphers not working on Ruby 1.8.6 and below

‎lib/encrypted_strings.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'openssl'
2+
13
require 'encrypted_strings/extensions/string'
24
require 'encrypted_strings/cipher'
35
require 'encrypted_strings/symmetric_cipher'

‎lib/encrypted_strings/asymmetric_cipher.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ def encrypt(data)
107107
raise NoPublicKeyError, "Public key file: #{public_key_file}" unless public?
108108

109109
encrypted_data = public_rsa.public_encrypt(data)
110-
Base64.encode64(encrypted_data)
110+
[encrypted_data].pack('m')
111111
end
112112

113113
# Decrypts the given data. If no private key file has been specified, then
114114
# a NoPrivateKeyError will be raised.
115115
def decrypt(data)
116116
raise NoPrivateKeyError, "Private key file: #{private_key_file}" unless private?
117117

118-
decrypted_data = Base64.decode64(data)
118+
decrypted_data = data.unpack('m')[0]
119119
private_rsa.private_decrypt(decrypted_data)
120120
end
121121

‎lib/encrypted_strings/extensions/string.rb‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
require 'openssl'
2-
require 'base64'
3-
41
module EncryptedStrings
52
module Extensions #:nodoc:
63
# Adds support for in-place encryption/decryption of strings

‎lib/encrypted_strings/symmetric_cipher.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ def initialize(options = {})
8181
# Decrypts the current string using the current key and algorithm specified
8282
def decrypt(data)
8383
cipher = build_cipher(:decrypt)
84-
cipher.update(Base64.decode64(data)) + cipher.final
84+
cipher.update(data.unpack('m')[0]) + cipher.final
8585
end
8686

8787
# Encrypts the current string using the current key and algorithm specified
8888
def encrypt(data)
8989
cipher = build_cipher(:encrypt)
90-
Base64.encode64(cipher.update(data) + cipher.final)
90+
[cipher.update(data) + cipher.final].pack('m')
9191
end
9292

9393
private

‎test/sha_cipher_test.rb‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ class ShaCipherWithNonStringSaltTest < Test::Unit::TestCase
7272
require 'time'
7373

7474
def setup
75-
@sha_cipher = EncryptedStrings::ShaCipher.new(:salt => Time.parse('Tue Jan 01 00:00:00 UTC 2008'))
75+
@time = Time.parse('Tue Jan 01 00:00:00 UTC 2008')
76+
@sha_cipher = EncryptedStrings::ShaCipher.new(:salt => @time)
7677
end
7778

7879
def test_should_stringify_salt
79-
assert_equal 'Tue Jan 01 00:00:00 UTC 2008', @sha_cipher.salt
80+
assert_equal @time.to_s, @sha_cipher.salt
8081
end
8182
end

0 commit comments

Comments
 (0)