feat: update utils doc for crypto
This commit is contained in:
parent
26b05995d8
commit
5b6b77ab02
65
Utils.md
65
Utils.md
|
@ -1,12 +1,15 @@
|
|||
#### Utils
|
||||
# Utils
|
||||
工具类
|
||||
|
||||
##### parseXML: (xmlStr: string, options?: OpParseXMLOptions) => any
|
||||
## parseXML: (xmlStr: string, options?: OpParseXMLOptions) => any
|
||||
op.utils.parseXML 用来解析 XML 字符串并返回一个 JSON 对象。
|
||||
|
||||
参数说明:
|
||||
|
||||
TODO
|
||||
|
||||
示例代码:
|
||||
|
||||
```javaScript
|
||||
exports.run = async (ctx, op) => {
|
||||
const data = `
|
||||
|
@ -26,7 +29,9 @@ exports.run = async (ctx, op) => {
|
|||
});
|
||||
};
|
||||
```
|
||||
|
||||
以上代码返回:
|
||||
|
||||
```javaScript
|
||||
{
|
||||
"oauth": {
|
||||
|
@ -51,21 +56,26 @@ exports.run = async (ctx, op) => {
|
|||
|
||||
```
|
||||
|
||||
##### formatString: (data: any, from: string | undefined, to: string) => string
|
||||
## formatString: (data: any, from: string | undefined, to: string) => string
|
||||
op.utils.formatString 用以将字符串在 utf8、hex、base64 等格式之间进行转换。
|
||||
|
||||
示例代码:
|
||||
TODO
|
||||
|
||||
##### 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;
|
||||
## SM4
|
||||
|
||||
op.utils.encodeSM4 与 op.utils.decodeSM4 分别用以进行 sm4 的加解密。
|
||||
|
||||
```
|
||||
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;
|
||||
```
|
||||
|
||||
参数说明:
|
||||
TODO
|
||||
|
||||
示例代码:
|
||||
|
||||
```javaScript
|
||||
exports.run = async (ctx, op) => {
|
||||
// 子账号加密示例
|
||||
|
@ -78,4 +88,47 @@ exports.run = async (ctx, op) => {
|
|||
const decoded = op.utils.decodeSM4(key, encoded.replace(/^\$SM4\$/, ''), { inputEncoding: 'base64', outputEncoding: 'utf8' });
|
||||
log(decoded === data);
|
||||
};
|
||||
```
|
||||
```
|
||||
|
||||
## crypto
|
||||
|
||||
示例代码:
|
||||
|
||||
```javascript
|
||||
exports.run = async (ctx, op) => {
|
||||
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;
|
||||
};
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue