ossl_rsa 0.1.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: 3ea603bcf1f6ccd4ce66c1bac0791dc568953a38
4
+ data.tar.gz: a0904b70de93fb4397138a7bc746c481bba154a8
5
+ SHA512:
6
+ metadata.gz: ad76ee44d12a44ba7a6df406042a7d2a7fc66da1ba57c8c9be7c3cc41416a0aadd2325a4cb4874f32cc11ef27793ad18ca0b03177df85294cc593243e8cd4e3e
7
+ data.tar.gz: 86e96223cbe904cff5bfa86f738dfd92a1cd3c454e748adf4e36d04742f57e060f51df64ab554d3c2488280a716d054c01000871daea50b19e8218fd54836d90
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.8
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ossl_rsa.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 h.shigemoto
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # OsslRsa
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ossl_rsa`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ This gem provide RSA encryption and sign, verify by openssl lib.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ossl_rsa'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ossl_rsa
22
+
23
+ ## Usage
24
+
25
+ Encrypt and decrypt sample.
26
+
27
+ ```ruby
28
+ require 'ossl_rsa'
29
+
30
+ rsa = OsslRsa::Rsa.new({size: 2048})
31
+ enc_value = rsa.encrypt("encrypt value")
32
+ puts enc_value
33
+
34
+ dec_value = rsa.decrypt(enc_value)
35
+ puts dec_value
36
+ ```
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/h-shigemoto/ossl_rsa. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
47
+
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
52
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ossl_rsa"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,207 @@
1
+ require "openssl"
2
+ require "base64"
3
+ require "securerandom"
4
+
5
+ # openssl rsa module.
6
+ module OsslRsa
7
+
8
+ # rsa class
9
+ class Rsa
10
+
11
+ # constructor.
12
+ # generate rsa instance by options.
13
+ # options[:size] : key size
14
+ # options[:obj] : pem or der
15
+ # options[:pass] : password
16
+ # @param [Hash] options generate options.
17
+ def initialize(options={})
18
+
19
+ # if size and private exist, raise error.
20
+ if (!options[:size].nil? && !options[:obj].nil?)
21
+ raise OpenSSL::PKey::RSAError "size and obj is nil."
22
+ end
23
+
24
+ # if exist size, generate use size, cipher.
25
+ unless options[:size].nil?
26
+ @rsa = generate_rsa_by_size(options[:size])
27
+ end
28
+
29
+ # if exist obj, generate use obj, pass.
30
+ unless options[:obj].nil?
31
+ @rsa = generate_rsa_by_key(options[:obj], options[:pass])
32
+ end
33
+
34
+ # raise Error
35
+ if @rsa.nil?
36
+ raise OpenSSL::PKey::RSAError "fail create rsa instance."
37
+ end
38
+ end
39
+
40
+ # encrypt RSA. use public_key.
41
+ # @param [String] value encrypt target value.
42
+ # @param [integer] mode padding mode.
43
+ # @param [boolean] base64_encode base64 encode flag.
44
+ # @return [String] encrypt value.
45
+ def encrypt(value, mode=OpenSSL::PKey::RSA::PKCS1_PADDING, base64_encode=true)
46
+
47
+ # not public, return nil.
48
+ return nil unless @rsa.public?
49
+
50
+ encrypt_value = @rsa.public_encrypt(value, mode)
51
+ # base64 encode.
52
+ if base64_encode
53
+ encrypt_value = Base64.encode64(encrypt_value)
54
+ end
55
+ encrypt_value
56
+ end
57
+
58
+ # decrypt RSA. use private_key.
59
+ # @param [String] value decrypt target value.
60
+ # @param [integer] mode padding mode.
61
+ # @param [boolean] base64_decode base64 decode flag.
62
+ # @return [String] decrypt value.
63
+ def decrypt(value, mode=OpenSSL::PKey::RSA::PKCS1_PADDING, base64_decode=true)
64
+
65
+ # not private, return nil.
66
+ return nil unless @rsa.private?
67
+
68
+ # base64 decode
69
+ if base64_decode
70
+ value = Base64.decode64(value)
71
+ end
72
+
73
+ decrypt_value = @rsa.private_decrypt(value, mode)
74
+ decrypt_value
75
+ end
76
+
77
+ # sign data.
78
+ # @param [String] digest digest method.
79
+ # @param [String] value sign target value.
80
+ # @param [boolean] base64_encode base64 encode flag.
81
+ # @return [String] sign value.
82
+ def sign(digest, value, base64_encode=true)
83
+
84
+ # not private, return nil.
85
+ return nil unless @rsa.private?
86
+
87
+ sign_value = @rsa.sign(digest, value)
88
+ # base64 encode.
89
+ if base64_encode
90
+ sign_value = Base64.encode64(sign_value)
91
+ end
92
+ sign_value
93
+ end
94
+
95
+ # verify sign value.
96
+ # @param [String] digest digest method.
97
+ # @param [String] sign sign value.
98
+ # @param [String] value compare target value.
99
+ # @param [boolean] base64_decode base64 decode flag.
100
+ # @return [boolean] verify result.
101
+ def verify(digest, sign, value, base64_decode=true)
102
+
103
+ # not private, return false.
104
+ return false unless @rsa.private?
105
+
106
+ # base64 decode.
107
+ if base64_decode
108
+ sign = Base64.decode64(sign)
109
+ end
110
+ @rsa.verify(digest, sign, value)
111
+ end
112
+
113
+ # private check.
114
+ # @return [boolean] OpsnSSL::PKey::RSA.private?
115
+ def private?
116
+ @rsa.private?
117
+ end
118
+
119
+ # public check.
120
+ # @return [boolean] OpenSSL::PKey::RSA.public?
121
+ def public?
122
+ @rsa.public?
123
+ end
124
+
125
+ # get private and public key.
126
+ # private key set self OpenSSL::PKey::RSA instance.
127
+ # @return [Hash] key pair hash. xx[:private] = private_key, xx[:public] = public_key
128
+ def key_pair(mode, cipher=nil, pass=nil)
129
+
130
+ private_key = get_private_key(mode, cipher, pass)
131
+ public_key = get_public_key(mode, cipher, pass)
132
+
133
+ { private: private_key, public: public_key }
134
+ end
135
+
136
+ private
137
+
138
+ # generate rsa.
139
+ # @param [integer] size key size.
140
+ # @return [OpenSSL::PKey::RSA] rsa instance.
141
+ def generate_rsa_by_size(size)
142
+
143
+ # add seed.
144
+ OpenSSL::Random.seed(SecureRandom.hex(8))
145
+ # generate rsa instance.
146
+ rsa = OpenSSL::PKey::RSA.new(size)
147
+ rsa
148
+ end
149
+
150
+ # generate rsa.
151
+ # @param [String] pem / der.
152
+ # @param [String] pass password
153
+ # @return [OpenSSL::PKey::RSA] rsa instance.
154
+ def generate_rsa_by_key(obj, pass=nil)
155
+
156
+ rsa = OpenSSL::PKey::RSA.new(obj, pass)
157
+ rsa
158
+ end
159
+
160
+ # get private key.
161
+ # @param [integer] mode pem or der.
162
+ # @param [OpenSSL::Cipher] cipher cipher instance.
163
+ # @param [String] pass password
164
+ # @return [String] pem or der private key.
165
+ def get_private_key(mode, cipher, pass)
166
+
167
+ # if not have private_key, return nil
168
+ return nil unless @rsa.private?
169
+
170
+ cv_key = convert_key(mode, @rsa, cipher, pass)
171
+ cv_key
172
+ end
173
+
174
+ # get public key.
175
+ # @param [integer] mode pem or der.
176
+ # @param [OpenSSL::Cipher] cipher cipher instance.
177
+ # @param [String] pass password
178
+ # @return [String] pem or der public key.
179
+ def get_public_key(mode, cipher, pass)
180
+
181
+ # if not have public_key, return nil
182
+ return nil unless @rsa.public?
183
+
184
+ cv_key = convert_key(mode, @rsa.public_key, cipher, pass)
185
+ cv_key
186
+ end
187
+
188
+ # convert key to pem or der.
189
+ # @param [integer] mode pem or der.
190
+ # @param [OpsnSSL::PKey::RSA] key rsa key.
191
+ # @param [OpenSSL::Cipher] cipher cipher instance.
192
+ # @param [String] pass password
193
+ # @return [String] pem or der key.
194
+ def convert_key(mode, key, cipher, pass)
195
+
196
+ cv_key = nil
197
+ # convert public key.
198
+ if mode == PEM
199
+ cv_key = key.to_pem(cipher, pass)
200
+ elsif mode == DER
201
+ cv_key = key.to_der
202
+ end
203
+
204
+ cv_key
205
+ end
206
+ end
207
+ end
@@ -0,0 +1,3 @@
1
+ module OsslRsa
2
+ VERSION = "0.1.0"
3
+ end
data/lib/ossl_rsa.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "ossl_rsa/version"
2
+ require "ossl_rsa/rsa"
3
+
4
+ # openssl rsa module.
5
+ module OsslRsa
6
+
7
+ # pem mode.
8
+ PEM = 0
9
+ # der mode.
10
+ DER = 1
11
+ end
data/ossl_rsa.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ossl_rsa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ossl_rsa"
8
+ spec.version = OsslRsa::VERSION
9
+ spec.authors = ["h.shigemoto"]
10
+ spec.email = ["corporation.ore@gmail.com"]
11
+
12
+ spec.summary = %q{This gem provide RSA encryption and sign, verify.}
13
+ spec.description = %q{This gem provide RSA encryption and sign, verify by openssl lib.}
14
+ spec.homepage = "https://github.com/h-shigemoto/ossl_rsa"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ossl_rsa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - h.shigemoto
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: This gem provide RSA encryption and sign, verify by openssl lib.
56
+ email:
57
+ - corporation.ore@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/ossl_rsa.rb
73
+ - lib/ossl_rsa/rsa.rb
74
+ - lib/ossl_rsa/version.rb
75
+ - ossl_rsa.gemspec
76
+ homepage: https://github.com/h-shigemoto/ossl_rsa
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: This gem provide RSA encryption and sign, verify.
100
+ test_files: []
101
+ has_rdoc: