ktct 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
+ SHA256:
3
+ metadata.gz: 95a1da6215d98401a8b9374f3771f99f54e498a7e66e3d77eceb4aa6d325a0bd
4
+ data.tar.gz: 9daa6debac46407f51303273b4c4ff6e3692b104860964b2d58dd5fc4f4ae1dd
5
+ SHA512:
6
+ metadata.gz: 5a9c7f806351c0e5072f7239945136cb390269427738b8de0b97ebd5fa95b564b1979d50509549b7fa5683655952ac1d8d3653fdcd0f0f60154600630480f102
7
+ data.tar.gz: b2b6d3b97b907b0cea81009fbaf023497d6ee3148570f7f2393f3a31bb2379eb581fa478beae78487725d2b66a9a1e2595c154e0120700aba3f932da28acd349
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at liuxiang@ktjr.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ktct.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ktct (0.1.0)
5
+ thor (~> 0.20.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ byebug (11.0.1)
11
+ coderay (1.1.2)
12
+ diff-lcs (1.3)
13
+ method_source (0.9.2)
14
+ pry (0.12.2)
15
+ coderay (~> 1.1.0)
16
+ method_source (~> 0.9.0)
17
+ pry-byebug (3.7.0)
18
+ byebug (~> 11.0)
19
+ pry (~> 0.10)
20
+ rake (10.5.0)
21
+ rspec (3.8.0)
22
+ rspec-core (~> 3.8.0)
23
+ rspec-expectations (~> 3.8.0)
24
+ rspec-mocks (~> 3.8.0)
25
+ rspec-core (3.8.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-expectations (3.8.3)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-mocks (3.8.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.8.0)
33
+ rspec-support (3.8.0)
34
+ thor (0.20.3)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ bundler (~> 1.17)
41
+ ktct!
42
+ pry-byebug (>= 3.6.0)
43
+ rake (~> 10.0)
44
+ rspec (~> 3.0)
45
+
46
+ BUNDLED WITH
47
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Liu Xiang
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,92 @@
1
+ # Ktct
2
+
3
+ KT Crypto Tools
4
+
5
+ ## Installation
6
+
7
+
8
+ Install it yourself as:
9
+
10
+ $ gem install ktct
11
+
12
+ Or:
13
+
14
+ $ sudo gem install ktct
15
+
16
+ ## Usage
17
+
18
+ ```
19
+ Commands:
20
+ ktct bf <SUB_COMMAND> <OPTS> # crypto utils for baofu
21
+ ktct help [COMMAND] # Describe available commands or one specific command
22
+ ktct wjs <SUB_COMMAND> <OPTS> # crypto utils for wangjinshe
23
+
24
+ Options:
25
+ -c, [--config=CONFIG]
26
+ # Default: ~/.ktct.yml
27
+
28
+ bf Sub-commands:
29
+
30
+ ktct bf de_decrypt <DIGITAL_ENVELOP> [DATA] # Decrypt data using specified digital envelop, DATA can also be read from STDIN. Both encrypted and unencrypted digital envelop are supported.
31
+ ktct bf de_encrypt <DIGITAL_ENVELOP> [DATA] # Encrypt data using specified digital envelop, DATA can also be read from STDIN
32
+ ktct bf de_gen # Generate a random digital envelop
33
+ ktct bf decrypt [DATA] # Decrypt data using client's private key, DATA can also be read from STDIN. This command should be used for decrypting digital envelop in protocol payment API.
34
+ ktct bf decrypt_de <DIGITAL_ENVELOP> # Decrypt encrypted digital envelop using client's private key
35
+ ktct bf encrypt [DATA] # Encrypt data using Baofu's public key, DATA can also be read from STDIN. This command should be used for encrypting digital envelop in protocol payment API.
36
+ ktct bf help [COMMAND] # Describe subcommands or one specific subcommand
37
+ ktct bf sdecrypt [DATA] # Decrypt data using Baofu's public key, DATA can also be read from STDIN. This command should be used for decrypting data content in batch payment API.
38
+ ktct bf sencrypt [DATA] # Encrypt data using client's private key, DATA can also be read from STDIN. This command should be used for encrypting data content in batch payment API.
39
+ ktct bf ssign [DATA] # Sign data using client's private key, DATA can also be read from STDIN. This command should be used for signning params in protocol payment API.
40
+ ktct bf sverify <SIGNATURE> [DATA] # Verify signature using Baofu's public key, DATA can also be read from STDIN. This command should be used for verifying params signature in protocol payment ..
41
+
42
+ wjs Sub-commands:
43
+
44
+ ktct wjs decrypt [DATA] # Decrypt data using client's private key, DATA can also be read from STDIN
45
+ ktct wjs encrypt [DATA] # Encrypt data using Wangjinshe's public key, DATA can also be read from STDIN
46
+ ktct wjs help [COMMAND] # Describe subcommands or one specific subcommand
47
+ ktct wjs sign [DATA] # Sign data using client's private key, DATA can also be read from STDIN
48
+ ```
49
+
50
+ ## Config file Example
51
+
52
+ ```
53
+ default_env: demo
54
+ demo:
55
+ bf:
56
+ payee-private-key-path: /my/demo/keys/client.pfx
57
+ payee-private-key-passphrase: "12345678"
58
+ platform-certificate-path: /my/demo/keys/server.cer
59
+ wjs:
60
+ server_public_key_path: /my/demo/keys/client.pkcs8
61
+ client_private_key_path: /my/demo/keys/server.pkcs8
62
+
63
+ pro:
64
+ bf:
65
+ payee-private-key-path: /my/pro/keys/client.pfx
66
+ payee-private-key-passphrase: "12345678"
67
+ platform-certificate-path: /my/pro/keys/server.cer
68
+ wjs:
69
+ server_public_key_path: /my/pro/keys/client.pkcs8
70
+ client_private_key_path: /my/pro/keys/server.pkcs8
71
+ ```
72
+
73
+ Selected config environment could be specified via the `KTCT_ENV` environment variable.
74
+ If no `KTCT_ENV` specified, the value of `default_env` option in config yaml would be used.
75
+
76
+ ## Development
77
+
78
+ 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.
79
+
80
+ 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).
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lululau/ktct. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
85
+
86
+ ## License
87
+
88
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
89
+
90
+ ## Code of Conduct
91
+
92
+ Everyone interacting in the Ktct project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/lululau/ktct/blob/master/CODE_OF_CONDUCT.md).
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 "ktct"
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__)
data/bin/setup ADDED
@@ -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
data/exe/ktct ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ktct'
4
+
5
+ Ktct::Cli.start
data/ktct.gemspec ADDED
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ktct/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ktct"
8
+ spec.version = Ktct::VERSION
9
+ spec.authors = ["Liu Xiang"]
10
+ spec.email = ["liuxiang921@gmail.com"]
11
+
12
+ spec.summary = %q{Utilities for crypto things}
13
+ spec.description = %q{Utilities for crypto things}
14
+ spec.homepage = "https://github.com/lululau/ktct"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency 'thor', '~> 0.20.0'
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.17"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "pry-byebug", ">= 3.6.0"
32
+ end
data/lib/ktct.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "ktct/version"
2
+ require 'ktct/config'
3
+ require 'ktct/cryptable'
4
+ require 'ktct/digital_envelop'
5
+ require 'ktct/baofu'
6
+ require 'ktct/wangjinshe'
7
+ require 'ktct/cli'
data/lib/ktct/baofu.rb ADDED
@@ -0,0 +1,89 @@
1
+ require 'thor'
2
+ require 'openssl'
3
+
4
+ module Ktct
5
+ class Baofu < ::Thor
6
+ include Ktct::Config
7
+ include Ktct::Cryptable
8
+
9
+ no_commands do
10
+ alias_method :original_decrypt, :decrypt
11
+ end
12
+
13
+ desc 'encrypt [DATA]', "Encrypt data using Baofu's public key, DATA can also be read from STDIN. This command should be used for encrypting digital envelop in protocol payment API."
14
+ def encrypt(data = nil)
15
+ reset_key(config['platform-certificate-path'])
16
+ puts super(data || STDIN.read)
17
+ end
18
+
19
+ desc 'decrypt [DATA]', "Decrypt data using client's private key, DATA can also be read from STDIN. This command should be used for decrypting digital envelop in protocol payment API."
20
+ def decrypt(data = nil)
21
+ reset_key(config['payee-private-key-path'], config['payee-private-key-passphrase'])
22
+ puts super(data || STDIN.read)
23
+ end
24
+
25
+ desc 'sencrypt [DATA]', "Encrypt data using client's private key, DATA can also be read from STDIN. This command should be used for encrypting data content in batch payment API."
26
+ def sencrypt(data = nil)
27
+ reset_key(config['payee-private-key-path'], config['payee-private-key-passphrase'])
28
+ puts stupid_encrypt(data || STDIN.read)
29
+ end
30
+
31
+ desc 'sdecrypt [DATA]', "Decrypt data using Baofu's public key, DATA can also be read from STDIN. This command should be used for decrypting data content in batch payment API."
32
+ def sdecrypt(data = nil)
33
+ reset_key(config['platform-certificate-path'])
34
+ puts stupid_decrypt(data || STDIN.read)
35
+ end
36
+
37
+ desc 'ssign [DATA]', "Sign data using client's private key, DATA can also be read from STDIN. This command should be used for signning params in protocol payment API."
38
+ def ssign(data = nil)
39
+ reset_key(config['payee-private-key-path'], config['payee-private-key-passphrase'])
40
+ puts super(data || STDIN)
41
+ end
42
+
43
+ desc 'sverify <SIGNATURE> [DATA]', "Verify signature using Baofu's public key, DATA can also be read from STDIN. This command should be used for verifying params signature in protocol payment API."
44
+ def sverify(signature, data = nil)
45
+ reset_key(config['platform-certificate-path'])
46
+ puts super(signature, data || STDIN.read)
47
+ end
48
+
49
+ desc 'de_encrypt <DIGITAL_ENVELOP> [DATA]', 'Encrypt data using specified digital envelop, DATA can also be read from STDIN'
50
+ def de_encrypt(digital_envelop, data = nil)
51
+ puts DigitalEnvelop.new(digital_envelop).encrypt(data || STDIN.read)
52
+ end
53
+
54
+ desc 'de_decrypt <DIGITAL_ENVELOP> [DATA]', 'Decrypt data using specified digital envelop, DATA can also be read from STDIN. Both encrypted and unencrypted digital envelop are supported.'
55
+ def de_decrypt(digital_envelop, data = nil)
56
+ digital_envelop = decrypt_digital_envelop(digital_envelop)
57
+ puts DigitalEnvelop.new(digital_envelop).decrypt(data || STDIN.read)
58
+ end
59
+
60
+ desc 'de_gen', 'Generate a random digital envelop'
61
+ def de_gen
62
+ original = DigitalEnvelop.get
63
+ puts "Original: #{original}"
64
+ print 'Encrypted: '
65
+ encrypted = encrypt(original.to_s)
66
+ end
67
+
68
+ desc 'decrypt_de <DIGITAL_ENVELOP>', "Decrypt encrypted digital envelop using client's private key"
69
+ def decrypt_de(digital_envelop)
70
+ decrypt(digital_envelop)
71
+ end
72
+
73
+ no_commands do
74
+ def config
75
+ super['bf']
76
+ end
77
+
78
+ def decrypt_digital_envelop(raw)
79
+ raw = raw.chomp.strip
80
+ if raw.size == 19 && raw[2] == '|'
81
+ raw
82
+ else
83
+ reset_key(config['payee-private-key-path'], config['payee-private-key-passphrase'])
84
+ original_decrypt(raw)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
data/lib/ktct/cli.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'thor'
2
+
3
+ module Ktct
4
+ class Cli < ::Thor
5
+ class_option :config, aliases: ['-c'], type: :string, required: false, default: '~/.ktct.yml'
6
+
7
+ desc 'bf <SUB_COMMAND> <OPTS>', 'crypto utils for baofu'
8
+ subcommand :bf, Ktct::Baofu
9
+
10
+ desc 'wjs <SUB_COMMAND> <OPTS>', 'crypto utils for wangjinshe'
11
+ subcommand :wjs, Ktct::Wangjinshe
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'yaml'
2
+
3
+ module Ktct
4
+ module Config
5
+ def config
6
+ return @config if @config
7
+ config_file_name = options[:config]
8
+ yaml = YAML.load(File.read(File.expand_path(config_file_name)))
9
+ env = ENV['KTCT_ENV'] || yaml['default_env']
10
+ yaml[env]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,84 @@
1
+ require 'zlib'
2
+ require 'base64'
3
+ require 'digest'
4
+
5
+ module Ktct
6
+ module Cryptable
7
+ %i[public_encrypt private_decrypt private_encrypt public_decrypt].each do |type|
8
+ define_method type do |message|
9
+ crypt(message, type)
10
+ end
11
+ end
12
+
13
+ attr_accessor :key
14
+
15
+ def encrypt(message)
16
+ public_encrypt(Base64.strict_encode64(message)).unpack('H*').first
17
+ end
18
+
19
+ def decrypt(message)
20
+ Base64.strict_decode64(private_decrypt([message].pack('H*')))
21
+ end
22
+
23
+ def sverify(signature, message)
24
+ key.verify('sha1', [signature].pack('H*'), Digest::SHA1.hexdigest(message))
25
+ end
26
+
27
+ def ssign(message)
28
+ key.sign('sha1', Digest::SHA1.hexdigest(message)).unpack('H*').first
29
+ end
30
+
31
+ def stupid_decrypt(message)
32
+ Base64.decode64(public_decrypt([message].pack('H*'))).force_encoding('UTF-8')
33
+ end
34
+
35
+ def stupid_encrypt(message)
36
+ private_encrypt(Base64.encode64(message)).unpack('H*').first
37
+ end
38
+
39
+ def stupid_decrypt_batch(message)
40
+ Zlib.gunzip(public_decrypt([message].pack('H*'))).force_encoding('UTF-8')
41
+ end
42
+
43
+ def stupid_encrypt_batch(message)
44
+ private_encrypt(Zlib.gzip(message)).unpack('H*').first
45
+ end
46
+
47
+ def crypt(message, type)
48
+ bytes_array = message.unpack('C*')
49
+ input_length = bytes_array.length
50
+
51
+ result, offset, i = "", 0, 0
52
+ block_size = send("max_#{type.to_s.split('_').last}_block_size")
53
+ begin
54
+ bytes = bytes_array[offset, block_size]
55
+ result << key.send(type, bytes.pack("C*"))
56
+ offset = (i += 1) * block_size
57
+ end while input_length - offset > 0
58
+
59
+ result
60
+ end
61
+
62
+ def max_encrypt_block_size
63
+ modulus_byte_size - 11
64
+ end
65
+
66
+ def max_decrypt_block_size
67
+ modulus_byte_size
68
+ end
69
+
70
+ def modulus_byte_size
71
+ key.n.to_s(16).size / 2
72
+ end
73
+
74
+ def reset_key(key_file, passphrase=nil)
75
+ key_file = File.expand_path(key_file)
76
+ if key_file =~ /pfx/
77
+ @key = OpenSSL::PKCS12.new(IO.read(key_file), passphrase).key
78
+ else
79
+ @key = OpenSSL::X509::Certificate.new(IO.read(key_file)).public_key
80
+ end
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,48 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+ require 'securerandom'
4
+
5
+ module Ktct
6
+ class DigitalEnvelop
7
+ attr_accessor :aes, :algorithm, :key
8
+ def initialize(content)
9
+ _, @key = content.split('|')
10
+ end
11
+
12
+ def encrypt(content)
13
+ @aes = OpenSSL::Cipher::AES.new(128, :CBC)
14
+ @aes.encrypt
15
+ @aes.padding = 0
16
+ @aes.key = @key
17
+ @aes.iv = @key
18
+ (@aes.update(add_padding(Base64.encode64(content))) + @aes.final).unpack('H*').first
19
+ end
20
+
21
+ def decrypt(content)
22
+ @aes = OpenSSL::Cipher::AES.new(128, :CBC)
23
+ @aes.decrypt
24
+ @aes.padding = 0
25
+ @aes.key = @key
26
+ @aes.iv = @key
27
+ Base64.decode64((@aes.update([content].pack('H*')) + @aes.final).strip).force_encoding('UTF-8')
28
+ end
29
+
30
+ def add_padding(content)
31
+ if content.bytesize % 16 == 0
32
+ content
33
+ else
34
+ content + "\x00" * (16 - content.bytesize % 16)
35
+ end
36
+ end
37
+
38
+ def to_s
39
+ "01|#{@key}"
40
+ end
41
+
42
+ class << self
43
+ def get
44
+ new('01|%s' % SecureRandom.uuid.gsub(/-/, '')[0, 16])
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Ktct
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,47 @@
1
+ require 'thor'
2
+ require 'open-uri'
3
+
4
+ module Ktct
5
+ class Wangjinshe < ::Thor
6
+ include Ktct::Config
7
+ include Ktct::Cryptable
8
+
9
+ no_commands do
10
+ alias_method :original_decrypt, :decrypt
11
+ end
12
+
13
+ desc 'encrypt [DATA]', "Encrypt data using Wangjinshe's public key, DATA can also be read from STDIN"
14
+ def encrypt(data = nil)
15
+ reset_key(config['server_public_key_path'])
16
+ puts URI::encode_www_form_component(Base64.strict_encode64(public_encrypt(data || STDIN.read)))
17
+ end
18
+
19
+ desc 'decrypt [DATA]', "Decrypt data using client's private key, DATA can also be read from STDIN"
20
+ def decrypt(data = nil)
21
+ reset_key(config['client_private_key_path'])
22
+ puts private_decrypt(Base64.strict_decode64(data || STDIN.read))
23
+ end
24
+
25
+ desc 'sign [DATA]', "Sign data using client's private key, DATA can also be read from STDIN"
26
+ def sign(data = nil)
27
+ reset_key(config['client_private_key_path'])
28
+ puts URI::encode_www_form_component(Base64.strict_encode64(key.sign('sha1', data || STDIN.read)))
29
+ end
30
+
31
+ # desc 'verify <SIGNATURE> [DATA]', "Verify signature using Wangjinshe's public key, DATA can also be read from STDIN"
32
+ # def verify(signature, data = nil)
33
+ # reset_key(config['server_public_key_path'])
34
+ # puts key.verify('sha1', Base64.strict_decode64(signature), data || STDIN.read)
35
+ # end
36
+
37
+ no_commands do
38
+ def config
39
+ super['wjs']
40
+ end
41
+
42
+ def reset_key(key_file)
43
+ @key = OpenSSL::PKey::RSA.new(IO.read(File.expand_path(key_file)))
44
+ end
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ktct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Liu Xiang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.20.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.20.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 3.6.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.6.0
83
+ description: Utilities for crypto things
84
+ email:
85
+ - liuxiang921@gmail.com
86
+ executables:
87
+ - ktct
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - exe/ktct
103
+ - ktct.gemspec
104
+ - lib/ktct.rb
105
+ - lib/ktct/baofu.rb
106
+ - lib/ktct/cli.rb
107
+ - lib/ktct/config.rb
108
+ - lib/ktct/cryptable.rb
109
+ - lib/ktct/digital_envelop.rb
110
+ - lib/ktct/version.rb
111
+ - lib/ktct/wangjinshe.rb
112
+ homepage: https://github.com/lululau/ktct
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubygems_version: 3.0.3
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Utilities for crypto things
135
+ test_files: []