qsh-webview-sdk 2.0.3 → 2.0.5

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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # UniApp WebView SDK
1
+ # QSH WebView SDK
2
2
 
3
- 一个重构后的 UniApp WebView SDK,支持微信小程序和 APP 端的 WebView 与原生端通信。
3
+ 一个重构后的 QSH WebView SDK,支持微信小程序和 APP 端的 WebView 与原生端通信。
4
4
 
5
5
  ## 特性
6
6
 
@@ -22,7 +22,7 @@ npm install qsh-webview-sdk
22
22
  ### CDN 引入
23
23
 
24
24
  ```html
25
- <script src="path/to/uni-webview.umd.js"></script>
25
+ <script src="path/to/qsh-webview-sdk.umd.js"></script>
26
26
  ```
27
27
 
28
28
  ## 使用方法
@@ -37,10 +37,15 @@ npm install qsh-webview-sdk
37
37
  }
38
38
  </script>
39
39
 
40
- <!-- 引入 UniApp WebView SDK -->
41
- <script type="text/javascript" src="path/to/uni-webview.umd.js"></script>
40
+ <!-- 引入 QSH WebView SDK -->
41
+ <script type="text/javascript" src="path/to/qsh-webview-sdk.umd.js"></script>
42
42
  ```
43
43
 
44
+ ### 初始化
45
+
46
+ - SDK 会在 DOM 就绪后自动初始化,并触发 `UniAppJSBridgeReady` 事件。
47
+ - 可使用 `qsh.ready()` 等待准备完成;如需控制时机,可手动调用 `qsh.init()`。
48
+
44
49
  ### 基础使用
45
50
 
46
51
  ```javascript
@@ -115,6 +120,45 @@ console.log('是否微信小程序:', env.isWeixinMiniProgram);
115
120
  console.log('是否 APP:', env.isAppPlus);
116
121
  ```
117
122
 
123
+ ### 加密能力(SM2/SM3/SM4)
124
+
125
+ - 已内置并通过 `qsh.crypto` 暴露以下能力:`sm2`(密钥对/加解密/签名验签)、`sm3`(杂凑/HMAC)、`sm4`(对称加解密)。
126
+ - 参考实现来源:[miniprogram-sm-crypto(GitHub)](https://github.com/wechat-miniprogram/sm-crypto)。
127
+
128
+ #### ESM 使用
129
+
130
+ ```javascript
131
+ import qsh from 'qsh-webview-sdk';
132
+
133
+ // SM3 杂凑
134
+ const hash = qsh.crypto.sm3Hash('abc');
135
+
136
+ // SM2:生成密钥对 + 签名 + 验签(示例使用 DER 编码)
137
+ const { publicKey, privateKey } = qsh.crypto.sm2GenerateKeyPair();
138
+ const sig = qsh.crypto.sm2Sign('hello sm2', privateKey, { der: true });
139
+ const ok = qsh.crypto.sm2Verify('hello sm2', sig, publicKey, { der: true });
140
+
141
+ // SM4:默认 ECB + pkcs#7 填充
142
+ const key = '0123456789abcdeffedcba9876543210';
143
+ const cipher = qsh.crypto.sm4Encrypt('hello', key);
144
+ const plain = qsh.crypto.sm4Decrypt(cipher, key);
145
+ ```
146
+
147
+ #### UMD 使用
148
+
149
+ ```html
150
+ <script src="path/to/qsh-webview-sdk.umd.js"></script>
151
+ <script>
152
+ // 直接通过全局 qsh 访问
153
+ const hash = window.qsh.crypto.sm3Hash('abc');
154
+ </script>
155
+ ```
156
+
157
+ 提示:
158
+ - SM2 默认使用 C1C3C2(cipherMode=1)。与其他工具互通时请确保模式一致。
159
+ - SM4 支持 ECB/CBC 等模式;CBC 需提供 `iv`。
160
+ - 如需数组输入/输出,可通过 `options.output = 'array'` 控制(与上游库一致)。
161
+
118
162
  ## API 文档
119
163
 
120
164
  ### 导航 API
@@ -134,6 +178,10 @@ console.log('是否 APP:', env.isAppPlus);
134
178
  | `postMessage` | 向原生端发送消息 | `{ data?: any }` |
135
179
  | `getEnv` | 获取当前环境信息 | `callback: Function` |
136
180
 
181
+ ### 加密 API
182
+
183
+ - 通过 `qsh.crypto` 使用加密能力,详情与示例参见上文“加密能力(SM2/SM3/SM4)”。
184
+
137
185
  ### 环境属性
138
186
 
139
187
  | 属性 | 类型 | 说明 |
@@ -169,8 +217,10 @@ npm run build
169
217
 
170
218
  项目包含以下示例文件:
171
219
 
220
+ - `example/index.html` - 入口示例
172
221
  - `example/test.html` - 完整的测试页面,包含所有功能演示
173
222
  - `example/simple.html` - 简单示例,展示基础用法
223
+ - `example/npm-usage.html` - 以 ESModule 方式演示,包括加密能力示例
174
224
  - `test.html` - 快速测试页面
175
225
 
176
226
  ## 项目结构
@@ -186,7 +236,8 @@ src/
186
236
  │ └── app.js # APP 端
187
237
  ├── api/ # API 接口
188
238
  │ ├── navigation.js # 导航 API
189
- └── message.js # 消息 API
239
+ ├── message.js # 消息 API
240
+ │ └── crypto.js # 加密 API(SM2/SM3/SM4)
190
241
  ├── index.js # 主入口
191
242
  └── index.d.ts # 类型定义
192
243
  ```
package/dist/index.d.ts CHANGED
@@ -90,6 +90,26 @@ declare namespace UniWebView {
90
90
  ready(): Promise<void>;
91
91
  /** WebView 对象(兼容旧版本) */
92
92
  webView: WebView | null;
93
+ /** 加密 API(SM2/SM3/SM4) */
94
+ crypto: {
95
+ // SM2
96
+ sm2GenerateKeyPair(random?: any): { publicKey: string; privateKey: string };
97
+ sm2Encrypt(message: string | number[], publicKey: string, cipherMode?: 0 | 1, options?: Record<string, any>): string | number[];
98
+ sm2Decrypt(cipherText: string | number[], privateKey: string, cipherMode?: 0 | 1, options?: { output?: 'array' | 'string' }): string | number[];
99
+ sm2Sign(message: string | number[], privateKey: string, options?: Record<string, any>): string;
100
+ sm2Verify(message: string | number[], signatureHex: string, publicKey: string, options?: Record<string, any>): boolean;
101
+ sm2GetPublicKey(privateKey: string): string;
102
+ sm2GetPoint(): any;
103
+ sm2CompressPublicKey(publicKey: string): string;
104
+ sm2ComparePublicKey(a: string, b: string): boolean;
105
+ sm2VerifyPublicKey(publicKey: string): boolean;
106
+ // SM3
107
+ sm3Hash(message: string | number[], options?: { key?: string | number[] }): string;
108
+ // SM4
109
+ sm4Encrypt(message: string | number[], key: string | number[], options?: { padding?: 'pkcs#7' | 'pkcs#5' | 'none'; output?: 'array' | 'string'; mode?: 'ecb' | 'cbc'; iv?: string | number[] }): string | number[];
110
+ sm4Decrypt(cipherText: string | number[], key: string | number[], options?: { padding?: 'pkcs#7' | 'pkcs#5' | 'none'; output?: 'array' | 'string'; mode?: 'ecb' | 'cbc'; iv?: string | number[] }): string | number[];
111
+ __raw__: any;
112
+ };
93
113
  }
94
114
  }
95
115