Skip to content

Commit f3439cc

Browse files
authored
Merge pull request #8 from cheerful/ruby-2.4-and-above-support
Ruby 2.4+ Support
2 parents 73e3b9e + be32f13 commit f3439cc

6 files changed

Lines changed: 86 additions & 46 deletions

File tree

.travis.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
sudo: false
22
language: ruby
33
rvm:
4-
- 1.8.7
5-
- ree
6-
- 1.9.2
7-
- 1.9.3-p551
8-
- 2.0.0-p598
94
- 2.1.0
105
- 2.1.5
116
- 2.2.0
127
- 2.2.1
138
- 2.2.1-clang
9+
- 2.4.3
10+
- 2.5.5
11+
- 2.6.2
1412
- ruby-head
1513
- ruby-head-clang
1614
- jruby-19mode
1715
- jruby-head
16+
bundler_args: --binstubs rake
1817
notifications:
1918
recipients:
2019
- thomas@slash7.com

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ that doesn't have other authentication or persistence mechanisms (like cookies):
2020
* Links that come with an expiration date (à la S3)
2121
* Mini-apps that don't persist data on the server
2222

23-
Works with Ruby 1.8, 1.9 and 2.0.
23+
Works with Ruby 2.1+
2424

2525
**Important**: As a general guideline, URL lengths shouldn't exceed about 2000
2626
characters in length, as URLs longer than that will not work in some browsers
@@ -29,7 +29,7 @@ with URLcrypt.
2929

3030
**WORD OF WARNING: THERE IS NO GUARANTEE WHATSOEVER THAT THIS GEM IS ACTUALLY SECURE AND WORKS. USE AT YOUR OWN RISK.**
3131

32-
URLcrypt is an extraction from [Freckle Time Tracking](http://letsfreckle.com/),
32+
URLcrypt is an extraction from [Noko Time Tracking](https://nokotime.com),
3333
where it is used to generate URLs for dynamically generated images in emails.
3434

3535
Patches are welcome; please include tests!

lib/URLcrypt.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def self.encrypt(data)
7272
def self.cipher(mode)
7373
cipher = OpenSSL::Cipher.new('aes-256-cbc')
7474
cipher.send(mode)
75-
cipher.key = @key
75+
cipher.key = @key.byteslice(0,cipher.key_len)
7676
cipher
7777
end
7878

test/URLcrypt_test.rb

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,7 @@
11
# encoding: utf-8
2-
require 'bundler'
3-
Bundler.require(:default, :test)
4-
5-
require 'coveralls'
6-
Coveralls.wear!
7-
8-
require 'test/unit'
9-
10-
class TestURLcrypt < Test::Unit::TestCase
11-
12-
require 'URLcrypt'
13-
14-
def assert_bytes_equal(string1, string2)
15-
bytes1 = string1.bytes.to_a.join(':')
16-
bytes2 = string2.bytes.to_a.join(':')
17-
assert_equal(bytes1, bytes2)
18-
end
19-
20-
def assert_decoding(encoded, plain)
21-
decoded = URLcrypt.decode(encoded)
22-
assert_bytes_equal(plain, decoded)
23-
end
24-
25-
def assert_encoding(encoded, plain)
26-
actual = URLcrypt.encode(plain)
27-
assert_bytes_equal(encoded, actual)
28-
end
29-
30-
def assert_encode_and_decode(encoded, plain)
31-
assert_encoding(encoded, plain)
32-
assert_decoding(encoded, plain)
33-
end
2+
require 'test_helper'
343

4+
class TestURLcrypt < TestClass
355
def test_empty_string
366
assert_encode_and_decode('', '')
377
end
@@ -41,11 +11,11 @@ def test_encode
4111
'111gc86f4nxw5zj1b3qmhpb14n5h25l4m7111',
4212
"\0\0awesome \n ü string\0\0")
4313
end
44-
14+
4515
def test_invalid_encoding
4616
assert_decoding('ZZZZZ', '')
4717
end
48-
18+
4919
def test_arbitrary_byte_strings
5020
0.step(1500,17) do |n|
5121
original = (0..n).map{rand(256).chr}.join
@@ -55,10 +25,12 @@ def test_arbitrary_byte_strings
5525
end
5626

5727
def test_encryption
58-
# this key was generated via rake secret in a rails app, the pack() converts it into a byte array
59-
URLcrypt::key =
60-
['d25883a27b9a639da85ea7e159b661218799c9efa63069fac13a6778c954fb6d721968887a19bdb01af8f59eb5a90d256bd9903355c20b0b4b39bf4048b9b17b'].pack('H*')
61-
28+
# pack() converts this secret into a byte array
29+
secret = ['d25883a27b9a639da85ea7e159b661218799c9efa63069fac13a6778c954fb6d'].pack('H*')
30+
URLcrypt::key = secret
31+
32+
assert_equal OpenSSL::Cipher.new('aes-256-cbc').key_len, secret.bytesize
33+
6234
original = "hello world!"
6335
encrypted = URLcrypt::encrypt(original)
6436
assert_equal(URLcrypt::decrypt(encrypted), original)

test/regression_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# encoding: utf-8
2+
class URLcryptRegressionTest < TestClass
3+
def test_encryption_and_decryption
4+
original = '{"some":"json_data","token":"dfsfsdfsdf"}'
5+
encrypted = URLcrypt.encrypt(original)
6+
7+
encrypted = URLcrypt::encrypt(original)
8+
assert_equal(URLcrypt::decrypt(encrypted), original)
9+
end
10+
11+
def test_encryption_with_too_long_key
12+
# this key was generated via rake secret in a rails app, the pack() converts it into a byte array
13+
secret = ['d25883a27b9a639da85ea7e159b661218799c9efa63069fac13a6778c954fb6d721968887a19bdb01af8f59eb5a90d256bd9903355c20b0b4b39bf4048b9b17b'].pack('H*')
14+
URLcrypt::key = secret
15+
16+
assert OpenSSL::Cipher.new('aes-256-cbc').key_len < secret.bytesize
17+
18+
original = "hello world!"
19+
encrypted = URLcrypt::encrypt(original)
20+
assert_equal(URLcrypt::decrypt(encrypted), original)
21+
end
22+
23+
def test_encryption_and_decryption_with_too_long_key
24+
# this key was generated via rake secret in a rails app, the pack() converts it into a byte array
25+
secret = ['d25883a27b9a639da85ea7e159b661218799c9efa63069fac13a6778c954fb6d721968887a19bdb01af8f59eb5a90d256bd9903355c20b0b4b39bf4048b9b17b'].pack('H*')
26+
URLcrypt::key = secret
27+
28+
assert OpenSSL::Cipher.new('aes-256-cbc').key_len < secret.bytesize
29+
30+
original = '{"some":"json_data","token":"dfsfsdfsdf"}'
31+
encrypted = URLcrypt.encrypt(original)
32+
33+
encrypted = URLcrypt::encrypt(original)
34+
assert_equal(URLcrypt::decrypt(encrypted), original)
35+
end
36+
end

test/test_helper.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# encoding: utf-8
2+
require 'bundler'
3+
Bundler.require(:default, :test)
4+
5+
require 'coveralls'
6+
Coveralls.wear!
7+
8+
require 'test/unit'
9+
10+
class TestClass < Test::Unit::TestCase
11+
require 'URLcrypt'
12+
13+
def assert_bytes_equal(string1, string2)
14+
bytes1 = string1.bytes.to_a.join(':')
15+
bytes2 = string2.bytes.to_a.join(':')
16+
assert_equal(bytes1, bytes2)
17+
end
18+
19+
def assert_decoding(encoded, plain)
20+
decoded = URLcrypt.decode(encoded)
21+
assert_bytes_equal(plain, decoded)
22+
end
23+
24+
def assert_encoding(encoded, plain)
25+
actual = URLcrypt.encode(plain)
26+
assert_bytes_equal(encoded, actual)
27+
end
28+
29+
def assert_encode_and_decode(encoded, plain)
30+
assert_encoding(encoded, plain)
31+
assert_decoding(encoded, plain)
32+
end
33+
end

0 commit comments

Comments
 (0)