gcrypto_jce 0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ea980e568ff834fe048aec8765173363c7fef97
4
+ data.tar.gz: c0e4dd9410fd14407084f3cc1c36706dfc2a9e2e
5
+ SHA512:
6
+ metadata.gz: dbbd80105e940387809faff363c7c03e02b506c3e89ee3371b94c3789c68ecddec4a59be991816ca22d72d59f96afded95cf20a68c08e118dca06bc310e3d998
7
+ data.tar.gz: e7bf81bd606ec25dfcfdd8b940f10cf742742763fee88d4c3d26aa9c1dc4f643a977ad13064f758ba4d9e068fdeb2028d506b53ac9bf3b6c17d0d34708c81f31
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ tags
11
+ /test/artifacts/*
12
+ !/test/artifacts/.gitkeep
@@ -0,0 +1 @@
1
+ jruby-9.1.17.0
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gcrypto_jce.gemspec
4
+ gemspec
5
+
6
+ gem 'pkernel', path: '../../pkernel/pkernel'
7
+ gem 'pkernel_jce', path: '../../pkernel/pkernel_jce'
8
+
9
+ gem 'gcrypto', path: '../gcrypto'
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: ../../pkernel/pkernel_jce
3
+ specs:
4
+ pkernel_jce (0.1.0)
5
+ activesupport
6
+ tlogger
7
+
8
+ PATH
9
+ remote: ../../pkernel/pkernel
10
+ specs:
11
+ pkernel (0.1.0)
12
+ tlogger
13
+
14
+ PATH
15
+ remote: ../gcrypto
16
+ specs:
17
+ gcrypto (0.1.0)
18
+ pkernel
19
+ tlogger
20
+
21
+ PATH
22
+ remote: .
23
+ specs:
24
+ gcrypto_jce (0.1.0)
25
+ activesupport
26
+ pkernel
27
+ pkernel_jce
28
+ tlogger
29
+
30
+ GEM
31
+ remote: https://rubygems.org/
32
+ specs:
33
+ activesupport (5.2.3)
34
+ concurrent-ruby (~> 1.0, >= 1.0.2)
35
+ i18n (>= 0.7, < 2)
36
+ minitest (~> 5.1)
37
+ tzinfo (~> 1.1)
38
+ concurrent-ruby (1.1.5)
39
+ i18n (1.6.0)
40
+ concurrent-ruby (~> 1.0)
41
+ minitest (5.11.3)
42
+ rake (10.5.0)
43
+ thread_safe (0.3.6-java)
44
+ tlogger (0.8.0)
45
+ tzinfo (1.2.5)
46
+ thread_safe (~> 0.1)
47
+
48
+ PLATFORMS
49
+ java
50
+
51
+ DEPENDENCIES
52
+ bundler (~> 2.0)
53
+ gcrypto!
54
+ gcrypto_jce!
55
+ pkernel!
56
+ pkernel_jce!
57
+ rake (~> 10.0)
58
+
59
+ BUNDLED WITH
60
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Chris Liaw
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.
@@ -0,0 +1,39 @@
1
+ # GcryptoJce
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/gcrypto_jce`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'gcrypto_jce'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install gcrypto_jce
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gcrypto_jce.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gcrypto_jce"
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(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,47 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "gcrypto_jce/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gcrypto_jce"
8
+ spec.version = GcryptoJce::VERSION
9
+ spec.authors = ["Chris Liaw"]
10
+ spec.email = ["chrisliaw@antrapol.com"]
11
+
12
+ spec.summary = %q{}
13
+ spec.description = %q{}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ #if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+
22
+ # spec.metadata["homepage_uri"] = spec.homepage
23
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
24
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
25
+ #else
26
+ # raise "RubyGems 2.0 or newer is required to protect against " \
27
+ # "public gem pushes."
28
+ #end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_development_dependency "bundler", "~> 2.0"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+
42
+ spec.add_dependency "tlogger"
43
+ spec.add_dependency "pkernel"
44
+ spec.add_dependency "pkernel_jce"
45
+ spec.add_dependency 'activesupport'
46
+
47
+ end
Binary file
@@ -0,0 +1,49 @@
1
+
2
+ require 'gcrypto'
3
+ require "gcrypto_jce/version"
4
+
5
+ require_relative 'gcrypto_jce/keypair_crypto'
6
+ require_relative 'gcrypto_jce/secretkey'
7
+ require_relative 'gcrypto_jce/secretkey_crypto'
8
+ require_relative 'gcrypto_jce/secure_random'
9
+ require_relative 'gcrypto_jce/digest'
10
+ require_relative 'gcrypto_jce/hkdf'
11
+ require_relative 'gcrypto_jce/pbkdf2'
12
+ require_relative 'gcrypto_jce/converter'
13
+
14
+ module GcryptoJce
15
+ class Error < StandardError; end
16
+ # Your code goes here...
17
+
18
+ class Gcrypto::KeyPairCrypto
19
+ extend GcryptoJce::KeyPairCrypto
20
+ end
21
+
22
+ class Gcrypto::SecretKey
23
+ extend GcryptoJce::SecretKey
24
+ end
25
+
26
+ class Gcrypto::SecretKeyCrypto
27
+ extend GcryptoJce::SecretKeyCrypto
28
+ end
29
+
30
+ class Gcrypto::SecureRandom
31
+ extend GcryptoJce::SecureRandom
32
+ end
33
+
34
+ class Gcrypto::Digest
35
+ extend GcryptoJce::Digest
36
+ end
37
+
38
+ class Gcrypto::HKDF
39
+ extend GcryptoJce::KDF::HKDF
40
+ end
41
+
42
+ class Gcrypto::PBKDF2
43
+ extend GcryptoJce::KDF::PBKDF2
44
+ end
45
+
46
+ class Gcrypto::Converter
47
+ extend GcryptoJce::Converter
48
+ end
49
+ end
@@ -0,0 +1,29 @@
1
+
2
+
3
+ module GcryptoJce
4
+ module Converter
5
+ def to_hex(bin)
6
+ String.from_java_bytes(org.bouncycastle.util.encoders.Hex.encode(bin))
7
+ end
8
+ # end to_hex
9
+
10
+ def from_hex(str)
11
+ org.bouncycastle.util.encoders.Hex.decode(str)
12
+ end
13
+ # end from_hex
14
+
15
+ def to_b64(bin)
16
+ String.from_java_bytes(java.util.Base64.encoder.encode(bin))
17
+ end
18
+ # end to_b64
19
+
20
+ def from_b64(str)
21
+ java.util.Base64.decoder.decode(str)
22
+ end
23
+ # end from_b64
24
+
25
+ end
26
+ # module Converter
27
+ #
28
+ end
29
+ # module PkernelJce
@@ -0,0 +1,56 @@
1
+
2
+ require_relative 'error'
3
+ require_relative 'io_utils'
4
+ require_relative 'provider'
5
+ require_relative 'global'
6
+ #require_relative 'secretkey'
7
+
8
+
9
+ module GcryptoJce
10
+ module Digest
11
+
12
+ def generate(opts = { })
13
+
14
+ digest = opts[:digest] || :sha256
15
+ digest = digest.to_s.upcase if not (digest.nil? or digest.empty?)
16
+ is = IoUtils.load_input(opts)
17
+ prov = GcryptoJce::Provider.handle_options(opts)
18
+ if prov.nil?
19
+ md = java.security.MessageDigest.getInstance(digest)
20
+ else
21
+ md = java.security.MessageDigest.getInstance(digest,prov)
22
+ end
23
+
24
+ begin
25
+ bufConf = opts[:int_buf] || { }
26
+ total = 0
27
+ IoUtils.read_chunk(is, bufConf) do |buf, from, len|
28
+ md.update(buf, from, len)
29
+ total += len
30
+ GcryptoJce::GConf.instance.glog.debug "Processed #{NumberHelper.number_to_human_size(total)}"
31
+ end
32
+
33
+ val = md.digest
34
+ String.from_java_bytes(org.bouncycastle.util.encoders.Hex.encode(val))
35
+
36
+ rescue Exception
37
+ ensure
38
+ begin
39
+ is.close
40
+ rescue Exception
41
+ end
42
+ end
43
+
44
+ end
45
+ # end method generate()
46
+ #
47
+
48
+ end
49
+ # end module Digest
50
+ #
51
+
52
+ class DigestEngine
53
+ extend Digest
54
+ end
55
+
56
+ end
@@ -0,0 +1,6 @@
1
+
2
+
3
+ module GcryptoJce
4
+ class Error < StandardError; end
5
+
6
+ end
@@ -0,0 +1,26 @@
1
+
2
+ require 'singleton'
3
+ require 'tlogger'
4
+
5
+ module GcryptoJce
6
+ class GConf
7
+ include Singleton
8
+ attr_reader :logger_params, :glog
9
+ def initialize
10
+ @logger_params = STDOUT
11
+ @glog = Tlogger.new(STDOUT)
12
+ @glog.tag = :gcrypto_jce
13
+ @glog.show_source
14
+ end
15
+
16
+ def java_version
17
+ if @java_ver.nil?
18
+ @java_ver = ENV_JAVA['java.version']
19
+ end
20
+
21
+ @java_ver
22
+ end
23
+ end
24
+ end
25
+
26
+
@@ -0,0 +1,143 @@
1
+
2
+
3
+
4
+ require 'gcrypto'
5
+ require_relative 'secretkey'
6
+ require_relative 'error'
7
+ require_relative 'io_utils'
8
+ require_relative 'provider'
9
+ require_relative 'global'
10
+
11
+ module GcryptoJce
12
+ module KDF
13
+ module HKDF
14
+
15
+ #def extract(opts = { })
16
+ #
17
+ # salt = opts[:salt]
18
+ # if salt.nil?
19
+ # salt = Java::byte[hmac.mac_length].new
20
+ # end
21
+ #
22
+ # secKey = javax.crypto.spec.SecretKeySpec.new(salt, "AES")
23
+ #
24
+ # hmacSpec = opts[:hmacEng] || 'HmacSHA256'
25
+ # hmac = javax.crypto.Mac.getInstance(hmacSpec)
26
+ # hmac.init(secKey)
27
+
28
+ # ikm = opts[:ikm]
29
+ # if ikm.nil?
30
+ # raise GcryptoJce::Error, "Input Key Material (IKM) cannot be nil. It can be any random value or message itself"
31
+ # end
32
+ # is = IoUtils.load_input(ikm)
33
+ #
34
+ # os = java.io.ByteArrayOutputStream.new
35
+
36
+ # begin
37
+ # bufConf = opts[:int_buf] || { }
38
+ # total = 0
39
+ # IoUtils.read_chunk(is, bufConf) do |buf, from, len|
40
+ # os.write(hmac.update(buf, from, len))
41
+ # total += len
42
+ # GcryptoJce::GConf.instance.glog.debug "Processed #{NumberHelper.number_to_human_size(total)}"
43
+ # end
44
+
45
+ # os.write(hmac.doFinal)
46
+ # rescue Exception
47
+ # ensure
48
+ # begin
49
+ # is.close
50
+ # os.flush
51
+ # os.close
52
+ # rescue Exception
53
+ # end
54
+ # end
55
+
56
+ #
57
+ #end
58
+ # end extract
59
+ #
60
+
61
+ # allow key to be expended to arbitary length according to RFC5869
62
+ def expand(opts = {})
63
+
64
+ key = opts[:key]
65
+ if key.nil?
66
+ raise GcryptoJce::Error, "Key is not given to HKDF for expansion operation"
67
+ else
68
+ key = IoUtils.ensure_java_bytes(key)
69
+ end
70
+
71
+ kcType = opts[:key_type] || :aes
72
+ case kcType
73
+ when :aes, "AES", "aes"
74
+ secKey = javax.crypto.spec.SecretKeySpec.new(key, "AES")
75
+ else
76
+ raise GcryptoJce::Error, "Unknown key type '#{kcType}'"
77
+ end
78
+
79
+ hmacSpec = opts[:hmacEng] || 'HmacSHA256'
80
+ hmac = javax.crypto.Mac.getInstance(hmacSpec)
81
+ hmac.init(secKey)
82
+
83
+ ctxInfo = opts[:contextInfo]
84
+ if ctxInfo.nil?
85
+ ctxInfoBytes = "".to_java.getBytes
86
+ else
87
+ ctxInfoBytes = IoUtils.ensure_java_bytes(ctxInfo)
88
+ end
89
+
90
+ baseBuf = java.io.ByteArrayOutputStream.new
91
+ baseBuf.write(key)
92
+ baseBuf.write(ctxInfoBytes)
93
+ baseBufBin = baseBuf.toByteArray
94
+ baseBufLen = baseBufBin.length
95
+
96
+ GcryptoJce::GConf.instance.glog.debug "baseBufBin : #{baseBufBin}"
97
+
98
+ baseBlock = Java::byte[baseBufLen+1].new
99
+ java.lang.System.arraycopy(baseBufBin,0,baseBlock,0,baseBufLen)
100
+ GcryptoJce::GConf.instance.glog.debug "baseBlock : #{baseBlock}"
101
+
102
+ # always in bits
103
+ reqKeyLen = opts[:outKeyLen]
104
+ loopCnt = ((reqKeyLen*1.0) / (hmac.mac_length*8*1.0)).ceil
105
+ GcryptoJce::GConf.instance.glog.debug "loopCnt : #{loopCnt}"
106
+
107
+ exOut = java.io.ByteArrayOutputStream.new
108
+
109
+ cnt = 0
110
+ while (cnt < loopCnt) do
111
+ baseBlock[baseBlock.length-1] = cnt
112
+ #GcryptoJce::GConf.instance.glog.debug "baseBlock (#{cnt}) : #{baseBlock[baseBlock.length-1]}"
113
+ hash = hmac.doFinal(baseBlock)
114
+ exOut.write(hash)
115
+ cnt += 1
116
+ end
117
+
118
+ expanded = exOut.toByteArray
119
+ ret = Java::byte[reqKeyLen/8].new
120
+ java.lang.System.arraycopy(expanded,0,ret,0,ret.length)
121
+
122
+ ret
123
+
124
+ end
125
+ # end expand
126
+ #
127
+
128
+ end
129
+ # end HKDF
130
+ #
131
+
132
+ class HKDFEngine
133
+ extend HKDF
134
+ end
135
+
136
+ end
137
+ # end KDF
138
+ #
139
+ end
140
+ # end GcryptoJce
141
+
142
+
143
+