crypto_laser 0.0.3 → 0.0.4

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.
@@ -4,17 +4,14 @@
4
4
  <content url="file://$MODULE_DIR$" />
5
5
  <orderEntry type="inheritedJdk" />
6
6
  <orderEntry type="sourceFolder" forTests="false" />
7
- <orderEntry type="library" scope="PROVIDED" name="aead (v1.6.1, RVM: ruby-1.9.3-p194 [crypto_laser]) [gem]" level="application" />
8
7
  <orderEntry type="library" scope="PROVIDED" name="bundler (v1.2.1, RVM: ruby-1.9.3-p194 [crypto_laser]) [gem]" level="application" />
9
8
  <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.1.3, RVM: ruby-1.9.3-p194 [crypto_laser]) [gem]" level="application" />
10
- <orderEntry type="library" scope="PROVIDED" name="macaddr (v1.6.1, RVM: ruby-1.9.3-p194 [crypto_laser]) [gem]" level="application" />
11
9
  <orderEntry type="library" scope="PROVIDED" name="pivotal_git_scripts (v1.1.4, RVM: ruby-1.9.3-p194 [crypto_laser]) [gem]" level="application" />
12
10
  <orderEntry type="library" scope="PROVIDED" name="rake (v10.0.3, RVM: ruby-1.9.3-p194 [crypto_laser]) [gem]" level="application" />
13
11
  <orderEntry type="library" scope="PROVIDED" name="rspec (v2.12.0, RVM: ruby-1.9.3-p194 [crypto_laser]) [gem]" level="application" />
14
12
  <orderEntry type="library" scope="PROVIDED" name="rspec-core (v2.12.1, RVM: ruby-1.9.3-p194 [crypto_laser]) [gem]" level="application" />
15
13
  <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v2.12.0, RVM: ruby-1.9.3-p194 [crypto_laser]) [gem]" level="application" />
16
14
  <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v2.12.0, RVM: ruby-1.9.3-p194 [crypto_laser]) [gem]" level="application" />
17
- <orderEntry type="library" scope="PROVIDED" name="systemu (v2.5.2, RVM: ruby-1.9.3-p194 [crypto_laser]) [gem]" level="application" />
18
15
  </component>
19
16
  </module>
20
17
 
data/Gemfile.lock CHANGED
@@ -1,17 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- crypto_laser (0.0.3)
5
- aead
4
+ crypto_laser (0.0.4)
6
5
 
7
6
  GEM
8
7
  remote: http://rubygems.org/
9
8
  specs:
10
- aead (1.6.1)
11
- macaddr (~> 1)
12
9
  diff-lcs (1.1.3)
13
- macaddr (1.6.1)
14
- systemu (~> 2.5.0)
15
10
  pivotal_git_scripts (1.1.4)
16
11
  rake (10.0.2)
17
12
  rspec (2.12.0)
@@ -22,7 +17,6 @@ GEM
22
17
  rspec-expectations (2.12.0)
23
18
  diff-lcs (~> 1.1.3)
24
19
  rspec-mocks (2.12.0)
25
- systemu (2.5.2)
26
20
 
27
21
  PLATFORMS
28
22
  ruby
data/crypto_laser.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "crypto_laser"
6
- s.version = "0.0.3"
6
+ s.version = "0.0.4"
7
7
  s.authors = %w(Goodsearch)
8
8
  s.email = %w(dev@goodsearch.com)
9
9
  s.homepage = "http://www.goodsearch.com"
@@ -14,8 +14,6 @@ Gem::Specification.new do |s|
14
14
  s.require_paths = %w(lib)
15
15
  s.required_ruby_version = '>= 1.8.7'
16
16
 
17
- s.add_dependency 'aead'
18
-
19
17
  s.add_development_dependency "rspec"
20
18
  s.add_development_dependency "rake"
21
19
 
data/lib/crypto_laser.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'aead'
1
+ require 'openssl'
2
2
  require 'base64'
3
3
 
4
4
  # Simple library for authenticated encryption. Most of the work
@@ -17,36 +17,51 @@ require 'base64'
17
17
  class CryptoLaser
18
18
 
19
19
  def self.encrypt(key, plain_text)
20
+ enc_key = key[0...32]
21
+ mac_key = key[32...64]
22
+
20
23
  code = CryptoLaser.default_algorithm_code
21
- mode = AEAD::Cipher.new(CryptoLaser.algorithms[code])
22
- cipher = mode.new(key)
23
- nonce = CryptoLaser.generate_nonce
24
- cipher_text = cipher.encrypt(nonce, code, plain_text)
25
- Base64.strict_encode64(code + nonce + cipher_text).chomp
24
+
25
+ cipher = OpenSSL::Cipher.new(CryptoLaser.algorithms[code])
26
+ cipher.encrypt
27
+ cipher.key = enc_key
28
+ nonce = cipher.random_iv
29
+ cipher.iv = nonce
30
+ cipher_text = cipher.update(plain_text) + cipher.final
31
+
32
+ text_to_mac = code + nonce + cipher_text
33
+ mac = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), mac_key, text_to_mac)
34
+
35
+ Base64.strict_encode64(text_to_mac + mac).chomp
26
36
  end
27
37
 
28
38
  def self.decrypt(key, base64_cipher_text)
39
+ enc_key = key[0...32]
40
+ mac_key = key[32...64]
29
41
  cipher_text = Base64.decode64(base64_cipher_text)
42
+
30
43
  code = cipher_text[0,2]
31
44
  algorithm = CryptoLaser.algorithms[code]
32
45
  raise "Invalid algorithm code." unless algorithm
33
- mode = AEAD::Cipher.new(algorithm)
34
- cipher = mode.new(key)
35
- nonce = cipher_text[2...18] # TODO: Base on code
36
- ctext = cipher_text[18..-1]
37
- cipher.decrypt(nonce, code, ctext)
46
+
47
+ text_to_mac = cipher_text[0...-32]
48
+ mac = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), mac_key, text_to_mac)
49
+ raise "MAC check failed" unless mac == cipher_text[-32..-1]
50
+
51
+ decipher = OpenSSL::Cipher.new(CryptoLaser.algorithms[code])
52
+ decipher.decrypt
53
+ decipher.key = enc_key
54
+ decipher.iv = cipher_text[2...18]
55
+
56
+ decipher.update(cipher_text[18...-32]) + decipher.final
38
57
  end
39
58
 
40
59
  def self.algorithms
41
- { "V1" => 'AES-256-CBC-HMAC-SHA-256' }
60
+ { "V1" => 'AES-256-CBC' }
42
61
  end
43
62
 
44
63
  def self.default_algorithm_code
45
64
  "V1"
46
65
  end
47
66
 
48
- def self.generate_nonce
49
- SecureRandom.random_bytes(16)
50
- end
51
-
52
67
  end
@@ -1,24 +1,23 @@
1
1
  require 'spec_helper'
2
-
3
2
  require 'crypto_laser'
4
3
 
5
4
  describe CryptoLaser do
6
5
 
7
6
  let(:key) { "\"/\\xE0x5\\x9A\\xE9\\x82\\xB8p \\xED^\\xFFX\\xF6\\xB3}\\xB9bR\\xCF\\xDAdH\\xE4\\x9D\\xB5\\xC2r\\x98\\xD3\\xFC\"" }
8
7
  let(:nonce) { "\x97\x88\xF3\x0Ei\x84\x99\xC7 OZ2\xCA\v\x873" }
9
- let(:cipher_text) { "VjGXiPMOaYSZxyBPWjLKC4czENVqZt2Eyj9+h+58kte4cpck4HhanqaFCaSEZL0K2E1WVVn1gPl+at0XAiYj3jd0" }
8
+ let(:cipher_text) { "VjGXiPMOaYSZxyBPWjLKC4czENVqZt2Eyj9+h+58kte4co0qArWC6f0+tYBGaC5rdU+R5fcmzLlFtD9j4eJiJWZN" }
10
9
  let(:plain_text) { "ZOMG PONIES" }
11
10
 
12
11
  describe "#encrypt" do
13
12
 
14
- before { described_class.stub(:generate_nonce).and_return nonce }
13
+ before { OpenSSL::Cipher.any_instance.stub(:random_iv).and_return nonce }
15
14
 
16
15
  subject { described_class.encrypt(key, plain_text) }
17
16
 
18
- it "encrypts plaintext" do
19
- puts "#{__FILE__}:#{__LINE__} #{key.bytesize}"
17
+ it "encrypts plain text" do
20
18
  subject.should == cipher_text
21
19
  end
20
+
22
21
  end
23
22
 
24
23
  describe "#decrypt" do
@@ -47,13 +46,4 @@ describe CryptoLaser do
47
46
 
48
47
  end
49
48
 
50
- describe ".generate_nonce" do
51
-
52
- subject { described_class.generate_nonce }
53
-
54
- it "should return 16 bytes" do
55
- subject.bytesize.should == 16
56
- end
57
- end
58
-
59
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crypto_laser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-19 00:00:00.000000000 Z
12
+ date: 2012-12-20 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: aead
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
14
  - !ruby/object:Gem::Dependency
31
15
  name: rspec
32
16
  requirement: !ruby/object:Gem::Requirement