gibberish 0.0.2 → 1.0.0

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.
@@ -0,0 +1,3 @@
1
+ [submodule "doc"]
2
+ path = doc
3
+ url = git@github.com:mdp/gibberish.git
@@ -0,0 +1,6 @@
1
+ lib/gibberish.rb 802b1397065b8f34b094cd5e797446a42c2c9b7e
2
+ lib/gibberish/rsa.rb 011d6178967c8f41bcafe8be060d5aad41c7ebc4
3
+ lib/gibberish/aes.rb c0f393617c375e47516948a7540eb2bd9f9a261c
4
+ lib/gibberish/hmac.rb ef42bd281f69be2e4073cd844d02ff26b7247c05
5
+ lib/gibberish/digest.rb 37e1d1c4daa2ce783e26c7920aa927dc4c869f4e
6
+ lib/gibberish/version.rb 698d6220529ab0bfd8853ae631aa0eb5b75ef934
Binary file
@@ -0,0 +1,2 @@
1
+ {I" Object:EF:
2
+ class
@@ -0,0 +1,2 @@
1
+ --output-dir doc
2
+ --markup markdown
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
  gemspec
3
- gem 'rspec'
3
+ gem 'minitest'
4
+ gem 'mini_shoulda'
@@ -1,24 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gibberish (0.0.1)
4
+ gibberish (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
- diff-lcs (1.1.2)
10
- rspec (2.5.0)
11
- rspec-core (~> 2.5.0)
12
- rspec-expectations (~> 2.5.0)
13
- rspec-mocks (~> 2.5.0)
14
- rspec-core (2.5.1)
15
- rspec-expectations (2.5.0)
16
- diff-lcs (~> 1.1.2)
17
- rspec-mocks (2.5.0)
9
+ mini_shoulda (0.2.0)
10
+ minitest (~> 2.0.2)
11
+ minitest (2.0.2)
18
12
 
19
13
  PLATFORMS
20
14
  ruby
21
15
 
22
16
  DEPENDENCIES
23
17
  gibberish!
24
- rspec
18
+ mini_shoulda
19
+ minitest
@@ -1,18 +1,31 @@
1
- # Gibberish - Stop looking up encryption code snippets!
1
+ # Gibberish - Encryption in Ruby made simple
2
2
 
3
- Gibberish is an opinionated cryptography library for Ruby. Its objective is easy to use
4
- encryption in Ruby while remaining secure.
3
+ ### What
4
+ Gibberish is an opinionated cryptography library for Ruby. Its objective is easy but secure
5
+ encryption in Ruby.
5
6
 
6
- ## Digests
7
+ ### Why
8
+ While OpenSSL is an extremely capable encryption library, it lacks a terse and clean
9
+ interface in Ruby.
7
10
 
8
- Gibberish::MD5("somedata")
9
- #=> aefaf7502d52994c3b01957636a3cdd2
11
+ ### Goals
12
+ - This library should remain easily iteroperable with the OpenSSL command
13
+ line interface. Each function will include documentation on how to perform
14
+ the same routine via the command line with OpenSSL
10
15
 
11
- Gibberish::SHA1("somedata")
12
- #=> efaa311ae448a7374c122061bfed952d940e9e37
16
+ - It should default to a reasonably secure setting, e.g. 256-bit AES, or SHA1 for HMAC
17
+ But it should allow the user to specify a stronger setting, within reason.
13
18
 
14
- Gibberish::SHA256("somedata")
15
- #=> 87d149cb424c0387656f211d2589fb5b1e16229921309e98588419ccca8a7362
19
+ - Procedures should be well tested and be compatible with Ruby 1.8.7 and 1.9
20
+
21
+
22
+ ## Requirements
23
+
24
+ Ruby compiled with OpenSSL support
25
+
26
+ ## Installation
27
+
28
+ gem install gibberish
16
29
 
17
30
  ## AES
18
31
 
@@ -30,18 +43,42 @@ Gibberish AES is fully compatible with default OpenSSL on the command line
30
43
  echo "U2FsdGVkX187oKRbgDkUcMKaFfB5RsXQj/X4mc8X3lsUVgwb4+S55LQo6f6N\nIDMX\n" | \
31
44
  openssl enc -d -aes-256-cbc -a -k p4ssw0rd
32
45
 
33
- ## HMAC
34
-
35
- Defaults to 256 bit digest
36
-
37
- Gibberish::HMAC("key", "some data")
38
- #=> 521677c580722c5c52fa15d978e8656341c4f3c5
46
+ [Find out more](http://rdoc.info/github/mdp/gibberish/master/Gibberish/AES)
39
47
 
40
48
  ## RSA
41
49
 
42
50
  k = Gibberish::RSA.generate_keypair(1024)
43
- cipher = Gibberish::RSA.new(k.public_key, k.private_key)
51
+ cipher = Gibberish::RSA.new(k.public_key)
44
52
  enc = cipher.encrypt("Some data")
45
53
  # Defaults to Base64 output
46
54
  #=> "JKm98wKyJljqmpx7kP8ZsdeXiShllEMcRHVnjUjc4ecyYK/doKAkVTLho1Gp\ng697qrljyClF0AcIH+XZmeF/TrqYUuCEUyhOD6OL1bs5dn8vFQefS5KdaC5Y\ndLADvh3mSfE/w/gs4vaf/OtbZNBeSl6ROCZasWTfRewp4n1RDmE=\n"
55
+ cipher = Gibberish::RSA.new(k.private_key)
47
56
  dec = cipher.decrypt(enc)
57
+
58
+ [Find out more](http://rdoc.info/github/mdp/gibberish/master/Gibberish/RSA)
59
+
60
+ ## HMAC
61
+
62
+ Defaults to 256 bit digest
63
+
64
+ Gibberish::HMAC("key", "some data")
65
+ #=> 521677c580722c5c52fa15d978e8656341c4f3c5
66
+
67
+ [Find out more](http://rdoc.info/github/mdp/gibberish/master/Gibberish/HMAC)
68
+
69
+ ## Digests
70
+
71
+ Gibberish::MD5("somedata")
72
+ #=> aefaf7502d52994c3b01957636a3cdd2
73
+
74
+ Gibberish::SHA1("somedata")
75
+ #=> efaa311ae448a7374c122061bfed952d940e9e37
76
+
77
+ Gibberish::SHA256("somedata")
78
+ #=> 87d149cb424c0387656f211d2589fb5b1e16229921309e98588419ccca8a7362
79
+
80
+ [Find out more](http://rdoc.info/github/mdp/gibberish/master/Gibberish/Digest)
81
+
82
+ ## TODO
83
+
84
+ - Cover OpenSSL exceptions with more reasonable and easier to understand exceptions.
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+ Rake::TestTask.new do |t|
3
+ t.libs << "spec"
4
+ t.test_files = FileList['spec/*_spec.rb']
5
+ t.verbose = true
6
+ end
7
+ $: << '.'
8
+ require 'spec/spec_helper'
@@ -1,7 +1,36 @@
1
1
  module Gibberish
2
+ # Handles AES encryption and decryption in a way that is compatible
3
+ # with OpenSSL.
4
+ #
5
+ # Defaults to 256-bit CBC encryption, ideally you should leave it
6
+ # this way
7
+ #
8
+ # ## Basic Usage
9
+ #
10
+ # ### Encrypting
11
+ #
12
+ # cipher = Gibberish::AES.new('p4ssw0rd')
13
+ # cipher.encrypt("some secret text")
14
+ # #=> "U2FsdGVkX1/D7z2azGmmQELbMNJV/n9T/9j2iBPy2AM=\n"
15
+ #
16
+ # ### Decrypting
17
+ #
18
+ # cipher = Gibberish::AES.new('p4ssw0rd')
19
+ # cipher.decrypt(""U2FsdGVkX1/D7z2azGmmQELbMNJV/n9T/9j2iBPy2AM=\n"")
20
+ # #=> "some secret text"
21
+ #
22
+ # ## OpenSSL Interop
23
+ #
24
+ # echo "U2FsdGVkX1/D7z2azGmmQELbMNJV/n9T/9j2iBPy2AM=\n" | openssl enc -d -aes-256-cbc -a -k p4ssw0rd
25
+ #
2
26
  class AES
3
27
 
4
28
  attr_reader :password, :size, :cipher
29
+
30
+ # Initialize with the password
31
+ #
32
+ # @param [String] password
33
+ # @param [Integer] size
5
34
  def initialize(password, size=256)
6
35
  @password = password
7
36
  @size = size
@@ -1,4 +1,12 @@
1
1
  module Gibberish
2
+ # Allows for the simple digest of data, supports MD5, SHA1, and SHA256
3
+ #
4
+ # ## Examples
5
+ #
6
+ # Gibberish::MD5("data") #=> 8d777f385d3dfec8815d20f7496026dc
7
+ # Gibberish::SHA1("data") #=> a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd
8
+ # Gibberish::SHA256("data") #=> 3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7
9
+ #
2
10
  class Digest
3
11
 
4
12
  def self.sha1(val, opts={})
@@ -1,4 +1,12 @@
1
1
  module Gibberish
2
+ # Easy to use HMAC, defaults to SHA1
3
+ #
4
+ # ## Example
5
+ #
6
+ # Gibberish::HMAC('key', 'data') #=> 104152c5bfdca07bc633eebd46199f0255c9f49d
7
+ # Gibberish::HMAC('key', 'data', :digest => :sha256)
8
+ # #=> 5031fe3d989c6d1537a013fa6e739da23463fdaec3b70137d828e36ace221bd0
9
+ #
2
10
  class HMAC
3
11
  DIGEST = {
4
12
  :sha1 => OpenSSL::Digest::Digest.new('sha1'),
@@ -1,13 +1,48 @@
1
1
  module Gibberish
2
- class RSA
3
2
 
3
+ # This wraps the OpenSSL RSA functions
4
+ # Simply instantiate with a public key or private key
5
+ #
6
+ # cipher = Gibberish::RSA.new(private_key)
7
+ # enc = cipher.encrypt(data)
8
+ # dec = cipher.decrypt(enc)
9
+ #
10
+ # cipher = Gibberish::RSA(public_key)
11
+ # cipher.decrypt(enc)
12
+ #
13
+ #
14
+ # You can also generate a keypair using Gibberish::RSA.generate_keypair
15
+ #
16
+ # kp = Gibberish::RSA.generate_keypair(4096)
17
+ # kp.public_key #=> Outputs a Base64 encoded public key
18
+ # kp.private_key #=> Outputs the Base64 pem
19
+ #
20
+ # KeyPair will hand back the private key when passed
21
+ # to the RSA class.
22
+ #
23
+ # cipher = Gibberish::RSA.new(kp)
24
+ #
25
+ # ## OpenSSL CLI Interop
26
+ #
27
+ # openssl rsautl -decrypt -inkey [pem_file] -in [BinaryEncodedCryptedFile]
28
+ #
29
+ # or if you're using the default base64 output, you'll need to decode that first
30
+ #
31
+ # openssl enc -d -base64 -in [gibberish.crypted] | \
32
+ # openssl rsautl -decrypt -inkey [pem_file]
33
+ #
34
+
35
+ class RSA
4
36
  class KeyPair
5
37
  def self.generate(bits=2048)
6
38
  self.new(OpenSSL::PKey::RSA.generate(bits))
7
39
  end
8
40
 
41
+ attr_accessor :passphrase
42
+
9
43
  def initialize(key)
10
44
  @key = key
45
+ @cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
11
46
  end
12
47
 
13
48
  def public_key
@@ -15,24 +50,43 @@ module Gibberish
15
50
  end
16
51
 
17
52
  def private_key
18
- @key.to_pem
53
+ if @passphrase
54
+ @key.to_pem(@cipher, @passphrase)
55
+ else
56
+ @key.to_pem
57
+ end
58
+ end
59
+
60
+ def to_s
61
+ private_key
19
62
  end
20
63
 
21
64
  end
22
65
 
66
+ # Generate an RSA keypair - defaults to 2048 bits
67
+ #
68
+ # @param [Integer] bits
23
69
  def RSA.generate_keypair(bits=2048)
24
70
  KeyPair.generate(bits)
25
71
  end
26
72
 
27
73
  # Expects a public key at the minumum
28
74
  #
29
- def initialize(public_key, private_key=nil)
30
- @pub_key = OpenSSL::PKey::RSA.new(public_key)
31
- @priv_key = OpenSSL::PKey::RSA.new(private_key)
75
+ # @param [#to_s] key public or private
76
+ # @params [String] passphrase to key
77
+ #
78
+ def initialize(key, passphrase=nil)
79
+ @key = OpenSSL::PKey::RSA.new(key.to_s, passphrase)
32
80
  end
33
81
 
82
+ # Encrypt data using the key
83
+ #
84
+ # @param [#to_s] data
85
+ # @param [Hash] opts
86
+ # @option opts [Boolean] :binary (false) encode the data in binary, not Base64
34
87
  def encrypt(data, opts={})
35
- enc = @pub_key.public_encrypt(data)
88
+ data = data.to_s
89
+ enc = @key.public_encrypt(data)
36
90
  if opts[:binary]
37
91
  enc
38
92
  else
@@ -40,12 +94,18 @@ module Gibberish
40
94
  end
41
95
  end
42
96
 
97
+ # Decrypt data using the key
98
+ #
99
+ # @param [#to_s] data
100
+ # @param [Hash] opts
101
+ # @option opts [Boolean] :binary (false) don't decode the data as Base64
43
102
  def decrypt(data, opts={})
44
- raise "No private key set!" unless @priv_key
103
+ data = data.to_s
104
+ raise "No private key set!" unless @key.private?
45
105
  unless opts[:binary]
46
106
  data = Base64.decode64(data)
47
107
  end
48
- @priv_key.private_decrypt(data)
108
+ @key.private_decrypt(data)
49
109
  end
50
110
  end
51
111
 
@@ -1,3 +1,3 @@
1
1
  module Gibberish
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -10,14 +10,14 @@ describe "the aes cipher" do
10
10
  secret_text = "Made with Gibberish"
11
11
  encrypted = @cipher.e(secret_text)
12
12
  from_openssl = `echo "#{encrypted}" | openssl enc -d -aes-256-cbc -a -k password`
13
- from_openssl.should eql(secret_text)
13
+ from_openssl.must_equal(secret_text)
14
14
  end
15
15
 
16
16
  it "should decrypt base64 encoded data from the OpenSSL CLI" do
17
17
  secret_text = "Made with Gibberish"
18
18
  from_openssl = `echo #{secret_text} | openssl enc -aes-256-cbc -a -k password`
19
19
  decrypted_text = @cipher.d(from_openssl).chomp
20
- decrypted_text.should eql(secret_text)
20
+ decrypted_text.must_equal(secret_text)
21
21
  end
22
22
 
23
23
  end
@@ -3,15 +3,15 @@ require 'spec_helper'
3
3
  describe "A variety of digest methods" do
4
4
 
5
5
  it "should work with MD5" do
6
- Gibberish::MD5("password").should eql("5f4dcc3b5aa765d61d8327deb882cf99")
6
+ Gibberish::MD5("password").must_equal("5f4dcc3b5aa765d61d8327deb882cf99")
7
7
  end
8
8
 
9
9
  it "should work with SHA1" do
10
- Gibberish::SHA1("password").should eql("5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8")
10
+ Gibberish::SHA1("password").must_equal("5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8")
11
11
  end
12
12
 
13
13
  it "should work with SHA256" do
14
- Gibberish::SHA256("password").should eql("5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8")
14
+ Gibberish::SHA256("password").must_equal("5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8")
15
15
  end
16
16
 
17
17
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe "HMAC" do
4
4
 
5
5
  it "should hopefully work" do
6
- Gibberish::HMAC("password", "data").should eql("08d13c72bed7ace5efadc09df109a78a5d713097")
6
+ Gibberish::HMAC("password", "data").must_equal("08d13c72bed7ace5efadc09df109a78a5d713097")
7
7
  end
8
8
 
9
9
  end
@@ -0,0 +1,3 @@
1
+ U/��T����z��)6����'A���6B�)��Z7>t�q�r��jOo�h��'��G�(��N� ���T ����bJ}s
2
+ s� ��4���(�⛸�W�$d����:���&?O���GxmG�=��{��c<������az�$��]�Kw�������}:&m�~ �'��T����A�(�����KT%���2�]M�� ���t� O��}4)���\ �'��޾
3
+ HNW����կ�q_���x�J������d�
@@ -0,0 +1 @@
1
+ This is in plaintext!!!
@@ -0,0 +1,30 @@
1
+ -----BEGIN RSA PRIVATE KEY-----
2
+ Proc-Type: 4,ENCRYPTED
3
+ DEK-Info: DES-EDE3-CBC,9DFA13921C765D75
4
+
5
+ as7z49axWP7a0p44YlnLwiY9di9BOjSJo98z/Ld0V02p1BHPBElbwIrxcPbAix+I
6
+ 0sl43NtkVlHkiLJR9ZKVWqF8Uly9PCicqKRliPHBw1hiVQuejrmv+6Zx3Aktdunm
7
+ mdXnwa8TVgpSHZcSUynvwp6nfJOtPAmAQLKRFyrOrNRDfLBqZ0Ha5l1+9MhzZxW6
8
+ j6EFw5gOzvcR35ghBN6nCD/UNArh9mKPJFjv6Pfkcwlqxiy7OqHS7lPwN+7WnrFf
9
+ 7jyLEAAaOGQxWjtfb9/3A0sjlrfn7j0uO9I/A6ogOd7wcIaBwy0qtGQ3bsj8ctZw
10
+ 6cHWvSiExLDLdxARa1wROqLM5da3eD/W7BvA+9hPuAJxz6iDWPNWHj0IrsGWiHtF
11
+ dGDbbSyg81/tPKbR0DNpl1bn0ft9h7oQWiM9uubNFCFdLrjJ+z2hMW52PHu89C1f
12
+ swaG+CgB0754eNoBSNm2z4OaBebA8zbRP+8eqOs/ZDCSK4w2e0OFR2T51eEDDsg6
13
+ Q7WHBg1Hq9tBTb/xwJ1u/CzzbHcIksnpE5fiHr0Xs/HMdTYXylcDwqN5df2He7FH
14
+ PyAK41lH4K9MZVpIsWu/6gWk2Nt7UAkG9O/d/el0qMdtWNdJjxP/yFgWi1/zUBH+
15
+ jqavta/jxbEvqVycL+dU0tZSnukarmziMLg3644WW6AVtUeEScO9DibBYdMDIlgg
16
+ m+2oOW90uzgdM236OurBQ240TzJYKRu60uOfjjbYzGw9MM4rwHXx3vk3plOw/vpe
17
+ 827LBvSiw5WSD5wwR5nOpkDkMiNFvCO1JoSYkPYo/xy3yhps3Ea2PtnsoA5VcJRA
18
+ VM0L8ad/taubVqVjDKyMggD6ynB9D0X+SYxbW/UAEKgHbEt6eSRF/egbRhDkt6zl
19
+ im75EuZpres+5rlAi2X+yOwkHGDh3vDRk0Xmr+Eb5VfY+NQQFJy7sDYXKAEy0lsO
20
+ wEsmtxam5u1FNVsFALKXTP2HPn+2fzH25ovA/ujXdlD9Q7LgtQpe+N1LjrK2SzWE
21
+ hKmZZznVVSpDYsIEk95N/yPuDb57+N+UcesasekFkZu/14qseeIZRXYNEgQQqNsS
22
+ wFJua9A4pcfSefSvMMa43ZFXtc+lRi8oCy0X0nN+Gs/f5LzxKexMfJLoXxORLq1Q
23
+ s2zOQnyToHV52cGmE5stuWU1K2XIr8LLv/5nqDLscGZcJ/SWBwth1Lrpw/eLHxEY
24
+ ltzsJzyShsuXFjAIgroKeGpv37D+ekqjVsTOPGtPJ04Q8kX34qTDQA6BvJ8kRQba
25
+ 4hGNtlJlLFwpAIEd43VFkgsiACj2DuqeNGwvtSGkopq0Y5pfSKniezxuezekAn80
26
+ uIGIT1slYrUm8w6qQOka64W2yE/YaV3lv8HH2eQQ9XmRI4UgFQzSAk1sdOmJadzW
27
+ EF55t1GJN0BLyg8v/gmk+N3Tj83ZcxbY4fbQGaroOS83HhzLPoaT05PBqChSYhBo
28
+ 97YKZPLDvxfbMjM5XyltNNEqZyKvR/9FIGvNWNT7uwEThqUIQ+OWiaRLo7y2duq4
29
+ CvPwa8FkA0geEX5RTA05CuxuHriRrWtyoYhtNK2ZJ5pEuRoocP4jaw==
30
+ -----END RSA PRIVATE KEY-----
@@ -0,0 +1,9 @@
1
+ -----BEGIN PUBLIC KEY-----
2
+ MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApkrxFVDdfsB1OnOg6CjD
3
+ 3FX/uuYf3bmdiVfzBkNJvZruqCALjesAu4uGrbmBGhe/qyKYQHPzY2KwqpfwtXft
4
+ 61QFtDdZVTjPzi5iqZcpUIYD6sMdJPH0E8YBADAMPyvKbWtjsLZADxRhKQN3Dwb0
5
+ DTaq0UFVzq/40YZMFzXH9SIWH/4qJnCsqC24DQM9f8mKAGSC5KCReJIzuXorT0RI
6
+ 9ghrwqr8EnglFWT51vcDjR80EfZusvLlep378AmTgYpf9Qxq7BRBx2wp8jWnxsV+
7
+ xYHuruJbZeqIs4FM2dRLLcMlA8Ky6IrViAR+wfy0CyK/ZGdozvXSOV8lh7gspwBh
8
+ KQIDAQAB
9
+ -----END PUBLIC KEY-----
@@ -3,19 +3,19 @@ require 'spec_helper'
3
3
  describe "RSA key generation" do
4
4
  it "should generate a key" do
5
5
  keypair = Gibberish::RSA.generate_keypair
6
- keypair.should be_instance_of(Gibberish::RSA::KeyPair)
6
+ keypair.must_be_instance_of(Gibberish::RSA::KeyPair)
7
7
  end
8
8
 
9
9
  it "should generate a key with custom bits" do
10
10
  keypair = Gibberish::RSA.generate_keypair(1024)
11
- keypair.should be_instance_of(Gibberish::RSA::KeyPair)
11
+ keypair.must_be_instance_of(Gibberish::RSA::KeyPair)
12
12
  end
13
13
 
14
14
  it "should answer to public and private key methods" do
15
15
  keypair = Gibberish::RSA.generate_keypair(1024)
16
- keypair.should be_instance_of(Gibberish::RSA::KeyPair)
17
- keypair.public_key.should_not be_nil
18
- keypair.private_key.should_not be_nil
16
+ keypair.must_be_instance_of(Gibberish::RSA::KeyPair)
17
+ keypair.public_key.wont_be_nil
18
+ keypair.private_key.wont_be_nil
19
19
  end
20
20
 
21
21
  end
@@ -23,21 +23,56 @@ end
23
23
  describe "RSA" do
24
24
  before do
25
25
  k = Gibberish::RSA.generate_keypair(1024)
26
- @cipher = Gibberish::RSA.new(k.public_key, k.private_key)
27
- @pub_cipher = Gibberish::RSA.new(k.public_key, k.private_key)
26
+ @cipher = Gibberish::RSA.new(k.private_key)
27
+ @pub_cipher = Gibberish::RSA.new(k.public_key)
28
28
  end
29
29
 
30
30
  it "should encrypt/decrypt with a keypair" do
31
31
  encrypted = @cipher.encrypt("Some data")
32
- p encrypted
33
32
  decrypted = @cipher.decrypt(encrypted)
34
- encrypted.should match(/^[a-zA-Z0-9\+\\\n=]+$/) # Be base64
35
- decrypted.should eql("Some data")
33
+ encrypted.must_match(/^[a-zA-Z0-9\+\/\n=]+$/) # Be base64
34
+ decrypted.must_equal("Some data")
36
35
  end
37
36
 
38
37
  it "should work without private key" do
39
38
  enc = @pub_cipher.encrypt("Some data")
40
- enc.should_not be_nil
39
+ enc.must_match(/^[a-zA-Z0-9\+\/\n=]+$/) # Be base64
40
+ end
41
+
42
+ end
43
+
44
+ describe "OpenSSL interop" do
45
+
46
+ before do
47
+ @ossl_private_key = File.read('spec/openssl/private.pem')
48
+ @ossl_public_key = File.read('spec/openssl/public.pem')
49
+ @keypair = Gibberish::RSA.generate_keypair(1024)
50
+ @keypair.passphrase = "p4ssw0rd"
51
+ tmp_file = "/tmp/gibberish-spec"
52
+ @pub_key_file = "#{tmp_file}-pub.pem"
53
+ @priv_key_file = "#{tmp_file}-priv.pem"
54
+ File.open(@pub_key_file, 'w') {|f| f.write(@keypair.public_key) }
55
+ File.open(@priv_key_file, 'w') {|f| f.write(@keypair.private_key) }
56
+ end
57
+
58
+ it "should decode and OpenSSL generated key and crypted message" do
59
+ # openssl genrsa -des3 -out spec/openssl/private.pem 2048
60
+ # openssl rsa -in spec/openssl/private.pem -out spec/openssl/public.pem -outform PEM -pubout
61
+ # openssl rsautl -encrypt -inkey public.pem -pubin -in spec/openssl/plaintext.txt -out spec/openssl/plaintext.crypted
62
+ cipher = Gibberish::RSA.new(@ossl_private_key, @keypair.passphrase)
63
+ cipher.decrypt(File.read('spec/openssl/plaintext.crypted'), :binary => true).must_equal(File.read('spec/openssl/plaintext.txt'))
64
+ end
65
+
66
+ if ENV['INTERACTIVE']
67
+ it "should encode an OpenSSL compatible format" do
68
+ # openssl rsautl -decrypt -inkey /tmp/gibberish-spec-priv.pem -in /tmp/gibberish-spec-test.crypted
69
+ cipher = Gibberish::RSA.new(@keypair.public_key)
70
+ tmp_crypt_file = '/tmp/gibberish-spec-test.crypted'
71
+ File.open(tmp_crypt_file, 'w') {|f| f.write(cipher.encrypt("secret text", :binary => true))}
72
+ puts "\n Please type '#{@keypair.passphrase}' when prompted"
73
+ output = `openssl rsautl -decrypt -inkey /tmp/gibberish-spec-priv.pem -in /tmp/gibberish-spec-test.crypted`
74
+ output.must_equal("secret text")
75
+ end
41
76
  end
42
77
 
43
78
  end
@@ -1,8 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
+ gem 'minitest' # ensures you're using the gem, and not the built in MT
4
+ require 'minitest/autorun'
3
5
 
4
6
  require 'gibberish'
5
7
 
6
- RSpec.configure do |config|
7
- # some (optional) config here
8
- end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gibberish
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 2
10
- version: 0.0.2
5
+ version: 1.0.0
11
6
  platform: ruby
12
7
  authors:
13
8
  - Mark Percival
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-03-29 00:00:00 -07:00
13
+ date: 2011-04-02 00:00:00 +00:00
19
14
  default_executable:
20
15
  dependencies: []
21
16
 
@@ -30,9 +25,15 @@ extra_rdoc_files: []
30
25
 
31
26
  files:
32
27
  - .gitignore
28
+ - .gitmodules
29
+ - .yardoc/checksums
30
+ - .yardoc/objects/root.dat
31
+ - .yardoc/proxy_types
32
+ - .yardopts
33
33
  - Gemfile
34
34
  - Gemfile.lock
35
35
  - README.markdown
36
+ - Rakefile
36
37
  - gibberish.gemspec
37
38
  - lib/gibberish.rb
38
39
  - lib/gibberish/aes.rb
@@ -43,6 +44,10 @@ files:
43
44
  - spec/aes_spec.rb
44
45
  - spec/digest_spec.rb
45
46
  - spec/hmac_spec.rb
47
+ - spec/openssl/plaintext.crypted
48
+ - spec/openssl/plaintext.txt
49
+ - spec/openssl/private.pem
50
+ - spec/openssl/public.pem
46
51
  - spec/rsa_spec.rb
47
52
  - spec/spec_helper.rb
48
53
  has_rdoc: true
@@ -59,18 +64,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
64
  requirements:
60
65
  - - ">="
61
66
  - !ruby/object:Gem::Version
62
- hash: 3
63
- segments:
64
- - 0
65
67
  version: "0"
66
68
  required_rubygems_version: !ruby/object:Gem::Requirement
67
69
  none: false
68
70
  requirements:
69
71
  - - ">="
70
72
  - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
73
  version: "0"
75
74
  requirements: []
76
75
 
@@ -79,9 +78,5 @@ rubygems_version: 1.6.2
79
78
  signing_key:
80
79
  specification_version: 3
81
80
  summary: An opinionated ruby encryption library
82
- test_files:
83
- - spec/aes_spec.rb
84
- - spec/digest_spec.rb
85
- - spec/hmac_spec.rb
86
- - spec/rsa_spec.rb
87
- - spec/spec_helper.rb
81
+ test_files: []
82
+