encrypto 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ # See http://help.github.com/ignore-files/ for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile ~/.gitignore_global
6
+
7
+ # Ignore bundler config
8
+ /.bundle
9
+ /.jbundler
10
+
11
+ # Ignore the default SQLite database.
12
+ /db/*.sqlite3
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp
17
+
18
+ rspec.html
19
+ cucumber.html
20
+ **/.DS_Store
21
+
22
+ /coverage/
23
+ .vagrant
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ encrypto (0.0.1)
5
+ attr_encrypted (~> 1.3.0)
6
+ rbnacl (~> 1.1.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ attr_encrypted (1.3.0)
12
+ encryptor (>= 1.3.0)
13
+ coderay (1.1.0)
14
+ diff-lcs (1.2.5)
15
+ docile (1.1.1)
16
+ encryptor (1.3.0)
17
+ ffi (1.9.3)
18
+ method_source (0.8.2)
19
+ multi_json (1.8.2)
20
+ pry (0.9.12.4)
21
+ coderay (~> 1.0)
22
+ method_source (~> 0.8)
23
+ slop (~> 3.4)
24
+ pry-nav (0.2.3)
25
+ pry (~> 0.9.10)
26
+ rbnacl (1.1.0)
27
+ ffi
28
+ rspec (2.14.1)
29
+ rspec-core (~> 2.14.0)
30
+ rspec-expectations (~> 2.14.0)
31
+ rspec-mocks (~> 2.14.0)
32
+ rspec-core (2.14.7)
33
+ rspec-expectations (2.14.4)
34
+ diff-lcs (>= 1.1.3, < 2.0)
35
+ rspec-mocks (2.14.4)
36
+ simplecov (0.8.2)
37
+ docile (~> 1.1.0)
38
+ multi_json
39
+ simplecov-html (~> 0.8.0)
40
+ simplecov-html (0.8.0)
41
+ slop (3.4.7)
42
+ spec_coverage (0.0.5)
43
+ rspec (~> 2.0)
44
+ simplecov
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ encrypto!
51
+ pry-nav (~> 0.2.3)
52
+ rspec (~> 2.14.1)
53
+ spec_coverage (~> 0.0.5)
@@ -0,0 +1,7 @@
1
+ Encrypto
2
+ ========
3
+
4
+ A gem that supports encrypting personal data by using rbnacl and attr_encrypted
5
+
6
+
7
+ This gem was made by @Ruben-Hartog and @arnetim, inspired by @ariekanarie and @rvdijk
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/encrypto/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'encrypto'
6
+ gem.version = Encrypto::VERSION
7
+ gem.date = '2013-12-04'
8
+ gem.summary = "A gem that supports encrypting personal data by using rbnacl and attr_encrypted"
9
+ gem.description = "A gem that supports encrypting personal data by using rbnacl and attr_encrypted"
10
+ gem.authors = ["Ruben", "Arne"]
11
+ gem.email = 'service@finalist.nl'
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.require_paths = ["lib"]
15
+ gem.homepage = 'http://github.com/finalist/encrypto'
16
+
17
+ gem.add_dependency "rbnacl", "~> 1.1.0"
18
+ gem.add_dependency "attr_encrypted", "~> 1.3.0"
19
+
20
+ gem.add_development_dependency "pry-nav", "~> 0.2.3"
21
+ gem.add_development_dependency "rspec", "~> 2.14.1"
22
+ gem.add_development_dependency "spec_coverage", "~> 0.0.5"
23
+ end
@@ -0,0 +1,5 @@
1
+ require "rbnacl"
2
+ require "attr_encrypted"
3
+ require "encrypto/version"
4
+
5
+ Dir[File.dirname(__FILE__) + '/encrypto/**/*.rb'].each {|file| require file }
@@ -0,0 +1,30 @@
1
+ module Encrypto
2
+
3
+ class Box
4
+ def initialize(nacl_box)
5
+ @nacl_box = nacl_box
6
+ end
7
+
8
+ def box(value)
9
+ @nacl_box.box(value, :hex)
10
+ end
11
+
12
+ def open(cipher_text)
13
+ @nacl_box.open(cipher_text, :hex)
14
+ end
15
+
16
+ def self.from_passphrase(passphrase)
17
+ passphrase_sha = Crypto::Hash.sha256(passphrase)
18
+ from_secret_key(passphrase_sha)
19
+ end
20
+
21
+ def self.from_secret_key(secret_key)
22
+ new(Crypto::RandomNonceBox.from_secret_key(secret_key))
23
+ end
24
+
25
+ def self.from_keypair(public_key, private_key)
26
+ new(Crypto::RandomNonceBox.from_keypair(public_key, private_key))
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ module Encrypto
2
+ module Database
3
+ module AttrEncrypted
4
+
5
+ def self.included(model)
6
+ model.class_eval do
7
+ attr_encrypted_options.merge!(
8
+ :encryptor => ::Encrypto::Database::Encryptor,
9
+ :key => :encryption_key,
10
+ :encode => false
11
+ )
12
+ end
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Encrypto
2
+ module Database
3
+
4
+ class Encryptor
5
+ def self.encrypt(options)
6
+ box = Box.from_secret_key(options[:key])
7
+ box.box(options[:value])
8
+ end
9
+
10
+ def self.decrypt(options)
11
+ box = Box.from_secret_key(options[:key])
12
+ box.open(options[:value])
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,40 @@
1
+ module Encrypto
2
+
3
+ def self.generate_keypair
4
+ Keys.generate_keypair
5
+ end
6
+
7
+ def self.generate_random_key
8
+ Random.bytes
9
+ end
10
+
11
+ def self.encrypt_with_passphrase(value, passphrase)
12
+ passphrase_box(passphrase).box(value)
13
+ end
14
+
15
+ def self.decrypt_with_passphrase(cipher_text, passphrase)
16
+ passphrase_box(passphrase).open(cipher_text)
17
+ end
18
+
19
+ def self.encrypt_with_keypair(value, public_key, signing_private_key)
20
+ hex_public_key = Keys.hex_public_key(public_key)
21
+ keypair_box(hex_public_key, signing_private_key).box(value)
22
+ end
23
+
24
+ def self.decrypt_with_keypair(cipher_text, hex_public_key, private_key)
25
+ public_key = Keys.hex_public_key(hex_public_key)
26
+ box = Box.from_keypair(public_key, private_key)
27
+ box.open(cipher_text)
28
+ end
29
+
30
+ private
31
+
32
+ def self.keypair_box(public_key, private_key)
33
+ Box.from_keypair(public_key, private_key)
34
+ end
35
+
36
+ def self.passphrase_box(passphrase)
37
+ Box.from_passphrase(passphrase)
38
+ end
39
+
40
+ end
@@ -0,0 +1,14 @@
1
+ module Encrypto
2
+ module Keys
3
+
4
+ def self.generate_keypair
5
+ private_key = Crypto::PrivateKey.generate
6
+ [private_key.public_key.to_s(:hex), private_key]
7
+ end
8
+
9
+ def self.hex_public_key(value)
10
+ Crypto::PublicKey.new(value, :hex)
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ module Encrypto
2
+ module Random
3
+
4
+ def self.bytes
5
+ Crypto::Random.random_bytes
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Encrypto
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ echo "--- Making sure bundler is installed"
5
+
6
+ gem which bundler &> /dev/null || gem install bundler --no-ri --no-rdoc
7
+
8
+ echo "--- Installing gems"
9
+
10
+ bundle check --no-color || time bundle install --no-color
@@ -0,0 +1,22 @@
1
+ run() {
2
+ time bundle exec $*
3
+ }
4
+
5
+ echo "--- Starting continuous integration build"
6
+
7
+ ./script/bundler
8
+
9
+ if [[ -d coverage ]]; then
10
+ echo "Removing old coverage report"
11
+ rm -r coverage
12
+ fi
13
+
14
+ echo "--- Running RSpec"
15
+
16
+ run rspec --color spec --format SpecCoverage --format progress --format html --out rspec.html
17
+ rspec=$?
18
+
19
+ if [[ $rspec -ne 0 ]]; then
20
+ echo "--- Some tests have failed."
21
+ exit 1
22
+ fi
@@ -0,0 +1,10 @@
1
+ #!/bin/sh
2
+ if [ ! -f /usr/local/lib/libsodium.a ];
3
+ then
4
+ curl -o /tmp/sodium.tar.gz https://download.libsodium.org/libsodium/releases/old/libsodium-0.2.tar.gz
5
+ cd /tmp
6
+ tar vfzx sodium.tar.gz
7
+ cd libsodium-0.2
8
+ ./configure
9
+ make && make check && make install
10
+ fi
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ module Encrypto
4
+ describe Box do
5
+
6
+ describe '.from_passphrase' do
7
+ let(:passphrase) { 'password' }
8
+
9
+ it 'hashes the secret key' do
10
+ Crypto::Hash.should_receive(:sha256).with(passphrase)
11
+ Crypto::RandomNonceBox.stub(:from_secret_key)
12
+ Encrypto::Box.from_passphrase(passphrase)
13
+ end
14
+
15
+ it 'creates a random nonce box based on the hashed secret key' do
16
+ Crypto::Hash.stub(:sha256 => "sha")
17
+ Crypto::RandomNonceBox.should_receive(:from_secret_key).with("sha")
18
+ Encrypto::Box.from_passphrase(passphrase)
19
+ end
20
+
21
+ it 'initializes with a random nonce box' do
22
+ box = double("box")
23
+ Crypto::Hash.stub(:sha256 => "sha")
24
+ Crypto::RandomNonceBox.stub(:from_secret_key => box)
25
+ Encrypto::Box.should_receive(:new).with(box)
26
+ Encrypto::Box.from_passphrase(passphrase)
27
+ end
28
+ end
29
+
30
+ describe ".from_keypair" do
31
+ let(:public_key) { double("public key") }
32
+ let(:private_key) { double("private key") }
33
+
34
+ it "creates a random nonce box based on the keypair" do
35
+ Crypto::RandomNonceBox.should_receive(:from_keypair).with(public_key, private_key)
36
+ Encrypto::Box.from_keypair(public_key, private_key)
37
+ end
38
+
39
+ it "initializes with a random nonce box" do
40
+ box = double("box")
41
+ Crypto::RandomNonceBox.stub(:from_keypair => box)
42
+ Encrypto::Box.should_receive(:new).with(box)
43
+ Encrypto::Box.from_keypair(public_key, private_key)
44
+ end
45
+ end
46
+
47
+ describe '#box' do
48
+ it 'boxes the value' do
49
+ value = double("value")
50
+
51
+ some_box = double("box")
52
+ some_box.should_receive(:box).with(value, :hex)
53
+
54
+ box = Encrypto::Box.new(some_box)
55
+ box.box(value)
56
+ end
57
+ end
58
+
59
+ describe "#open" do
60
+ it "opens the cipher text" do
61
+ cipher_text = double("cipher text")
62
+
63
+ some_box = double("box")
64
+ some_box.should_receive(:open).with(cipher_text, :hex)
65
+
66
+ box = Encrypto::Box.new(some_box)
67
+ box.open(cipher_text)
68
+ end
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ module Encrypto
4
+ module Database
5
+
6
+ describe Encryptor do
7
+
8
+ describe ".encrypt" do
9
+
10
+ it "creates a symmetric box based on the key" do
11
+ box = double("box").as_null_object
12
+ Box.should_receive(:from_secret_key).with("key").and_return(box)
13
+
14
+ Encryptor.encrypt({:value => "value", :key => "key"})
15
+ end
16
+
17
+ it "boxes the value" do
18
+ box = double("box")
19
+ Box.stub(:from_secret_key).and_return(box)
20
+
21
+ box.should_receive(:box).with("value")
22
+
23
+ Encryptor.encrypt({:value => "value", :key => "key"})
24
+ end
25
+
26
+ end
27
+
28
+ describe ".decrypt" do
29
+
30
+ it "creates a symmetric box based on the key" do
31
+ box = double("box").as_null_object
32
+ Box.should_receive(:from_secret_key).with("key").and_return(box)
33
+
34
+ Encryptor.decrypt({:value => "value", :key => "key"})
35
+ end
36
+
37
+ it "opens the value" do
38
+ box = double("box")
39
+ Box.stub(:from_secret_key).and_return(box)
40
+
41
+ box.should_receive(:open).with("value")
42
+
43
+ Encryptor.decrypt({:value => "value", :key => "key"})
44
+ end
45
+
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,109 @@
1
+ require 'spec_helper'
2
+
3
+ module Encrypto
4
+ describe Encrypto do
5
+
6
+ describe ".generate_keypair" do
7
+ it "delegates to Keys" do
8
+ Encrypto::Keys.should_receive(:generate_keypair)
9
+ subject.generate_keypair
10
+ end
11
+ end
12
+
13
+ describe ".generate_random_key" do
14
+ it "delegates to Random" do
15
+ Encrypto::Random.should_receive(:bytes)
16
+ subject.generate_random_key
17
+ end
18
+ end
19
+
20
+ describe '.encrypt_with_passphrase' do
21
+ it 'boxes the value in a passphrase box' do
22
+ value = double("value")
23
+ passphrase = double("passphrase")
24
+ box = double("box")
25
+
26
+ Encrypto::Box.should_receive(:from_passphrase).
27
+ with(passphrase).
28
+ and_return(box)
29
+
30
+ box.should_receive(:box).
31
+ with(value)
32
+
33
+ subject.encrypt_with_passphrase(value, passphrase)
34
+ end
35
+ end
36
+
37
+ describe ".decrypt_with_passphrase" do
38
+ it "opens the ciphertext with from passphrase box" do
39
+ passphrase = double("passphrase")
40
+ box = double("box")
41
+ cipher_text = double("cipher text")
42
+
43
+ Encrypto::Box.should_receive(:from_passphrase)
44
+ .with(passphrase)
45
+ .and_return(box)
46
+
47
+ box.should_receive(:open).
48
+ with(cipher_text)
49
+
50
+ subject.decrypt_with_passphrase(cipher_text, passphrase)
51
+ end
52
+ end
53
+
54
+ describe '.encrypt_with_keypair' do
55
+ it 'boxes the value in a keypair box' do
56
+ value = double("value")
57
+ public_key = double("public")
58
+ hex_public_key = double("hex_public_key")
59
+ signing_private_key = double("signing_private_key")
60
+ box = double("box")
61
+
62
+ Encrypto::Keys.should_receive(:hex_public_key).
63
+ with(public_key).
64
+ and_return(hex_public_key)
65
+
66
+ Encrypto::Box.should_receive(:from_keypair).
67
+ with(hex_public_key, signing_private_key).
68
+ and_return(box)
69
+
70
+ box.should_receive(:box).
71
+ with(value)
72
+
73
+ subject.encrypt_with_keypair(value, public_key, signing_private_key)
74
+ end
75
+ end
76
+
77
+ describe ".decrypt_with_keypair" do
78
+ let(:cipher_text) { double("cipher text") }
79
+ let(:hex_public_key) { double("hex public key") }
80
+ let(:public_key) { double("public key") }
81
+ let(:private_key) { double("private key") }
82
+
83
+ it "creates a public key" do
84
+ Encrypto::Keys.should_receive(:hex_public_key).with(hex_public_key)
85
+ Encrypto::Box.stub(from_keypair: double(open: nil))
86
+
87
+ subject.decrypt_with_keypair(cipher_text, hex_public_key, private_key)
88
+ end
89
+
90
+ it "decrypts the cipher text with the keypair" do
91
+ box = double
92
+
93
+ Encrypto::Keys.stub(hex_public_key: public_key)
94
+ Encrypto::Box.should_receive(:from_keypair).with(public_key, private_key).and_return(box)
95
+ box.should_receive(:open).with(cipher_text)
96
+
97
+ subject.decrypt_with_keypair(cipher_text, hex_public_key, private_key)
98
+ end
99
+
100
+ it "returns the decrypted cipher text" do
101
+ box = double(open: "decrypted value")
102
+ Encrypto::Keys.stub(hex_public_key: public_key)
103
+ Encrypto::Box.stub(from_keypair: box)
104
+
105
+ subject.decrypt_with_keypair(cipher_text, hex_public_key, private_key).should eql "decrypted value"
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ module Encrypto
4
+ describe Keys do
5
+
6
+ describe ".generate_keypair" do
7
+ it "generates a keypair" do
8
+ public_key = double("public_key", :to_s => "hex public key")
9
+ private_key = double("private key", :public_key => public_key)
10
+ Crypto::PrivateKey.should_receive(:generate).and_return(private_key)
11
+ Encrypto::Keys.generate_keypair.should eql ["hex public key", private_key]
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ module Encrypto
4
+ describe Random
5
+
6
+ describe ".bytes" do
7
+ it "creates a random byte sequence" do
8
+ random_bytes = "asf2020fasd"
9
+ Crypto::Random.should_receive(:random_bytes).and_return(random_bytes)
10
+ Encrypto::Random.bytes.should eql random_bytes
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,4 @@
1
+ require 'pry-nav'
2
+ require 'rbnacl'
3
+ require 'attr_encrypted'
4
+ require "encrypto"
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: encrypto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ruben
9
+ - Arne
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-12-04 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rbnacl
17
+ requirement: &70128989961020 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 1.1.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70128989961020
26
+ - !ruby/object:Gem::Dependency
27
+ name: attr_encrypted
28
+ requirement: &70128989960500 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *70128989960500
37
+ - !ruby/object:Gem::Dependency
38
+ name: pry-nav
39
+ requirement: &70128989959820 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 0.2.3
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70128989959820
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ requirement: &70128989959120 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 2.14.1
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70128989959120
59
+ - !ruby/object:Gem::Dependency
60
+ name: spec_coverage
61
+ requirement: &70128989958440 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 0.0.5
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *70128989958440
70
+ description: A gem that supports encrypting personal data by using rbnacl and attr_encrypted
71
+ email: service@finalist.nl
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - README.md
81
+ - encrypto.gemspec
82
+ - lib/encrypto.rb
83
+ - lib/encrypto/box.rb
84
+ - lib/encrypto/database/attr_encrypted.rb
85
+ - lib/encrypto/database/encryptor.rb
86
+ - lib/encrypto/encrypto.rb
87
+ - lib/encrypto/keys.rb
88
+ - lib/encrypto/random.rb
89
+ - lib/encrypto/version.rb
90
+ - script/bundler
91
+ - script/ci
92
+ - script/install_libsodium
93
+ - spec/encrypto/box_spec.rb
94
+ - spec/encrypto/database/encryptor_spec.rb
95
+ - spec/encrypto/encrypto_spec.rb
96
+ - spec/encrypto/keys_spec.rb
97
+ - spec/encrypto/random_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: http://github.com/finalist/encrypto
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.10
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: A gem that supports encrypting personal data by using rbnacl and attr_encrypted
123
+ test_files:
124
+ - spec/encrypto/box_spec.rb
125
+ - spec/encrypto/database/encryptor_spec.rb
126
+ - spec/encrypto/encrypto_spec.rb
127
+ - spec/encrypto/keys_spec.rb
128
+ - spec/encrypto/random_spec.rb
129
+ - spec/spec_helper.rb