cryptosystem 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8f81553a727d6fa1eaa44385bdf8e2086b7c09f9
4
+ data.tar.gz: f1077b92847b5136fd53dd548041dc9e33b3f338
5
+ SHA512:
6
+ metadata.gz: 1630aab91d71c11266a1509230b59ce8e19e354e53b0374d09c1712ecf893ebf1b5d739e4300e178c9592671c19155efe0e0ce7dedf22c77321ed69a62f04db4
7
+ data.tar.gz: f677b47d6486ddd6ee6487101a0a3cd2fb8f39882a25b6ace6c8f25ef3d231d46b75616125765f319675993077b8ccae7c148c91a61c1ce67446b9ccba686f80
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .ruby-version
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Josh Wetzel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs.push 'test'
5
+ t.pattern = 'test/**/*_test.rb'
6
+ end
7
+
8
+ task default: :test
@@ -0,0 +1,9 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'cryptosystem'
3
+ s.version = '0.0.0'
4
+ s.files = `git ls-files`.split($/)
5
+ s.summary = 'Asymmetric encryption'
6
+ s.author = 'Josh Wetzel'
7
+ s.license = 'MIT'
8
+ s.required_ruby_version = '~> 2'
9
+ end
@@ -0,0 +1,6 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+
4
+ require 'cryptosystem/base'
5
+ require 'cryptosystem/exceptions'
6
+ require 'cryptosystem/rsa'
@@ -0,0 +1,25 @@
1
+ module Cryptosystem
2
+ class Base
3
+ class << self
4
+ def configure
5
+ yield self
6
+ end
7
+ end
8
+
9
+ def expand_path(path)
10
+ File.expand_path(path)
11
+ end
12
+
13
+ def read_file(path)
14
+ File.read(path)
15
+ end
16
+
17
+ def base64_encode(value)
18
+ Base64.strict_encode64(value)
19
+ end
20
+
21
+ def base64_decode(value)
22
+ Base64.strict_decode64(value)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Cryptosystem
2
+ ConfigurationError = Class.new(StandardError)
3
+ DecryptError = Class.new(StandardError)
4
+ EncryptError = Class.new(StandardError)
5
+ end
@@ -0,0 +1,47 @@
1
+ module Cryptosystem
2
+ class RSA < Base
3
+ class << self
4
+ attr_accessor :password
5
+ attr_accessor :private_key_path
6
+ attr_accessor :public_key_path
7
+ end
8
+
9
+ attr_reader :password
10
+ attr_reader :private_key_path
11
+ attr_reader :public_key_path
12
+
13
+ def initialize
14
+ @password = Cryptosystem::RSA.password
15
+ @private_key_path = expand_path(Cryptosystem::RSA.private_key_path)
16
+ @public_key_path = expand_path(Cryptosystem::RSA.public_key_path)
17
+ rescue => error
18
+ raise ConfigurationError, error.message
19
+ end
20
+
21
+ def encrypt(value)
22
+ base64_encode(public_key.public_encrypt(value))
23
+ rescue => error
24
+ raise EncryptError, error.message
25
+ end
26
+
27
+ def decrypt(value)
28
+ private_key.private_decrypt(base64_decode(value))
29
+ rescue => error
30
+ raise DecryptError, error.message
31
+ end
32
+
33
+ private
34
+
35
+ def private_key
36
+ @private_key ||= rsa.new(read_file(private_key_path), password)
37
+ end
38
+
39
+ def public_key
40
+ @public_key ||= rsa.new(read_file(public_key_path))
41
+ end
42
+
43
+ def rsa
44
+ OpenSSL::PKey::RSA
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ class RSATest < Minitest::Test
4
+ def setup
5
+ @rsa = Cryptosystem::RSA.new
6
+ end
7
+
8
+ def test_initialize
9
+ assert_equal Cryptosystem::RSA.password, @rsa.password
10
+ assert_equal File.expand_path(@rsa.private_key_path), @rsa.private_key_path
11
+ assert_equal File.expand_path(@rsa.public_key_path), @rsa.public_key_path
12
+ end
13
+
14
+ def test_value_is_encrypted
15
+ encrypted_value = @rsa.encrypt('test')
16
+ refute_equal 'test', encrypted_value
17
+ end
18
+
19
+ def test_value_is_decrypted
20
+ encrypted_value = @rsa.encrypt('test')
21
+ assert_equal 'test', @rsa.decrypt(encrypted_value)
22
+ end
23
+
24
+ def test_encrypted_value_is_base64_encoded
25
+ assert Base64.strict_decode64(@rsa.encrypt('test'))
26
+ end
27
+
28
+ def test_decrypted_value_is_base64_decoded
29
+ assert Base64.strict_encode64(@rsa.decrypt(@rsa.encrypt('test')))
30
+ end
31
+
32
+ def test_bad_encrypt_raises_exception
33
+ assert_raises Cryptosystem::EncryptError do
34
+ @rsa.encrypt(nil)
35
+ end
36
+ end
37
+
38
+ def test_bad_decrypt_raises_exception
39
+ assert_raises Cryptosystem::DecryptError do
40
+ @rsa.decrypt(nil)
41
+ end
42
+ end
43
+ end
data/test/keys/rsa.key ADDED
@@ -0,0 +1,54 @@
1
+ -----BEGIN RSA PRIVATE KEY-----
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+ KANRPBzvPT+ZZQmzQereaksCAwEAAQ==
14
+ -----END PUBLIC KEY-----
@@ -0,0 +1,8 @@
1
+ require 'minitest/autorun'
2
+ require 'cryptosystem'
3
+
4
+ Cryptosystem::RSA.configure do |config|
5
+ config.password = 'test'
6
+ config.private_key_path = 'test/keys/rsa.key'
7
+ config.public_key_path = 'test/keys/rsa.pub'
8
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cryptosystem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Wetzel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".gitignore"
20
+ - LICENSE
21
+ - Rakefile
22
+ - cryptosystem.gemspec
23
+ - lib/cryptosystem.rb
24
+ - lib/cryptosystem/base.rb
25
+ - lib/cryptosystem/exceptions.rb
26
+ - lib/cryptosystem/rsa.rb
27
+ - test/cryptosystem/rsa_test.rb
28
+ - test/keys/rsa.key
29
+ - test/keys/rsa.pub
30
+ - test/test_helper.rb
31
+ homepage:
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '2'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 2.5.1
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Asymmetric encryption
55
+ test_files: []