node-easywechat 2.6.10 → 2.8.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.
Files changed (66) hide show
  1. package/README.md +26 -3
  2. package/dist/BaseService/ContentSecurity/ContentSecurityClient.d.ts +5 -3
  3. package/dist/BaseService/ContentSecurity/ContentSecurityClient.js +2 -2
  4. package/dist/BaseService/Jssdk/JssdkClient.d.ts +15 -15
  5. package/dist/BaseService/Jssdk/JssdkClient.js +15 -15
  6. package/dist/BaseService/Media/MediaClient.d.ts +22 -20
  7. package/dist/BaseService/Media/MediaClient.js +14 -14
  8. package/dist/BaseService/Url/UrlClient.d.ts +1 -1
  9. package/dist/Core/AES.d.ts +46 -0
  10. package/dist/Core/AES.js +95 -0
  11. package/dist/Core/Encryptor.d.ts +2 -14
  12. package/dist/Core/Encryptor.js +9 -39
  13. package/dist/Core/Http/Request.js +2 -6
  14. package/dist/Core/Messages/Message.js +2 -16
  15. package/dist/Core/PKCS.d.ts +15 -0
  16. package/dist/Core/PKCS.js +44 -0
  17. package/dist/Core/RSA.d.ts +59 -0
  18. package/dist/Core/RSA.js +99 -0
  19. package/dist/Core/ServerGuard.d.ts +1 -1
  20. package/dist/Core/ServerGuard.js +5 -33
  21. package/dist/Core/Types.d.ts +24 -4
  22. package/dist/Core/Utils.d.ts +8 -2
  23. package/dist/Core/Utils.js +61 -20
  24. package/dist/MicroMerchant/Application.d.ts +27 -0
  25. package/dist/MicroMerchant/Application.js +105 -0
  26. package/dist/MicroMerchant/Base/MicroMerchantBase.d.ts +7 -0
  27. package/dist/MicroMerchant/Base/MicroMerchantBase.js +52 -0
  28. package/dist/MicroMerchant/Certficates/CertficatesClient.d.ts +5 -0
  29. package/dist/MicroMerchant/Certficates/CertficatesClient.js +52 -0
  30. package/dist/MicroMerchant/Core/BaseClient.d.ts +9 -0
  31. package/dist/MicroMerchant/Core/BaseClient.js +106 -0
  32. package/dist/MicroMerchant/Media/MediaClient.d.ts +6 -0
  33. package/dist/MicroMerchant/Media/MediaClient.js +33 -0
  34. package/dist/MicroMerchant/MerchantConfig/MerchantConfigClient.d.ts +8 -0
  35. package/dist/MicroMerchant/MerchantConfig/MerchantConfigClient.js +49 -0
  36. package/dist/MicroMerchant/Meterial/MeterialClient.d.ts +5 -0
  37. package/dist/MicroMerchant/Meterial/MeterialClient.js +30 -0
  38. package/dist/MicroMerchant/Withdraw/WithdrawClient.d.ts +5 -0
  39. package/dist/MicroMerchant/Withdraw/WithdrawClient.js +28 -0
  40. package/dist/MiniProgram/Application.d.ts +4 -0
  41. package/dist/MiniProgram/Application.js +10 -0
  42. package/dist/MiniProgram/Encryptor.js +2 -6
  43. package/dist/MiniProgram/PhoneNumber/PhoneNumberClient.d.ts +10 -0
  44. package/dist/MiniProgram/PhoneNumber/PhoneNumberClient.js +20 -0
  45. package/dist/MiniProgram/Union/UnionClient.d.ts +3 -0
  46. package/dist/MiniProgram/Union/UnionClient.js +9 -0
  47. package/dist/MiniProgram/UrlLink/UrlLinkClient.d.ts +4 -0
  48. package/dist/MiniProgram/UrlLink/UrlLinkClient.js +12 -0
  49. package/dist/Payment/Bill/BillClient.js +1 -1
  50. package/dist/Payment/Core/BaseClient.d.ts +0 -1
  51. package/dist/Payment/Core/BaseClient.js +2 -20
  52. package/dist/Payment/Jssdk/JssdkClient.d.ts +12 -0
  53. package/dist/Payment/Jssdk/JssdkClient.js +32 -0
  54. package/dist/Payment/Notify/Handler.js +3 -11
  55. package/dist/Payment/Notify/Refunded.d.ts +0 -1
  56. package/dist/Payment/Notify/Refunded.js +1 -11
  57. package/dist/Payment/Transfer/TransferClient.js +4 -3
  58. package/dist/Work/ExternalContact/Client.d.ts +146 -3
  59. package/dist/Work/ExternalContact/Client.js +156 -6
  60. package/dist/Work/ExternalContact/MessageClient.d.ts +45 -0
  61. package/dist/Work/ExternalContact/MessageClient.js +86 -0
  62. package/dist/index.d.ts +6 -0
  63. package/dist/index.js +2 -0
  64. package/node-easywechat.file_cache.easywechat.kernel.access_token.8a9684b45da7de42a06a70ba01a7005a.cache +1 -1
  65. package/node-easywechat.file_cache.easywechat.kernel.access_token.c3d06edaf326aad75d32f31348df787a.cache +1 -1
  66. package/package.json +4 -5
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  ## EasyWechat for Node.js
3
3
 
4
- [![Build Status](https://www.travis-ci.com/hpyer/node-easywechat.svg?branch=develop)](https://www.travis-ci.com/hpyer/node-easywechat) [![npm](https://img.shields.io/npm/v/node-easywechat.svg)](https://www.npmjs.com/package/node-easywechat) [![License](https://img.shields.io/npm/l/node-easywechat.svg)](LICENSE)
4
+ [![Build Status](https://app.travis-ci.com/hpyer/node-easywechat.svg?branch=develop)](https://app.travis-ci.com/hpyer/node-easywechat) [![npm](https://img.shields.io/npm/v/node-easywechat.svg)](https://www.npmjs.com/package/node-easywechat) [![License](https://img.shields.io/npm/l/node-easywechat.svg)](LICENSE)
5
5
 
6
6
  **注:2.x分支针对 EasyWechat 的 5.x版本(由于4.x与5.x的结构基本一致,就不升级大版本了)。若您需要 EasyWechat 的 3.x版本,请切换到 [1.x](https://github.com/hpyer/node-easywechat/tree/1.x) 分支。**
7
7
 
@@ -16,7 +16,7 @@
16
16
 
17
17
  ### 使用说明
18
18
 
19
- 绝大部分API都可以根据 [EasyWechat 的文档](https://www.easywechat.com/docs) 来使用。小部分(如获取请求相关数据、返回响应数据、支付证书等)的操作,由于语言环境的不同,会有不同处理。具体可以查看 [node-easywechat-demo](https://github.com/hpyer/node-easywechat-demo/) 以及下方的[自定义模块说明](#自定义模块模块替换使用方法) 。如果仍有疑问,请提issue,谢谢~
19
+ 绝大部分API都可以根据 [EasyWechat 的文档](https://www.easywechat.com/5.x/) 来使用。小部分(如获取请求相关数据、返回响应数据、支付证书等)的操作,由于语言环境的不同,会有不同处理。具体可以查看 [node-easywechat-demo](https://github.com/hpyer/node-easywechat-demo/) 以及下方的[自定义模块说明](#自定义模块模块替换使用方法) 。如果仍有疑问,请提issue,谢谢~
20
20
 
21
21
  更多信息详见:[API文档](docs/README.md)
22
22
 
@@ -46,6 +46,11 @@ let openPlatform = new EasyWechat.Factory.Work({
46
46
  // ...
47
47
  });
48
48
 
49
+ // 小微商户
50
+ let microMerchant = new EasyWechat.Factory.MicroMerchant({
51
+ // ...
52
+ });
53
+
49
54
  // 通用方式
50
55
  let app = EasyWechat.Factory.getInstance('OficialAccount', {
51
56
  // ...
@@ -124,6 +129,24 @@ let app = EasyWechat.Factory.getInstance('OficialAccount', {
124
129
  }
125
130
  ```
126
131
 
132
+ ``` js
133
+ // 小微商户配置
134
+ {
135
+ // 商户号
136
+ mch_id: 'your-mch-id',
137
+ // 签名密钥
138
+ key: 'key-for-signature',
139
+ // V3版本签名密钥
140
+ apiv3_key: 'apiv3_key-for-signature',
141
+ // 证书地址,Node.js 只需要 .pfx 证书文件即可
142
+ cert_path: 'path/to/your/cert.pfx', // XXX: 绝对路径!!!!
143
+
144
+ // 以下两项配置在获取证书接口时可为空,在调用入驻接口前请先调用获取证书接口获取以下两项配置,如果获取过证书可以直接在这里配置,也可参照本文档获取平台证书章节中示例
145
+ // serial_no: '获取证书接口获取到的平台证书序列号',
146
+ // certificate: '获取证书接口获取到的证书内容'
147
+ }
148
+ ```
149
+
127
150
  ### 模块支持情况
128
151
 
129
152
  - [x] 公众号模块
@@ -132,7 +155,7 @@ let app = EasyWechat.Factory.getInstance('OficialAccount', {
132
155
  - [x] 开放平台
133
156
  - [x] 企业微信
134
157
  - [ ] 企业微信开放平台
135
- - [ ] 小微商户
158
+ - [x] 小微商户
136
159
  - [x] 自定义
137
160
 
138
161
  ### 自定义模块(模块替换)使用方法
@@ -1,14 +1,16 @@
1
+ /// <reference types="node" />
1
2
  import BaseClient from '../../Core/BaseClient';
3
+ import Stream from 'stream';
2
4
  export default class ContentSecurityClient extends BaseClient {
3
5
  protected baseUrl: string;
4
6
  /**
5
7
  * 校验一段文本是否含有违法内容
6
- * @param {string} text 待校验文本
8
+ * @param text 待校验文本
7
9
  */
8
10
  checkText(text: string): Promise<any>;
9
11
  /**
10
12
  * 校验一张图片是否含有敏感信息
11
- * @param {string/ReadableStream} file 文件路径或可读stream
13
+ * @param file 文件路径或可读stream
12
14
  */
13
- checkImage(file: any): Promise<any>;
15
+ checkImage(file: string | Stream.Readable): Promise<any>;
14
16
  }
@@ -20,7 +20,7 @@ class ContentSecurityClient extends BaseClient_1.default {
20
20
  }
21
21
  /**
22
22
  * 校验一段文本是否含有违法内容
23
- * @param {string} text 待校验文本
23
+ * @param text 待校验文本
24
24
  */
25
25
  checkText(text) {
26
26
  return __awaiter(this, void 0, void 0, function* () {
@@ -31,7 +31,7 @@ class ContentSecurityClient extends BaseClient_1.default {
31
31
  }
32
32
  /**
33
33
  * 校验一张图片是否含有敏感信息
34
- * @param {string/ReadableStream} file 文件路径或可读stream
34
+ * @param file 文件路径或可读stream
35
35
  */
36
36
  checkImage(file) {
37
37
  return __awaiter(this, void 0, void 0, function* () {
@@ -10,35 +10,35 @@ export default class Client extends BaseClient {
10
10
  getTicket(refresh?: boolean, type?: string): Promise<string>;
11
11
  /**
12
12
  * 获取JSSDK的配置
13
- * @param {Array<string>} jsApiList API列表
14
- * @param {Boolean} debug 是否调试模式,默认:false
15
- * @param {Boolean} beta 是否测试模式,默认:false
16
- * @param {Boolean} json true时返回JSON字符串,默认:true
17
- * @param {Array<string>} openTagList 开放标签列表,默认:[]
18
- * @param {string} url 请求URL,默认:当前URL
13
+ * @param jsApiList API列表
14
+ * @param debug 是否调试模式,默认:false
15
+ * @param beta 是否测试模式,默认:false
16
+ * @param json true时返回JSON字符串,默认:true
17
+ * @param openTagList 开放标签列表,默认:[]
18
+ * @param url 请求URL,默认:当前URL
19
19
  */
20
20
  buildConfig(jsApiList: Array<string>, debug?: Boolean, beta?: Boolean, json?: Boolean, openTagList?: Array<string>, url?: string): Promise<any>;
21
21
  /**
22
22
  * 获取JSSDK的配置对象
23
- * @param {Array<string>} jsApiList API列表
24
- * @param {Boolean} debug 是否调试模式,默认:false
25
- * @param {Boolean} beta 是否测试模式,默认:false
26
- * @param {Array<string>} openTagList 开放标签列表,默认:[]
27
- * @param {string} url 请求URL,默认:当前URL
23
+ * @param jsApiList API列表
24
+ * @param debug 是否调试模式,默认:false
25
+ * @param beta 是否测试模式,默认:false
26
+ * @param openTagList 开放标签列表,默认:[]
27
+ * @param url 请求URL,默认:当前URL
28
28
  */
29
29
  getConfigArray(jsApiList: Array<string>, debug?: Boolean, beta?: Boolean, openTagList?: Array<string>, url?: string): Promise<any>;
30
30
  /**
31
31
  * 获取签名配置
32
- * @param {string} url 完整的URL地址
33
- * @param {string} nonce 随机字符串,默认:随机10位
34
- * @param {string} timestamp 时间戳,默认:当前时间
32
+ * @param url 完整的URL地址
33
+ * @param nonce 随机字符串,默认:随机10位
34
+ * @param timestamp 时间戳,默认:当前时间
35
35
  */
36
36
  configSignature(url?: string, nonce?: string, timestamp?: string): Promise<object>;
37
37
  protected getTicketSignature(ticket: string, nonce: string, timestamp: string, url: string): string;
38
38
  protected dictionaryOrderSignature(args: Array<string>): string;
39
39
  /**
40
40
  * 设置当前URL
41
- * @param {string} url 完整的URL地址
41
+ * @param url 完整的URL地址
42
42
  */
43
43
  setUrl(url: string): this;
44
44
  /**
@@ -46,12 +46,12 @@ class Client extends BaseClient_1.default {
46
46
  }
47
47
  /**
48
48
  * 获取JSSDK的配置
49
- * @param {Array<string>} jsApiList API列表
50
- * @param {Boolean} debug 是否调试模式,默认:false
51
- * @param {Boolean} beta 是否测试模式,默认:false
52
- * @param {Boolean} json true时返回JSON字符串,默认:true
53
- * @param {Array<string>} openTagList 开放标签列表,默认:[]
54
- * @param {string} url 请求URL,默认:当前URL
49
+ * @param jsApiList API列表
50
+ * @param debug 是否调试模式,默认:false
51
+ * @param beta 是否测试模式,默认:false
52
+ * @param json true时返回JSON字符串,默认:true
53
+ * @param openTagList 开放标签列表,默认:[]
54
+ * @param url 请求URL,默认:当前URL
55
55
  */
56
56
  buildConfig(jsApiList, debug = false, beta = false, json = true, openTagList = [], url = '') {
57
57
  return __awaiter(this, void 0, void 0, function* () {
@@ -63,20 +63,20 @@ class Client extends BaseClient_1.default {
63
63
  }
64
64
  /**
65
65
  * 获取JSSDK的配置对象
66
- * @param {Array<string>} jsApiList API列表
67
- * @param {Boolean} debug 是否调试模式,默认:false
68
- * @param {Boolean} beta 是否测试模式,默认:false
69
- * @param {Array<string>} openTagList 开放标签列表,默认:[]
70
- * @param {string} url 请求URL,默认:当前URL
66
+ * @param jsApiList API列表
67
+ * @param debug 是否调试模式,默认:false
68
+ * @param beta 是否测试模式,默认:false
69
+ * @param openTagList 开放标签列表,默认:[]
70
+ * @param url 请求URL,默认:当前URL
71
71
  */
72
72
  getConfigArray(jsApiList, debug = false, beta = false, openTagList = [], url = '') {
73
73
  return this.buildConfig(jsApiList, debug, beta, false, openTagList, url);
74
74
  }
75
75
  /**
76
76
  * 获取签名配置
77
- * @param {string} url 完整的URL地址
78
- * @param {string} nonce 随机字符串,默认:随机10位
79
- * @param {string} timestamp 时间戳,默认:当前时间
77
+ * @param url 完整的URL地址
78
+ * @param nonce 随机字符串,默认:随机10位
79
+ * @param timestamp 时间戳,默认:当前时间
80
80
  */
81
81
  configSignature(url = '', nonce = '', timestamp = '') {
82
82
  return __awaiter(this, void 0, void 0, function* () {
@@ -106,7 +106,7 @@ class Client extends BaseClient_1.default {
106
106
  }
107
107
  /**
108
108
  * 设置当前URL
109
- * @param {string} url 完整的URL地址
109
+ * @param url 完整的URL地址
110
110
  */
111
111
  setUrl(url) {
112
112
  this.url = url;
@@ -1,55 +1,57 @@
1
+ /// <reference types="node" />
1
2
  import BaseClient from '../../Core/BaseClient';
3
+ import Stream from 'stream';
2
4
  export default class MediaClient extends BaseClient {
3
5
  protected baseUrl: string;
4
6
  protected allowTypes: Array<string>;
5
7
  /**
6
8
  * 上传图片到临时素材
7
- * @param {string/ReadableStream} file 文件路径或可读stream
9
+ * @param file 文件路径或可读stream
8
10
  */
9
- uploadImage(file: any): Promise<any>;
11
+ uploadImage(file: string | Stream.Readable): Promise<any>;
10
12
  /**
11
13
  * 上传视频到临时素材
12
- * @param {string/ReadableStream} file 文件路径或可读stream
14
+ * @param file 文件路径或可读stream
13
15
  */
14
- uploadVideo(file: any): Promise<any>;
16
+ uploadVideo(file: string | Stream.Readable): Promise<any>;
15
17
  /**
16
18
  * 上传语音到临时素材
17
- * @param {string/ReadableStream} file 文件路径或可读stream
19
+ * @param file 文件路径或可读stream
18
20
  */
19
- uploadVoice(file: any): Promise<any>;
21
+ uploadVoice(file: string | Stream.Readable): Promise<any>;
20
22
  /**
21
23
  * 上传缩略图到临时素材
22
- * @param {string/ReadableStream} file 文件路径或可读stream
24
+ * @param file 文件路径或可读stream
23
25
  */
24
- uploadThumb(file: any): Promise<any>;
26
+ uploadThumb(file: string | Stream.Readable): Promise<any>;
25
27
  /**
26
28
  * 上传到临时素材
27
- * @param {string} type 类型,可选值:image,video,voice,thumb
28
- * @param {string/ReadableStream} file 文件路径或可读stream
29
+ * @param type 类型,可选值:image,video,voice,thumb
30
+ * @param file 文件路径或可读stream
29
31
  */
30
- upload(type: string, file: any): Promise<any>;
32
+ upload(type: string, file: string | Stream.Readable): Promise<any>;
31
33
  /**
32
34
  * 上传群发视频
33
- * @param {string/ReadableStream} file 文件路径或可读stream
34
- * @param {string} title 标题
35
- * @param {string} description 描述
35
+ * @param file 文件路径或可读stream
36
+ * @param title 标题
37
+ * @param description 描述
36
38
  */
37
- uploadVideoForBroadcasting(file: any, title: string, description: string): Promise<any>;
39
+ uploadVideoForBroadcasting(file: string | Stream.Readable, title: string, description: string): Promise<any>;
38
40
  /**
39
41
  * 创建群发消息
40
- * @param {string} media_id 上传视频得到 media_id
41
- * @param {string} title 标题
42
- * @param {string} description 描述
42
+ * @param media_id 上传视频得到 media_id
43
+ * @param title 标题
44
+ * @param description 描述
43
45
  */
44
46
  createVideoForBroadcasting(media_id: string, title: string, description: string): Promise<any>;
45
47
  /**
46
48
  * 获取临时素材内容
47
- * @param {string} media_id 上传视频得到 media_id
49
+ * @param media_id 上传视频得到 media_id
48
50
  */
49
51
  get(media_id: string): Promise<any>;
50
52
  /**
51
53
  * 获取 JSSDK 上传的高清语音
52
- * @param {string} media_id 上传视频得到 media_id
54
+ * @param media_id 上传视频得到 media_id
53
55
  */
54
56
  getJssdkMedia(media_id: string): Promise<any>;
55
57
  }
@@ -23,36 +23,36 @@ class MediaClient extends BaseClient_1.default {
23
23
  }
24
24
  /**
25
25
  * 上传图片到临时素材
26
- * @param {string/ReadableStream} file 文件路径或可读stream
26
+ * @param file 文件路径或可读stream
27
27
  */
28
28
  uploadImage(file) {
29
29
  return this.upload('image', file);
30
30
  }
31
31
  /**
32
32
  * 上传视频到临时素材
33
- * @param {string/ReadableStream} file 文件路径或可读stream
33
+ * @param file 文件路径或可读stream
34
34
  */
35
35
  uploadVideo(file) {
36
36
  return this.upload('video', file);
37
37
  }
38
38
  /**
39
39
  * 上传语音到临时素材
40
- * @param {string/ReadableStream} file 文件路径或可读stream
40
+ * @param file 文件路径或可读stream
41
41
  */
42
42
  uploadVoice(file) {
43
43
  return this.upload('voice', file);
44
44
  }
45
45
  /**
46
46
  * 上传缩略图到临时素材
47
- * @param {string/ReadableStream} file 文件路径或可读stream
47
+ * @param file 文件路径或可读stream
48
48
  */
49
49
  uploadThumb(file) {
50
50
  return this.upload('thumb', file);
51
51
  }
52
52
  /**
53
53
  * 上传到临时素材
54
- * @param {string} type 类型,可选值:image,video,voice,thumb
55
- * @param {string/ReadableStream} file 文件路径或可读stream
54
+ * @param type 类型,可选值:image,video,voice,thumb
55
+ * @param file 文件路径或可读stream
56
56
  */
57
57
  upload(type, file) {
58
58
  if (!file) {
@@ -69,9 +69,9 @@ class MediaClient extends BaseClient_1.default {
69
69
  }
70
70
  /**
71
71
  * 上传群发视频
72
- * @param {string/ReadableStream} file 文件路径或可读stream
73
- * @param {string} title 标题
74
- * @param {string} description 描述
72
+ * @param file 文件路径或可读stream
73
+ * @param title 标题
74
+ * @param description 描述
75
75
  */
76
76
  uploadVideoForBroadcasting(file, title, description) {
77
77
  return __awaiter(this, void 0, void 0, function* () {
@@ -84,9 +84,9 @@ class MediaClient extends BaseClient_1.default {
84
84
  }
85
85
  /**
86
86
  * 创建群发消息
87
- * @param {string} media_id 上传视频得到 media_id
88
- * @param {string} title 标题
89
- * @param {string} description 描述
87
+ * @param media_id 上传视频得到 media_id
88
+ * @param title 标题
89
+ * @param description 描述
90
90
  */
91
91
  createVideoForBroadcasting(media_id, title, description) {
92
92
  return this.httpPostJson('media/uploadvideo', {
@@ -97,7 +97,7 @@ class MediaClient extends BaseClient_1.default {
97
97
  }
98
98
  /**
99
99
  * 获取临时素材内容
100
- * @param {string} media_id 上传视频得到 media_id
100
+ * @param media_id 上传视频得到 media_id
101
101
  */
102
102
  get(media_id) {
103
103
  return __awaiter(this, void 0, void 0, function* () {
@@ -123,7 +123,7 @@ class MediaClient extends BaseClient_1.default {
123
123
  }
124
124
  /**
125
125
  * 获取 JSSDK 上传的高清语音
126
- * @param {string} media_id 上传视频得到 media_id
126
+ * @param media_id 上传视频得到 media_id
127
127
  */
128
128
  getJssdkMedia(media_id) {
129
129
  return __awaiter(this, void 0, void 0, function* () {
@@ -5,5 +5,5 @@ export default class UrlClient extends BaseClient {
5
5
  * 获取短网址
6
6
  * @param long_url 原始URL地址
7
7
  */
8
- shorten(long_url: any): Promise<any>;
8
+ shorten(long_url: string): Promise<any>;
9
9
  }
@@ -0,0 +1,46 @@
1
+ /// <reference types="node" />
2
+ import { CipherGCMTypes, BinaryLike, CipherKey } from 'crypto';
3
+ export declare class AES {
4
+ /**
5
+ * 加密
6
+ * @static
7
+ * @param plaintext 待加密文本
8
+ * @param key 密钥
9
+ * @param iv 向量
10
+ * @param autoPadding 自动补位,默认:null,表示不设置
11
+ * @param method 加密方式,默认:'aes-256-cbc'
12
+ */
13
+ static encrypt(plaintext: BinaryLike, key: CipherKey, iv?: BinaryLike, autoPadding?: boolean, method?: string): Buffer;
14
+ /**
15
+ * 解密
16
+ * @static
17
+ * @param ciphertext 待解密文本,Base64 编码字符串
18
+ * @param key 密钥
19
+ * @param iv 向量
20
+ * @param autoPadding 自动补位,默认:null,表示不设置
21
+ * @param method 加密方式,默认:aes-256-cbc
22
+ */
23
+ static decrypt(ciphertext: NodeJS.ArrayBufferView, key: BinaryLike, iv?: BinaryLike, autoPadding?: boolean, method?: string): Buffer;
24
+ }
25
+ export declare class AES_GCM {
26
+ /**
27
+ * GCM模式加密
28
+ * @static
29
+ * @param plaintext 待加密文本
30
+ * @param key 密钥
31
+ * @param iv 向量
32
+ * @param aad aad
33
+ * @param method 加密方式,默认:aes-256-gcm
34
+ */
35
+ static encrypt(plaintext: BinaryLike, key: CipherKey, iv?: BinaryLike, aad?: string, method?: CipherGCMTypes): Buffer;
36
+ /**
37
+ * GCM模式解密
38
+ * @static
39
+ * @param ciphertext 待解密文本,Base64 编码字符串
40
+ * @param key 密钥
41
+ * @param iv 向量
42
+ * @param aad aad
43
+ * @param method 加密方式,默认:aes-256-gcm
44
+ */
45
+ static decrypt(ciphertext: NodeJS.ArrayBufferView, key: BinaryLike, iv?: BinaryLike, aad?: string, method?: CipherGCMTypes): Buffer;
46
+ }
@@ -0,0 +1,95 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AES_GCM = exports.AES = void 0;
4
+ const crypto_1 = require("crypto");
5
+ class AES {
6
+ /**
7
+ * 加密
8
+ * @static
9
+ * @param plaintext 待加密文本
10
+ * @param key 密钥
11
+ * @param iv 向量
12
+ * @param autoPadding 自动补位,默认:null,表示不设置
13
+ * @param method 加密方式,默认:'aes-256-cbc'
14
+ */
15
+ static encrypt(plaintext, key, iv = '', autoPadding = null, method = 'aes-256-cbc') {
16
+ let cipher = crypto_1.createCipheriv(method, key, iv);
17
+ if (typeof autoPadding === 'boolean') {
18
+ cipher.setAutoPadding(autoPadding);
19
+ }
20
+ return Buffer.concat([
21
+ cipher.update(plaintext),
22
+ cipher.final(),
23
+ ]);
24
+ }
25
+ /**
26
+ * 解密
27
+ * @static
28
+ * @param ciphertext 待解密文本,Base64 编码字符串
29
+ * @param key 密钥
30
+ * @param iv 向量
31
+ * @param autoPadding 自动补位,默认:null,表示不设置
32
+ * @param method 加密方式,默认:aes-256-cbc
33
+ */
34
+ static decrypt(ciphertext, key, iv = '', autoPadding = null, method = 'aes-256-cbc') {
35
+ let decipher = crypto_1.createDecipheriv(method, key, iv);
36
+ if (typeof autoPadding === 'boolean') {
37
+ decipher.setAutoPadding(autoPadding);
38
+ }
39
+ if (typeof ciphertext === 'string') {
40
+ ciphertext = Buffer.from(ciphertext, 'base64');
41
+ }
42
+ let deciphered = Buffer.concat([
43
+ decipher.update(ciphertext),
44
+ decipher.final(),
45
+ ]);
46
+ return deciphered;
47
+ }
48
+ }
49
+ exports.AES = AES;
50
+ class AES_GCM {
51
+ /**
52
+ * GCM模式加密
53
+ * @static
54
+ * @param plaintext 待加密文本
55
+ * @param key 密钥
56
+ * @param iv 向量
57
+ * @param aad aad
58
+ * @param method 加密方式,默认:aes-256-gcm
59
+ */
60
+ static encrypt(plaintext, key, iv = '', aad = '', method = 'aes-256-gcm') {
61
+ let cipher = crypto_1.createCipheriv(method, key, iv).setAAD(Buffer.from(aad));
62
+ return Buffer.concat([
63
+ cipher.update(plaintext),
64
+ cipher.final(),
65
+ cipher.getAuthTag(),
66
+ ]);
67
+ }
68
+ /**
69
+ * GCM模式解密
70
+ * @static
71
+ * @param ciphertext 待解密文本,Base64 编码字符串
72
+ * @param key 密钥
73
+ * @param iv 向量
74
+ * @param aad aad
75
+ * @param method 加密方式,默认:aes-256-gcm
76
+ */
77
+ static decrypt(ciphertext, key, iv = '', aad = '', method = 'aes-256-gcm') {
78
+ let buf = null;
79
+ if (typeof ciphertext === 'string') {
80
+ buf = Buffer.from(ciphertext, 'base64');
81
+ }
82
+ else {
83
+ buf = Buffer.from(ciphertext);
84
+ }
85
+ let tag = buf.slice(-16);
86
+ let payload = buf.slice(0, -16);
87
+ let decipher = crypto_1.createDecipheriv(method, key, iv).setAuthTag(tag).setAAD(Buffer.from(aad));
88
+ return Buffer.concat([
89
+ decipher.update(payload),
90
+ decipher.final(),
91
+ ]);
92
+ }
93
+ }
94
+ exports.AES_GCM = AES_GCM;
95
+ ;
@@ -7,18 +7,6 @@ export default class Encryptor {
7
7
  constructor(appId: string, token: string, aesKey: string);
8
8
  getToken(): string;
9
9
  signature(...args: any[]): string;
10
- encrypt(text: any, nonce?: any, timestamp?: any): object;
11
- decrypt(text: any, msgSignature: any, nonce: any, timestamp: any): string;
12
- /**
13
- * 删除解密后明文的补位字符
14
- * @param {Buffer} text 解密后的明文
15
- * @return {Buffer}
16
- */
17
- pkcs7Unpad(text: Buffer): Buffer;
18
- /**
19
- * 对需要加密的明文进行填充补位
20
- * @param {Buffer} text 需要进行填充补位操作的明文
21
- * @return {Buffer}
22
- */
23
- pkcs7Pad(text: Buffer, blockSize: number): Buffer;
10
+ encrypt(text: string, nonce?: string, timestamp?: number): string;
11
+ decrypt(text: string, msgSignature: string, nonce: string, timestamp: number): string;
24
12
  }
@@ -5,6 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const crypto_1 = __importDefault(require("crypto"));
7
7
  const Utils_1 = require("./Utils");
8
+ const AES_1 = require("./AES");
9
+ const PKCS_1 = require("./PKCS");
8
10
  class Encryptor {
9
11
  constructor(appId, token, aesKey) {
10
12
  this.appId = null;
@@ -20,9 +22,7 @@ class Encryptor {
20
22
  }
21
23
  signature(...args) {
22
24
  args.sort();
23
- let shasum = crypto_1.default.createHash('sha1');
24
- shasum.update(args.join(''));
25
- return shasum.digest('hex');
25
+ return Utils_1.createHash(args.join(''), 'sha1');
26
26
  }
27
27
  encrypt(text, nonce = null, timestamp = null) {
28
28
  let encrypted = '';
@@ -32,10 +32,8 @@ class Encryptor {
32
32
  let msg = Buffer.from(text);
33
33
  let msgLength = Buffer.alloc(4);
34
34
  msgLength.writeUInt32BE(msg.length, 0);
35
- let encoded = this.pkcs7Pad(Buffer.concat([randomString, msgLength, msg, Buffer.from(this.appId)]), this.blockSize);
36
- let cipher = crypto_1.default.createCipheriv('aes-256-cbc', this.aesKey, this.aesKey.slice(0, 16));
37
- cipher.setAutoPadding(false);
38
- encrypted = Buffer.concat([cipher.update(encoded), cipher.final()]).toString('base64');
35
+ let encoded = PKCS_1.PKCS7.pad(Buffer.concat([randomString, msgLength, msg, Buffer.from(this.appId)]), this.blockSize);
36
+ encrypted = AES_1.AES.encrypt(encoded, this.aesKey, this.aesKey.slice(0, 16), false, 'aes-256-cbc').toString('base64');
39
37
  }
40
38
  catch (e) {
41
39
  throw new Error('Fail to encrypt data');
@@ -44,22 +42,21 @@ class Encryptor {
44
42
  nonce = this.appId.slice(0, 10);
45
43
  if (!timestamp)
46
44
  timestamp = Utils_1.getTimestamp();
47
- return {
45
+ let response = {
48
46
  Encrypt: encrypted,
49
47
  MsgSignature: this.signature(this.token, timestamp, nonce, encrypted),
50
48
  TimeStamp: timestamp,
51
49
  Nonce: nonce,
52
50
  };
51
+ return Utils_1.buildXml(response);
53
52
  }
54
53
  decrypt(text, msgSignature, nonce, timestamp) {
55
54
  let signature = this.signature(this.token, nonce, timestamp, text);
56
55
  if (signature !== msgSignature) {
57
56
  throw new Error('Invalid Signature.');
58
57
  }
59
- let decipher = crypto_1.default.createDecipheriv('aes-256-cbc', this.aesKey, this.aesKey.slice(0, 16));
60
- decipher.setAutoPadding(false);
61
- let deciphered = Buffer.concat([decipher.update(text, 'base64'), decipher.final()]);
62
- deciphered = this.pkcs7Unpad(deciphered);
58
+ let deciphered = AES_1.AES.decrypt(Buffer.from(text, 'base64'), this.aesKey, this.aesKey.slice(0, 16), false, 'aes-256-cbc');
59
+ deciphered = PKCS_1.PKCS7.unpad(deciphered, this.blockSize);
63
60
  let content = deciphered.slice(16);
64
61
  let length = content.slice(0, 4).readUInt32BE(0);
65
62
  if (content.slice(length + 4).toString() !== this.appId) {
@@ -67,33 +64,6 @@ class Encryptor {
67
64
  }
68
65
  return content.slice(4, length + 4).toString();
69
66
  }
70
- /**
71
- * 删除解密后明文的补位字符
72
- * @param {Buffer} text 解密后的明文
73
- * @return {Buffer}
74
- */
75
- pkcs7Unpad(text) {
76
- var pad = text[text.length - 1];
77
- if (pad < 1 || pad > this.blockSize) {
78
- pad = 0;
79
- }
80
- return text.slice(0, text.length - pad);
81
- }
82
- /**
83
- * 对需要加密的明文进行填充补位
84
- * @param {Buffer} text 需要进行填充补位操作的明文
85
- * @return {Buffer}
86
- */
87
- pkcs7Pad(text, blockSize) {
88
- if (blockSize > 256) {
89
- throw new Error('blockSize may not be more than 256');
90
- }
91
- //计算需要填充的位数
92
- let amountToPad = blockSize - (text.length % blockSize);
93
- let result = Buffer.alloc(amountToPad);
94
- result.fill(amountToPad);
95
- return Buffer.concat([text, result]);
96
- }
97
67
  }
98
68
  exports.default = Encryptor;
99
69
  ;