weixin_message_encryptor 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/weixin_message_encryptor.rb +101 -0
- data/lib/weixin_message_encryptor/version.rb +3 -0
- data/weixin_message_encryptor.gemspec +30 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 078898dc51a1a01e6f218666e45cb126ba076b54
|
4
|
+
data.tar.gz: af1e9443ae0b895c73c37e9f813d4f63cb547a3c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f6940aff753c0d4488bd1b4df4147e0ba9e1f5e84f249a4e064f2f638aff1735ad59192f8f6ab220fa0d0aacfef10ef73458635c607f64cbc121f2b04747738b
|
7
|
+
data.tar.gz: ba660926e5f548e659a7c74242a5fdedaf89ae116125df37f7cf415b3c392d9c267a3d76b5bf363eeb945d474621ad0be039c984c2a3efdfb5f51345388d7457
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 FENG Zhichao
|
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,39 @@
|
|
1
|
+
# WeixinMessageEncryptor
|
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/weixin_message_encryptor`. 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 'weixin_message_encryptor'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install weixin_message_encryptor
|
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. Then, run `rake spec` to run the tests. 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]/weixin_message_encryptor.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'weixin_message_encryptor'
|
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,101 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'openssl'
|
3
|
+
require 'securerandom'
|
4
|
+
|
5
|
+
require 'weixin_message_encryptor/version'
|
6
|
+
|
7
|
+
class WeixinMessageEncryptor
|
8
|
+
|
9
|
+
BLOCK_SIZE = 32
|
10
|
+
|
11
|
+
attr_reader :encoding_aes_key, :sign_token, :app_id
|
12
|
+
|
13
|
+
def initialize(encoding_aes_key: nil, sign_token: nil, app_id: nil)
|
14
|
+
@encoding_aes_key = encoding_aes_key
|
15
|
+
@sign_token = sign_token
|
16
|
+
@app_id = app_id
|
17
|
+
end
|
18
|
+
|
19
|
+
def encrypt(payload)
|
20
|
+
encrypted_payload = aes_encrypt(payload).delete("\n")
|
21
|
+
encrypted_message(encrypted_payload)
|
22
|
+
end
|
23
|
+
|
24
|
+
def decrypt(payload)
|
25
|
+
encrypted_payload = payload['echostr'] || payload['xml']['Encrypt']
|
26
|
+
sign = generate_signature encrypted_payload, payload['timestamp'], payload['nonce']
|
27
|
+
message, app_id = decrypt_payload encrypted_payload
|
28
|
+
return message, app_id, sign
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def aes_key
|
34
|
+
@aes_key ||= Base64.decode64 encoding_aes_key + '='
|
35
|
+
end
|
36
|
+
|
37
|
+
def encrypted_message(encrypted_payload)
|
38
|
+
timestamp = Time.now.to_i.to_s
|
39
|
+
nonce = SecureRandom.hex(8)
|
40
|
+
signature = generate_signature(encrypted_payload, timestamp, nonce)
|
41
|
+
<<XML
|
42
|
+
<xml>
|
43
|
+
<Encrypt><![CDATA[#{encrypted_payload}]]></Encrypt>
|
44
|
+
<MsgSignature><![CDATA[#{signature}]]></MsgSignature>
|
45
|
+
<TimeStamp>#{timestamp}</TimeStamp>
|
46
|
+
<Nonce><![CDATA[#{nonce}]]></Nonce>
|
47
|
+
</xml>
|
48
|
+
XML
|
49
|
+
end
|
50
|
+
|
51
|
+
def decrypt_payload(encrypted_payload)
|
52
|
+
text = Base64.decode64 encrypted_payload
|
53
|
+
text = handle_cipher(:decrypt, text)
|
54
|
+
result = pkcs7_decode(text)
|
55
|
+
content = result[16...result.length]
|
56
|
+
len_list = content[0...4].unpack('N')
|
57
|
+
xml_len = len_list[0]
|
58
|
+
payload = content[4...4 + xml_len]
|
59
|
+
app_id = content[xml_len+4...content.size]
|
60
|
+
return payload, app_id
|
61
|
+
end
|
62
|
+
|
63
|
+
def generate_signature(message, timestamp, nonce)
|
64
|
+
sorted_params = [message, sign_token, timestamp, nonce].sort.join
|
65
|
+
Digest::SHA1.hexdigest(sorted_params)
|
66
|
+
end
|
67
|
+
|
68
|
+
def aes_encrypt(text)
|
69
|
+
text = text.force_encoding('ASCII-8BIT')
|
70
|
+
random = SecureRandom.hex(8)
|
71
|
+
msg_len = [text.length].pack('N')
|
72
|
+
text = "#{random}#{msg_len}#{text}#{app_id}"
|
73
|
+
text = encode(text)
|
74
|
+
text = handle_cipher(:encrypt, text)
|
75
|
+
Base64.encode64(text)
|
76
|
+
end
|
77
|
+
|
78
|
+
def handle_cipher(action, text)
|
79
|
+
cipher = OpenSSL::Cipher.new('AES-256-CBC')
|
80
|
+
cipher.send(action)
|
81
|
+
cipher.padding = 0
|
82
|
+
cipher.key = aes_key
|
83
|
+
cipher.iv = aes_key[0...16]
|
84
|
+
cipher.update(text) + cipher.final
|
85
|
+
end
|
86
|
+
|
87
|
+
def pkcs7_decode(text)
|
88
|
+
pad = text[-1].ord
|
89
|
+
pad = 0 if pad < 1 || pad > BLOCK_SIZE
|
90
|
+
size = text.size - pad
|
91
|
+
text[0...size]
|
92
|
+
end
|
93
|
+
|
94
|
+
def encode(text)
|
95
|
+
amount_to_pad = BLOCK_SIZE - (text.length % BLOCK_SIZE)
|
96
|
+
amount_to_pad = BLOCK_SIZE if amount_to_pad.zero?
|
97
|
+
pad_chr = amount_to_pad.chr
|
98
|
+
"#{text}#{pad_chr * amount_to_pad}"
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'weixin_message_encryptor/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'weixin_message_encryptor'
|
9
|
+
spec.version = WeixinMessageEncryptor::VERSION
|
10
|
+
spec.authors = ['FENG Zhichao']
|
11
|
+
spec.email = ['flankerfc@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = %q{A simple tool to decrypt and encrypt Weixin messages.}
|
14
|
+
spec.description = %q{A simple tool to decrypt and encrypt Weixin messages.}
|
15
|
+
spec.homepage = 'https://github.com/flanker/weixin_message_encryptor'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
28
|
+
spec.add_development_dependency 'timecop', '~> 0.9.1'
|
29
|
+
spec.add_development_dependency 'byebug'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weixin_message_encryptor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- FENG Zhichao
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-02 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.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: timecop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.9.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.9.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A simple tool to decrypt and encrypt Weixin messages.
|
84
|
+
email:
|
85
|
+
- flankerfc@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/weixin_message_encryptor.rb
|
100
|
+
- lib/weixin_message_encryptor/version.rb
|
101
|
+
- weixin_message_encryptor.gemspec
|
102
|
+
homepage: https://github.com/flanker/weixin_message_encryptor
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.4.8
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: A simple tool to decrypt and encrypt Weixin messages.
|
126
|
+
test_files: []
|