2023-12-01 13:44:51 +08:00
|
|
|
|
# Utils
|
2023-12-03 21:09:35 +08:00
|
|
|
|
|
2023-11-17 14:49:31 +08:00
|
|
|
|
工具类
|
|
|
|
|
|
2023-12-03 21:09:35 +08:00
|
|
|
|
## XML 解析
|
|
|
|
|
|
2023-11-17 14:49:31 +08:00
|
|
|
|
op.utils.parseXML 用来解析 XML 字符串并返回一个 JSON 对象。
|
|
|
|
|
|
2023-12-03 21:09:35 +08:00
|
|
|
|
```
|
|
|
|
|
parseXML: (xmlStr: string, options?: OpParseXMLOptions) => any
|
|
|
|
|
```
|
|
|
|
|
|
2023-11-17 14:49:31 +08:00
|
|
|
|
参数说明:
|
2023-12-01 13:44:51 +08:00
|
|
|
|
|
2023-11-17 14:49:31 +08:00
|
|
|
|
TODO
|
2023-12-01 13:44:51 +08:00
|
|
|
|
|
2023-11-17 14:49:31 +08:00
|
|
|
|
示例代码:
|
2023-12-01 13:44:51 +08:00
|
|
|
|
|
2023-11-17 14:49:31 +08:00
|
|
|
|
```javaScript
|
2023-12-03 21:09:35 +08:00
|
|
|
|
const onSyncTask = async (config, env, op, eventData) => {
|
2023-11-17 14:49:31 +08:00
|
|
|
|
const data = `
|
|
|
|
|
<oauth>
|
|
|
|
|
<data>
|
|
|
|
|
<list>
|
|
|
|
|
<name age="32">Tom</name>
|
|
|
|
|
</list>
|
|
|
|
|
<list>
|
|
|
|
|
<name age="28">Jerry</name>
|
|
|
|
|
</list>
|
|
|
|
|
</data>
|
|
|
|
|
</oauth>`;
|
|
|
|
|
return op.utils.parseXML(data, {
|
|
|
|
|
ignoreAttributes: false,
|
|
|
|
|
attributeNamePrefix : "X_",
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
```
|
2023-12-01 13:44:51 +08:00
|
|
|
|
|
2023-12-03 21:09:35 +08:00
|
|
|
|
以上代码将返回:
|
2023-12-01 13:44:51 +08:00
|
|
|
|
|
2023-11-17 14:49:31 +08:00
|
|
|
|
```javaScript
|
|
|
|
|
{
|
|
|
|
|
"oauth": {
|
|
|
|
|
"data": {
|
|
|
|
|
"list": [
|
|
|
|
|
{
|
|
|
|
|
"name": {
|
|
|
|
|
"#text": "Tom",
|
|
|
|
|
"X_age": "32",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"name": {
|
|
|
|
|
"#text": "Jerry",
|
|
|
|
|
"X_age": "28",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-12-03 21:09:35 +08:00
|
|
|
|
## 字符串编码转换(utf-8,hex,base64等)
|
|
|
|
|
|
2023-11-17 14:49:31 +08:00
|
|
|
|
op.utils.formatString 用以将字符串在 utf8、hex、base64 等格式之间进行转换。
|
|
|
|
|
|
2023-12-03 21:09:35 +08:00
|
|
|
|
```
|
|
|
|
|
formatString: (data: any, from: string | undefined, to: string) => string
|
|
|
|
|
```
|
|
|
|
|
|
2023-11-17 14:49:31 +08:00
|
|
|
|
示例代码:
|
2023-12-03 21:09:35 +08:00
|
|
|
|
|
|
|
|
|
``` js
|
|
|
|
|
const hexStr = op.utils.formatString('Hello IDMesh', 'utf8', 'hex');
|
|
|
|
|
const base64Str = op.utils.formatString(hexStr, 'hex', 'base64');
|
|
|
|
|
const hello = op.utils.formatString(base64Str, 'base64', 'utf8');
|
|
|
|
|
```
|
2023-11-17 14:49:31 +08:00
|
|
|
|
|
2023-12-01 13:44:51 +08:00
|
|
|
|
## SM4
|
2023-11-17 14:49:31 +08:00
|
|
|
|
|
|
|
|
|
op.utils.encodeSM4 与 op.utils.decodeSM4 分别用以进行 sm4 的加解密。
|
|
|
|
|
|
2023-12-01 13:44:51 +08:00
|
|
|
|
```
|
|
|
|
|
decodeSM4(key: string, data: string, opts?: { mode?: string, iv?: string, inputEncoding?: string, outputEncoding?: string }) => string;
|
|
|
|
|
encodeSM4(key: string, data: string, opts?: { mode?: string, iv?: string, inputEncoding?: string, outputEncoding?: string }) => string;
|
|
|
|
|
```
|
|
|
|
|
|
2023-11-17 14:49:31 +08:00
|
|
|
|
参数说明:
|
2023-12-03 21:09:35 +08:00
|
|
|
|
|
2023-11-17 14:49:31 +08:00
|
|
|
|
TODO
|
|
|
|
|
|
|
|
|
|
示例代码:
|
2023-12-01 13:44:51 +08:00
|
|
|
|
|
2023-11-17 14:49:31 +08:00
|
|
|
|
```javaScript
|
2023-12-03 21:09:35 +08:00
|
|
|
|
const onSyncTask = async (config, env, op, eventData) => {
|
2023-11-17 14:49:31 +08:00
|
|
|
|
// 子账号加密示例
|
|
|
|
|
const secret = 'YOUR_SECRET';
|
|
|
|
|
const key = op.utils.formatString(secret, 'utf8', 'hex').slice(0, 32);
|
|
|
|
|
const data = 'YOUR_PASSWORD';
|
|
|
|
|
const encoded = '$SM4$' + op.utils.encodeSM4(key, data, { outputEncoding: 'base64' });
|
|
|
|
|
log(encoded);
|
|
|
|
|
// 子账号解密示例
|
|
|
|
|
const decoded = op.utils.decodeSM4(key, encoded.replace(/^\$SM4\$/, ''), { inputEncoding: 'base64', outputEncoding: 'utf8' });
|
|
|
|
|
log(decoded === data);
|
|
|
|
|
};
|
2023-12-01 13:44:51 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## crypto
|
|
|
|
|
|
2023-12-03 21:09:35 +08:00
|
|
|
|
op.utils.crypto 提供了 MD5、SHA256、AES、TripleDES 等算法的使用。
|
|
|
|
|
|
2023-12-01 13:44:51 +08:00
|
|
|
|
示例代码:
|
|
|
|
|
|
|
|
|
|
```javascript
|
2023-12-03 21:09:35 +08:00
|
|
|
|
const onSyncTask = async (config, env, op, eventData) => {
|
2023-12-01 13:44:51 +08:00
|
|
|
|
const list = [];
|
|
|
|
|
let msg;
|
|
|
|
|
const key = 'aesEncryptionKey';
|
|
|
|
|
const iv = 'encryptionIntVec';
|
|
|
|
|
// hashing: md5, sha1, ...
|
|
|
|
|
list.push(op.utils.crypto('MD5', '123'));
|
|
|
|
|
list.push(op.utils.crypto('SHA1', '123'));
|
|
|
|
|
list.push(op.utils.crypto('SHA256', '123'));
|
|
|
|
|
list.push(op.utils.crypto('SHA512', '123'));
|
|
|
|
|
list.push(op.utils.crypto('SHA3', '123'));
|
|
|
|
|
list.push(op.utils.crypto('RIPEMD160', '123'));
|
|
|
|
|
// aes with ecb
|
|
|
|
|
msg = op.utils.crypto('AES', 'encrypt', 'msy123', key, { mode: 'ECB' });
|
|
|
|
|
list.push(msg);
|
|
|
|
|
list.push(op.utils.crypto('AES', 'decrypt', msg, key, { mode: 'ECB' }));
|
|
|
|
|
// aes with CBC
|
|
|
|
|
msg = op.utils.crypto('AES', 'encrypt', 'msy123', key, { mode: 'CBC', iv });
|
|
|
|
|
list.push(msg);
|
|
|
|
|
list.push(op.utils.crypto('AES', 'decrypt', msg, key, { mode: 'CBC', iv }));
|
|
|
|
|
// des
|
|
|
|
|
msg = op.utils.crypto('DES', 'encrypt', 'msy123', key);
|
|
|
|
|
list.push(op.utils.crypto('DES', 'decrypt', msg, key));
|
|
|
|
|
// 3des
|
|
|
|
|
msg = op.utils.crypto('TripleDES', 'encrypt', 'msy123', key);
|
|
|
|
|
list.push(op.utils.crypto('TripleDES', 'decrypt', msg, key));
|
|
|
|
|
// base64
|
|
|
|
|
msg = op.utils.formatString('SGVsbG8sIFdvcmxkIQ==', 'base64', 'utf8');
|
|
|
|
|
list.push(msg);
|
|
|
|
|
list.push(op.utils.formatString(msg, 'utf8', 'base64'));
|
|
|
|
|
// hex
|
|
|
|
|
msg = op.utils.formatString('48656c6c6f2c20576f726c6421', 'hex', 'utf8');
|
|
|
|
|
list.push(msg);
|
|
|
|
|
list.push(op.utils.formatString(msg, 'utf8', 'hex'));
|
|
|
|
|
return list;
|
|
|
|
|
};
|
|
|
|
|
```
|