cryptor 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28c23ac85c838a1017c2298d7237cba2f4a4cce2
4
- data.tar.gz: 7176ba9550de9958c17cfebfdec784f66f236b5c
3
+ metadata.gz: 7e231e81835f4ec4b64c603b597b38266d505cd8
4
+ data.tar.gz: 0843bf2d906c77f5824d75c187d30d2d28bf246c
5
5
  SHA512:
6
- metadata.gz: 82907137a69c2f33963ea311ec7e9c8a632d4f91b4313b354bb69ec2b654c53c80906f9b57e8afcbe96b5f8fc6b17fab4baab48f12331d0e8db9437226b46485
7
- data.tar.gz: 85e14622245d730c9ec036644a5f5c32f9f7d6bfcbb19c4e8060dab4bc3eab4864af79d56dcbdc0913057facb8df4dfc61677cdab6092928149f08268c3cd01f
6
+ metadata.gz: 46ecd69eebc0ab266b699f978bc934f03fd7211414eb55a8147e9cdc14a1a999424fd266c66d2db211b4ce9a0734cb6799d7e45beba2927167cd40579d835c36
7
+ data.tar.gz: 3da93938f00f68a5e7b3d968e7628a09d788b3838142a48945ae14437150c5849bae15da751002ba205b70c5c90bf399ca6f80793315ce6e4a90a4f02ed48e21
data/CHANGES.md CHANGED
@@ -1,3 +1,7 @@
1
+ 0.0.2 (2014-06-07)
2
+ ------------------
3
+ * Raise Cryptor::CorruptedMessageError on MAC verification failure
4
+
1
5
  0.0.1 (2014-06-05)
2
6
  ------------------
3
7
  * Happy Snow day!
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.test_files = spec.files.grep(/^(test|spec|features)\//)
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.add_runtime_dependency 'ordo', '>= 0.0.1'
22
+ spec.add_runtime_dependency 'ordo', '>= 0.0.2'
23
23
 
24
24
  spec.add_development_dependency 'rake'
25
25
  spec.add_development_dependency 'rubocop'
@@ -4,3 +4,11 @@ require 'cryptor/cipher'
4
4
  require 'cryptor/encoding'
5
5
  require 'cryptor/secret_key'
6
6
  require 'cryptor/symmetric_encryption'
7
+
8
+ # Multi-backend high-level encryption library
9
+ module Cryptor
10
+ CryptoError = Class.new(StandardError)
11
+
12
+ InvalidMessageError = Class.new(CryptoError)
13
+ CorruptedMessageError = Class.new(CryptoError)
14
+ end
@@ -32,6 +32,8 @@ module Cryptor
32
32
 
33
33
  def decrypt(key, ciphertext)
34
34
  encryptor(key).decrypt_and_verify(ciphertext)
35
+ rescue ActiveSupport::MessageVerifier::InvalidSignature => ex
36
+ raise CorruptedMessageError, ex.to_s
35
37
  end
36
38
 
37
39
  private
@@ -14,6 +14,8 @@ module Cryptor
14
14
 
15
15
  def decrypt(key, ciphertext)
16
16
  box(key).decrypt(ciphertext)
17
+ rescue RbNaCl::CryptoError => ex
18
+ raise CorruptedMessageError, ex.to_s
17
19
  end
18
20
 
19
21
  private
@@ -28,24 +28,16 @@ module Cryptor
28
28
  end
29
29
 
30
30
  def decrypt(ciphertext)
31
- message = ORDO::Message.parse(ciphertext)
32
- fingerprint = message['Key-Fingerprint']
31
+ begin
32
+ message = ORDO::Message.parse(ciphertext)
33
+ rescue ORDO::ParseError => ex
34
+ raise InvalidMessageError, ex.to_s
35
+ end
33
36
 
37
+ fingerprint = message['Key-Fingerprint']
34
38
  fail ArgumentError, "no key configured for: #{fingerprint}" if @key.fingerprint != fingerprint
35
39
 
36
- @key.decrypt decode(message)
37
- end
38
-
39
- private
40
-
41
- def decode(message)
42
- encoding = message['Content-Transfer-Encoding']
43
-
44
- case encoding
45
- when 'base64' then Base64.strict_decode64(message.body)
46
- when 'binary' then message.body
47
- else fail ArgumentError, "invalid message encoding: #{encoding}"
48
- end
40
+ @key.decrypt message.body
49
41
  end
50
42
  end
51
43
  end
@@ -1,4 +1,4 @@
1
1
  # An easy-to-use library for real-world Ruby cryptography
2
2
  module Cryptor
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
@@ -2,6 +2,14 @@ require 'spec_helper'
2
2
 
3
3
  describe Cryptor::SymmetricEncryption do
4
4
  let(:plaintext) { 'THE MAGIC WORDS ARE SQUEAMISH OSSIFRAGE' }
5
+
6
+ let(:garbage) do
7
+ 'Timely and accurate information about the activities, capabilities, ' \
8
+ 'plans, and intentions of foreign powers, organizations, and persons ' \
9
+ 'and their agents, is essential to the national security of the ' \
10
+ 'United States.'
11
+ end
12
+
5
13
  subject { described_class.new(secret_key) }
6
14
 
7
15
  context 'xsalsa20poly1305' do
@@ -13,6 +21,20 @@ describe Cryptor::SymmetricEncryption do
13
21
  ciphertext = subject.encrypt(plaintext)
14
22
  expect(subject.decrypt(ciphertext)).to eq plaintext
15
23
  end
24
+
25
+ it 'raises InvalidMessageError if asked to decrypt garbage' do
26
+ expect { subject.decrypt(garbage) }.to raise_exception(Cryptor::InvalidMessageError)
27
+ end
28
+
29
+ it 'raises CorruptedMessageError if the message is corrupt' do
30
+ valid_message = subject.encrypt(plaintext)
31
+ munged_message = ORDO::Message.parse(valid_message)
32
+ munged_message.body.replace Base64.strict_encode64(munged_message.body + "\0")
33
+
34
+ expect do
35
+ subject.decrypt(munged_message.to_string)
36
+ end.to raise_exception(Cryptor::CorruptedMessageError)
37
+ end
16
38
  end
17
39
 
18
40
  context 'message_encryptor' do
@@ -24,5 +46,19 @@ describe Cryptor::SymmetricEncryption do
24
46
  ciphertext = subject.encrypt(plaintext)
25
47
  expect(subject.decrypt(ciphertext)).to eq plaintext
26
48
  end
49
+
50
+ it 'raises InvalidMessageError if asked to decrypt garbage' do
51
+ expect { subject.decrypt(garbage) }.to raise_exception(Cryptor::InvalidMessageError)
52
+ end
53
+
54
+ it 'raises CorruptedMessageError if the message is corrupt' do
55
+ valid_message = subject.encrypt(plaintext)
56
+ munged_message = ORDO::Message.parse(valid_message)
57
+ munged_message.body.replace Base64.strict_encode64(munged_message.body + "\0")
58
+
59
+ expect do
60
+ subject.decrypt(munged_message.to_string)
61
+ end.to raise_exception(Cryptor::CorruptedMessageError)
62
+ end
27
63
  end
28
64
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cryptor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Arcieri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-06 00:00:00.000000000 Z
11
+ date: 2014-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ordo
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.0.1
19
+ version: 0.0.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.0.1
26
+ version: 0.0.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement