struggle 2.2.2 → 2.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0de663c31ad25e4b730c910a5bb3a1d75fe00ac5
4
- data.tar.gz: 1e85ed90acdac7bb95e1ed61eef1607999a24a3d
3
+ metadata.gz: 02424ee5858b52a0fb130dd9cf972d3482212d08
4
+ data.tar.gz: 1eddbe2e05520e2d9fe91408f04aa3021f5d5ab8
5
5
  SHA512:
6
- metadata.gz: 4611e93533123a3025394be48eaf1d07e29478f8b97cb92f70e2efc3b4355ae73a38560c1722a3bc9ae0f117c7eabf7ab7d66ead4b790a02da140c3702dba00a
7
- data.tar.gz: d5869ca27cd5feefad64df206681d0078a8d37b7e034e24ec8fccce7631c5577749aa3aecf525d8955a31481345ae369fb83a98123484d92ba39d16e419afefd
6
+ metadata.gz: 9a174385cfe42e9e2d34ef15f7ff2680ff30d6d6298013af8161e2cbfe90d91483f7a7318062aaf23bddaf99672216b2999cd62bfc422efc86d396292a3772b8
7
+ data.tar.gz: 512e145db165b6d2c25a0dc35566180132020a4b5e5ba8d275764a3c57c7445c5d734ee88ab3930079c6f4c57e6ce897a8d41999ed4fadab8dd4f75817b44191
data/lib/struggle/aes.rb CHANGED
@@ -1,20 +1,76 @@
1
- module Struggle
2
- class Aes
3
- require 'openssl'
4
- def self.encrypt(data, key ,iv)
5
- cipher = OpenSSL::Cipher::AES.new(256, :CBC)
6
- cipher.encrypt
7
- cipher.key = key
8
- cipher.iv = iv
9
- Base64.encode64(cipher.update(data) + cipher.final)
1
+ class Aes
2
+ # aes =
3
+ def initialize(key)
4
+ @@key = key
5
+ end
6
+
7
+ # 加密,key 秘钥, encrypted_string 需要加密的内容
8
+ def encrypt(encrypted_string)
9
+ aes = OpenSSL::Cipher::Cipher.new("AES-256-ECB").clone
10
+ aes.encrypt
11
+ aes.key = @@key
12
+ txt = aes.update(encrypted_string) << aes.final
13
+ txt.unpack('H*')[0].upcase
14
+ end
15
+
16
+ # 解密,key 秘钥, decrypted_string 需要解密的内容
17
+ def decrypt(decrypted_string)
18
+ aes = OpenSSL::Cipher::Cipher.new("AES-256-ECB").clone
19
+ aes.decrypt
20
+ aes.key = @@key
21
+ aes.update([decrypted_string].pack('H*')) << aes.final
22
+ end
23
+
24
+ # 文件内容加密并保存
25
+ def file_encrypt(filename)
26
+ if File.exist? filename
27
+ puts filename
28
+ content = File.read(filename)
29
+ unless content.blank?
30
+ File.write(filename, encrypt(Base64.encode64(content.force_encoding("UTF-8"))))
31
+ end
10
32
  end
33
+ end
11
34
 
12
- def self.decrypt(data, key ,iv)
13
- decipher = OpenSSL::Cipher::AES.new(256, :CBC)
14
- decipher.decrypt
15
- decipher.key = key
16
- decipher.iv = iv
17
- decipher.update(Base64.decode64(data)) + decipher.final
35
+ # 文件内容解密并保存
36
+ def file_decrypt(filename)
37
+ if File.exist? filename
38
+ puts filename
39
+ content = File.read(filename)
40
+ unless content.blank?
41
+ File.write(filename, Base64.decode64(decrypt(content)).force_encoding("UTF-8"))
42
+ end
18
43
  end
19
44
  end
20
- end
45
+
46
+ # 目录递归所有文件加密
47
+ def dir_encrypt(path)
48
+ get_file_list(path).each do |fn|
49
+ file_encrypt(fn)
50
+ end
51
+ end
52
+
53
+ # 目录递归所有文件解密
54
+ def dir_decrypt(path)
55
+ get_file_list(path).each do |fn|
56
+ file_decrypt(fn)
57
+ end
58
+ end
59
+
60
+ private
61
+ # 递归目录下所有文件
62
+ def get_file_list(path)
63
+ files = []
64
+ Dir.entries(path).each do |sub|
65
+ if sub != '.' && sub != '..'
66
+ if File.directory?("#{path}/#{sub}")
67
+ get_file_list("#{path}/#{sub}")
68
+ else
69
+ files << "#{path}/#{sub}"
70
+ end
71
+ end
72
+ end
73
+ files
74
+ end
75
+
76
+ end
@@ -0,0 +1,123 @@
1
+ require 'digest'
2
+ class Getui
3
+ class<<self
4
+ AppID = "XxESwIkfcwAKhgIcQ0iyl8"
5
+ AppKey = "9WY9yq0cxj5O7kPJT72om9"
6
+ AppSecret = "RkZ9pNxnXl7hHYG4NX1s19"
7
+ MasterSecret = "ie5ftISOpv97cgTBsiigaA"
8
+
9
+ # 签权,获取权限。返回bool值,成功签权返回true,否则返回false。 @@auth_token 全局token
10
+ def sign
11
+ timestamp = "#{Time.now.to_i}000"
12
+ sign = Digest::SHA256.hexdigest(AppKey+timestamp+MasterSecret)
13
+ # sign = Digest::SHA256.base64digest sign
14
+ command = <<EOF
15
+ curl -H "Content-Type: application/json" \\
16
+ https://restapi.getui.com/v1/#{AppID}/auth_sign \\
17
+ -XPOST -d '{ "sign":"#{sign}",
18
+ "timestamp":"#{timestamp}",
19
+ "appkey":"#{AppKey}"
20
+ }'
21
+ EOF
22
+ r = JSON.parse `#{command}`
23
+ r["result"] == "ok" ? @@auth_token = r["auth_token"] : nil
24
+ end
25
+
26
+ # 关闭签权,return bool,success true, error false
27
+ def close
28
+ command = <<EOF
29
+ curl -H "authtoken: #{@@auth_token}" \
30
+ https://restapi.getui.com/v1/#{AppID}/auth_close \
31
+ -XPOST
32
+ EOF
33
+ r = JSON.parse `#{command}`
34
+ r["result"] == "ok"
35
+ end
36
+
37
+ # 单推
38
+ # 参数 cid:client_id, title: 标题, text: 消息内容, is_offline是否离线默认否。
39
+ # 返回值 bool,success true, error false
40
+ def onePush(cid, title, text, is_offline=false)
41
+ if sign
42
+ requestid = Random.new.rand(1000000000000000000000..99999999999999999999999)
43
+ command = <<EOF
44
+ curl -H "Content-Type: application/json" \
45
+ -H "authtoken:#{@@auth_token}" \
46
+ https://restapi.getui.com/v1/#{AppID}/push_single \
47
+ -XPOST -d '{
48
+ "message": {
49
+ "appkey": "#{AppKey}",
50
+ "is_offline": #{is_offline},
51
+ "offline_expire_time":100000000,
52
+ "msgtype": "notification"
53
+ },
54
+ "notification": {
55
+ "style": {
56
+ "type": 0,
57
+ "text": "#{text}",
58
+ "title": "#{title}"
59
+ },
60
+ "transmission_type": true,
61
+ "transmission_content": "透传内容"
62
+ },
63
+ "cid": "#{cid}",
64
+ "requestid": "#{requestid}"
65
+ }'
66
+ EOF
67
+ r = JSON.parse `#{command}`
68
+ close
69
+ return r["result"] == "ok"
70
+ else
71
+ return false
72
+ end
73
+ end
74
+
75
+ # 多推
76
+ # 参数 clients: 客户信息数组格式[{cid: "xxx", title: "xxx", text: "xxx", is_offline: false}],
77
+ # 具体组成 cid:client_id, title: 标题, text: 消息内容, is_offline是否离线默认否。
78
+ # 返回值 bool,success true, error false
79
+ def manyPush(clients)
80
+ if sign
81
+ msg_list = []
82
+ clients.each do |c|
83
+ requestid = Random.new.rand(1000000000000000000000..99999999999999999999999)
84
+ msg_list << {
85
+ "message": {
86
+ "appkey": "#{AppKey}",
87
+ "is_offline": c[:is_offline] || false,
88
+ "offline_expire_time": 100000000,
89
+ "msgtype": "notification"
90
+ },
91
+ "notification": {
92
+ "style": {
93
+ "type": 0,
94
+ "text": "#{c[:text]}",
95
+ "title": "#{c[:title]}"
96
+ },
97
+ "transmission_type": true,
98
+ "transmission_content": "透传内容"
99
+ },
100
+ "cid": "#{c[:cid]}",
101
+ "requestid": "#{requestid}"
102
+ }
103
+ end
104
+
105
+ command = <<EOF
106
+ curl -H "Content-Type: application/json" \
107
+ -H "authtoken:#{@@auth_token}" \
108
+ https://restapi.getui.com/v1/#{AppID}/push_single_batch \
109
+ -XPOST -d '{
110
+ "msg_list": #{msg_list.to_json},
111
+ "need_detail":true
112
+ }'
113
+ EOF
114
+ r = JSON.parse `#{command}`
115
+ close
116
+ return r["result"] == "ok"
117
+ else
118
+ return false
119
+ end
120
+ end
121
+
122
+ end
123
+ end
data/lib/struggle/http.rb CHANGED
@@ -3,31 +3,33 @@ module Struggle
3
3
  require 'net/http'
4
4
  require 'net/https'
5
5
  require 'uri'
6
- def self.get(url, params=nil, header=nil)
7
- uri = URI.parse(url)
8
- http = Net::HTTP.new(uri.host, uri.port)
9
- http.use_ssl = true if uri.scheme == 'https'
10
- request = Net::HTTP::Get.new(uri.request_uri)
6
+
7
+ attr_reader :uri
8
+ attr_reader :request
9
+
10
+ def initialize(url)
11
+ @uri = URI.parse(url)
12
+ @http = Net::HTTP.new(@uri.host, @uri.port)
13
+ @http.use_ssl = true if @uri.scheme == 'https'
14
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
15
+ end
16
+
17
+ def get(params=nil, header=nil)
18
+ @request = Net::HTTP::Get.new(@uri.request_uri, header)
11
19
  if !params.blank?
12
- request.form_data = params
13
- end
14
- if !header.nil?
15
- request.initialize_http_header(header)
20
+ @request.form_data = params
16
21
  end
17
- http.request(request)
22
+ @http.request(@request)
18
23
  end
19
- def self.post(url, params=nil, header=nil)
20
- uri = URI.parse(url)
21
- http = Net::HTTP.new(uri.host, uri.port)
22
- http.use_ssl = true if uri.scheme == 'https'
23
- request = Net::HTTP::Post.new(uri.request_uri)
24
+
25
+ def post(params=nil, header=nil)
26
+ @request = Net::HTTP::Post.new(@uri.request_uri, header)
24
27
  if !params.blank?
25
- request.set_form_data(params)
26
- end
27
- if !header.nil?
28
- request.initialize_http_header(header)
28
+ # @request.set_form_data(params)
29
+ @request.body = params
29
30
  end
30
- http.request(request)
31
+ @http.request(@request)
31
32
  end
33
+
32
34
  end
33
35
  end
@@ -0,0 +1,43 @@
1
+ # = RSA工具,基于openssl,功能包括,秘钥生成,加密解密
2
+ class Rsa
3
+ # 实例化Rsa类,keyfile是秘钥文件地址(公钥或私钥)文件格式pem
4
+ def initialize(keyfile)
5
+ keystr = File.read(keyfile)
6
+ if keystr
7
+ @@rsa = OpenSSL::PKey::RSA.new(keystr)
8
+ end
9
+ end
10
+
11
+ # Rsa.generate("/Users/apple/object") => /Users/apple/object/private_key.pem ,public_key.pem
12
+ # 创建2048位秘钥文件
13
+ def self.generate(dir)
14
+ pk = OpenSSL::PKey::RSA.generate(2048)
15
+ File.write(dir+"/private_key.pem", pk.to_pem)
16
+ File.write(dir+"/public_key.pem", pk.public_key.to_pem)
17
+ end
18
+
19
+ # 私钥加密
20
+ def private_encrypt(value)
21
+ Base64.encode64(@@rsa.private_encrypt(value.force_encoding("UTF-8"))) unless value.blank?
22
+ end
23
+
24
+ # 私钥解密
25
+ def private_decrypt(value)
26
+ @@rsa.private_decrypt(Base64.decode64(value.force_encoding("UTF-8"))) unless value.blank?
27
+ end
28
+
29
+ # 公钥加密
30
+ def public_encrypt(value)
31
+ Base64.encode64(@@rsa.public_encrypt(value.force_encoding("UTF-8"))) unless value.blank?
32
+ end
33
+
34
+ # 公钥解密
35
+ def public_decrypt(value)
36
+ @@rsa.public_decrypt(Base64.decode64(value.force_encoding("UTF-8"))) unless value.blank?
37
+ end
38
+
39
+ # 签名
40
+ def sign(value)
41
+ @@rsa.sign("sha1", value)
42
+ end
43
+ end
data/lib/struggle/sms.rb CHANGED
@@ -9,4 +9,17 @@ module Struggle
9
9
  return response.body=="100"
10
10
  end
11
11
  end
12
+ # 云讯科技
13
+ class SmsYx
14
+ URL = "http://sandbox.ytx.net"
15
+ require 'net/https'
16
+ require 'uri'
17
+
18
+ def Sms.send(accountSID, authToken, version, appid, tel, templateId, content)
19
+ url = "#{URL}/#{version}/sid/#{accountSID}/sms/TemplateSMS.wx"
20
+ data = {"action":"templateSms","mobile":tel,"appid":appid, "templateId":"#{templateId}","datas": content}
21
+ response = Http.post(URL, data)
22
+ return response.body
23
+ end
24
+ end
12
25
  end
data/lib/struggle.rb CHANGED
@@ -14,6 +14,8 @@ require 'struggle/sms'
14
14
  require 'struggle/tmagick'
15
15
  require 'struggle/code'
16
16
  require 'struggle/aes'
17
+ require 'struggle/rsa'
18
+ require 'struggle/getui'
17
19
  require 'struggle/concerns/string_extend'
18
20
  require 'struggle/concerns/int_extend'
19
21
  require 'struggle/concerns/decimal_extend'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: struggle
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.2
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - lean
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: code,tfile,pager,http,logistic,pay,tmagick,class_extend,aes,ftp_tool,zip_tool;
55
+ description: code,tfile,pager,http,logistic,pay,tmagick,class_extend,aes,rsa,ftp_tool,zip_tool;
56
56
  email: 54850915@qq.com
57
57
  executables: []
58
58
  extensions: []
@@ -69,9 +69,11 @@ files:
69
69
  - lib/struggle/concerns/string_extend.rb
70
70
  - lib/struggle/font/font.ttf
71
71
  - lib/struggle/ftp_tool.rb
72
+ - lib/struggle/getui.rb
72
73
  - lib/struggle/http.rb
73
74
  - lib/struggle/logistic.rb
74
75
  - lib/struggle/pager.rb
76
+ - lib/struggle/rsa.rb
75
77
  - lib/struggle/sms.rb
76
78
  - lib/struggle/tfile.rb
77
79
  - lib/struggle/tmagick.rb