gibberish 1.2.2 → 1.3.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 615d0b35bad8161dd357fca6dbe11049f9efcbb4
4
+ data.tar.gz: d9ab3a19076095e505785df668c4ad78305724e6
5
+ SHA512:
6
+ metadata.gz: 1ad21cb479d7d2252ee98dc1050a1f6d122a438aa9db60c4ed303980af1725f7687a938d22f0ee7aa790a42868a6dad5de000207f75c9a11988d9eff24d62d83
7
+ data.tar.gz: 11b78f9beae9d119ec36d26ad8dec82de4a6cb90bd22e7db7538946813cee1128de79cdb1e4c3787403703cd6fbc577d8978e4200ceb0d4fb1951243ac13d99a
data/CHANGELOG.mdown CHANGED
@@ -1,3 +1,7 @@
1
+ ### v1.3.0
2
+ * Add support for file encryption [PR #12](https://github.com/mdp/gibberish/pull/12)
3
+ * Add support for streaming encryption [PR #11](https://github.com/mdp/gibberish/pull/11)
4
+
1
5
  ### v1.2.2
2
6
  * Added support for EBC mode on AES [PR #10](https://github.com/mdp/gibberish/pull/10)
3
7
 
data/README.markdown CHANGED
@@ -14,7 +14,7 @@ interface in Ruby.
14
14
  line interface. Each function will include documentation on how to perform
15
15
  the same routine via the command line with OpenSSL
16
16
 
17
- - It should default to a reasonably secure setting, e.g. 256-bit AES, or SHA1 for HMAC
17
+ - It should default to a reasonably secure setting, e.g. 256-bit AES, or SHA1 for HMAC
18
18
  But it should allow the user to specify a stronger setting, within reason.
19
19
 
20
20
  - Procedures should be well tested and be compatible with Ruby 1.8.7 and 1.9
@@ -39,11 +39,19 @@ Defaults to 256 bit CBC encryption
39
39
  cipher.dec("U2FsdGVkX187oKRbgDkUcMKaFfB5RsXQj/X4mc8X3lsUVgwb4+S55LQo6f6N\nIDMX")
40
40
  #=> "Some top secret data"
41
41
 
42
+ To encrypt / decrypt a file
43
+
44
+ cipher.encrypt_file("secret.txt", "secret.txt.enc")
45
+
46
+ cipher.decrypt_file("secret.txt.enc", "secret.txt")
47
+
42
48
  Gibberish AES is fully compatible with default OpenSSL on the command line
43
49
 
44
50
  echo "U2FsdGVkX187oKRbgDkUcMKaFfB5RsXQj/X4mc8X3lsUVgwb4+S55LQo6f6N\nIDMX\n" | \
45
51
  openssl enc -d -aes-256-cbc -a -k p4ssw0rd
46
52
 
53
+ openssl aes-256-cbc -d -in secret.txt.enc -out secret.txt -k p4ssw0rd
54
+
47
55
  [Find out more](http://mdp.github.com/gibberish/Gibberish/AES.html)
48
56
 
49
57
  ## RSA
data/lib/gibberish/aes.rb CHANGED
@@ -12,19 +12,24 @@ module Gibberish
12
12
  # cipher = Gibberish::AES.new('p4ssw0rd')
13
13
  # cipher.encrypt("some secret text")
14
14
  # #=> "U2FsdGVkX1/D7z2azGmmQELbMNJV/n9T/9j2iBPy2AM=\n"
15
+ # cipher.encrypt_file("secret.txt", "secret.txt.enc")
15
16
  #
16
17
  # ### Decrypting
17
18
  #
18
19
  # cipher = Gibberish::AES.new('p4ssw0rd')
19
20
  # cipher.decrypt(""U2FsdGVkX1/D7z2azGmmQELbMNJV/n9T/9j2iBPy2AM=\n"")
20
21
  # #=> "some secret text"
22
+ # cipher.decrypt_file("secret.txt.enc", "secret.txt")
21
23
  #
22
24
  # ## OpenSSL Interop
23
25
  #
24
26
  # echo "U2FsdGVkX1/D7z2azGmmQELbMNJV/n9T/9j2iBPy2AM=\n" | openssl enc -d -aes-256-cbc -a -k p4ssw0rd
27
+ # openssl aes-256-cbc -d -in secret.txt.enc -out secret.txt -k p4ssw0rd
25
28
  #
26
29
  class AES
27
30
 
31
+ BUFFER_SIZE = 4096
32
+
28
33
  attr_reader :password, :size, :cipher
29
34
 
30
35
  # Initialize with the password
@@ -59,6 +64,55 @@ module Gibberish
59
64
  alias :dec :decrypt
60
65
  alias :d :decrypt
61
66
 
67
+ def encrypt_file(from_file, to_file, opts={})
68
+ salt = generate_salt(opts[:salt])
69
+ setup_cipher(:encrypt, salt)
70
+ buf = ""
71
+ File.open(to_file, "wb") do |outf|
72
+ outf << "Salted__#{salt}"
73
+ File.open(from_file, "rb") do |inf|
74
+ while inf.read(4096, buf)
75
+ outf << self.cipher.update(buf)
76
+ end
77
+ outf << self.cipher.final
78
+ end
79
+ end
80
+ end
81
+ alias :enc_file :encrypt_file
82
+ alias :ef :encrypt_file
83
+
84
+ def decrypt_file(from_file, to_file)
85
+ buf = ""
86
+ salt = ""
87
+ File.open(to_file, "wb") do |outf|
88
+ File.open(from_file, "rb") do |inf|
89
+ inf.seek(8, IO::SEEK_SET)
90
+ inf.read(8, salt)
91
+ setup_cipher(:decrypt, salt)
92
+ while inf.read(4096, buf)
93
+ outf << self.cipher.update(buf)
94
+ end
95
+ outf << self.cipher.final
96
+ end
97
+ end
98
+ end
99
+ alias :dec_file :decrypt_file
100
+ alias :df :decrypt_file
101
+
102
+ def encrypt_stream(in_stream, out_stream, opts={})
103
+ salt = generate_salt(opts[:salt])
104
+ setup_cipher(:encrypt, salt)
105
+ out_stream << "Salted__#{salt}"
106
+ copy_stream in_stream, out_stream
107
+ end
108
+
109
+ def decrypt_stream(in_stream, out_stream)
110
+ header = in_stream.read(16)
111
+ salt = header[8..15]
112
+ setup_cipher(:decrypt, salt)
113
+ copy_stream in_stream, out_stream
114
+ end
115
+
62
116
  private
63
117
 
64
118
  def generate_salt(supplied_salt)
@@ -74,5 +128,15 @@ module Gibberish
74
128
  cipher.send(method)
75
129
  cipher.pkcs5_keyivgen(password, salt, 1)
76
130
  end
131
+
132
+ def copy_stream(in_stream, out_stream)
133
+ buf = ''
134
+ while in_stream.read(BUFFER_SIZE, buf)
135
+ out_stream << cipher.update(buf)
136
+ end
137
+ out_stream << cipher.final
138
+ out_stream.flush
139
+ end
140
+
77
141
  end
78
142
  end
@@ -1,3 +1,3 @@
1
1
  module Gibberish
2
- VERSION = "1.2.2"
2
+ VERSION = "1.3.0"
3
3
  end
data/spec/aes_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'tempfile'
2
3
 
3
4
  describe "the aes cipher" do
4
5
 
@@ -13,6 +14,15 @@ describe "the aes cipher" do
13
14
  from_openssl.must_equal(secret_text)
14
15
  end
15
16
 
17
+ it "should encrypt file and be compatible with OpenSSL CLI" do
18
+ source_file_path = "spec/fixtures/secret.txt"
19
+ encrypted_file = Tempfile.new('secret.txt.enc')
20
+ @cipher.ef(source_file_path, encrypted_file.path)
21
+ decrypted_file = Tempfile.new('secret.txt')
22
+ `openssl aes-256-cbc -d -in #{encrypted_file.path} -out #{decrypted_file.path} -k password`
23
+ FileUtils.cmp(source_file_path, decrypted_file.path).must_equal(true)
24
+ end
25
+
16
26
  it "when salt is not specified, encrypted text from repeated calls should not be the same" do
17
27
  secret_text = "Made with Gibberish"
18
28
  encrypted1 = @cipher.e(secret_text)
@@ -59,8 +69,39 @@ describe "the aes cipher" do
59
69
  it "should decrypt base64 encoded data from the OpenSSL CLI" do
60
70
  secret_text = "Made with Gibberish"
61
71
  from_openssl = `echo #{secret_text} | openssl enc -aes-256-cbc -a -k password`
62
- decrypted_text = @cipher.d(from_openssl).chomp
72
+ decrypted_text = @cipher.d(from_openssl).chomp
63
73
  decrypted_text.must_equal(secret_text)
64
74
  end
65
75
 
76
+ it "should decrypt file encrypted with OpenSSL CLI" do
77
+ source_file_path = "spec/fixtures/secret.txt"
78
+ encrypted_file = Tempfile.new('secret.txt.enc')
79
+ `openssl aes-256-cbc -salt -in #{source_file_path} -out #{encrypted_file.path} -k password`
80
+ decrypted_file = Tempfile.new('secret.txt')
81
+ @cipher.df(encrypted_file.path, decrypted_file.path)
82
+ FileUtils.cmp(source_file_path, decrypted_file.path).must_equal(true)
83
+ end
84
+
85
+ describe 'stream encryption' do
86
+
87
+ it 'encrypts a file' do
88
+ File.open('spec/openssl/plaintext.txt', 'rb') do |in_file|
89
+ File.open(Tempfile.new('gib'), 'wb') do |enc_file|
90
+ @cipher.encrypt_stream in_file, enc_file, salt: 'SOMESALT'
91
+ File.read(enc_file.path).must_equal(File.read('spec/openssl/plaintext.aes'))
92
+ end
93
+ end
94
+ end
95
+
96
+ it 'decrypts a file' do
97
+ File.open('spec/openssl/plaintext.aes', 'rb') do |in_file|
98
+ File.open(Tempfile.new('gib'), 'wb') do |dec_file|
99
+ @cipher.decrypt_stream in_file, dec_file
100
+ File.read(dec_file.path).must_equal(File.read('spec/openssl/plaintext.txt'))
101
+ end
102
+ end
103
+ end
104
+
105
+ end
106
+
66
107
  end
@@ -0,0 +1,41 @@
1
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
2
+ tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
3
+ quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
4
+ consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
5
+ cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
6
+ proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
7
+
8
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
9
+ tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
10
+ quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
11
+ consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
12
+ cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
13
+ proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
14
+
15
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
16
+ tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
17
+ quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
18
+ consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
19
+ cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
20
+ proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
21
+
22
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
23
+ tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
24
+ quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
25
+ consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
26
+ cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
27
+ proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
28
+
29
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
30
+ tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
31
+ quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
32
+ consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
33
+ cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
34
+ proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
35
+
36
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
37
+ tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
38
+ quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
39
+ consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
40
+ cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
41
+ proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
@@ -0,0 +1 @@
1
+ Salted__SOMESALT!���,�CӇ����uz���_�_�i/
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gibberish
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
5
- prerelease:
4
+ version: 1.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Mark Percival
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-03-13 00:00:00.000000000 Z
11
+ date: 2013-08-24 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Supports OpenSSL compatible AES, HMAC, and RSA encryption
15
14
  email:
@@ -39,7 +38,9 @@ files:
39
38
  - lib/gibberish/version.rb
40
39
  - spec/aes_spec.rb
41
40
  - spec/digest_spec.rb
41
+ - spec/fixtures/secret.txt
42
42
  - spec/hmac_spec.rb
43
+ - spec/openssl/plaintext.aes
43
44
  - spec/openssl/plaintext.crypted
44
45
  - spec/openssl/plaintext.txt
45
46
  - spec/openssl/private.pem
@@ -48,26 +49,25 @@ files:
48
49
  - spec/spec_helper.rb
49
50
  homepage: http://github.com/mdp/gibberish
50
51
  licenses: []
52
+ metadata: {}
51
53
  post_install_message:
52
54
  rdoc_options: []
53
55
  require_paths:
54
56
  - lib
55
57
  required_ruby_version: !ruby/object:Gem::Requirement
56
- none: false
57
58
  requirements:
58
- - - ! '>='
59
+ - - '>='
59
60
  - !ruby/object:Gem::Version
60
61
  version: '0'
61
62
  required_rubygems_version: !ruby/object:Gem::Requirement
62
- none: false
63
63
  requirements:
64
- - - ! '>='
64
+ - - '>='
65
65
  - !ruby/object:Gem::Version
66
66
  version: '0'
67
67
  requirements: []
68
68
  rubyforge_project: gibberish
69
- rubygems_version: 1.8.23
69
+ rubygems_version: 2.0.2
70
70
  signing_key:
71
- specification_version: 3
71
+ specification_version: 4
72
72
  summary: An opinionated ruby encryption library
73
73
  test_files: []