flow-doc-deleted/op/Utils.md

3.8 KiB
Raw Blame History

Utils

工具类

XML 解析

op.utils.parseXML 用来解析 XML 字符串并返回一个 JSON 对象。

parseXML: (xmlStr: string, options?: OpParseXMLOptions) => any

参数说明:

TODO

示例代码:

const onSyncTask = async (config, env, op, eventData) => {
  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_",
  });
};

以上代码将返回:

{
  "oauth": {
    "data": {
      "list": [
        {
          "name": {
            "#text": "Tom",
            "X_age": "32",
          }
        },
        {
          "name": {
            "#text": "Jerry",
            "X_age": "28",
          }
        },
      ]
    }
  }
}

字符串编码转换utf-8hexbase64等

op.utils.formatString 用以将字符串在 utf8、hex、base64 等格式之间进行转换。

formatString: (data: any, from: string | undefined, to: string) => string

示例代码:

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');

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

示例代码:

const onSyncTask = async (config, env, op, eventData) => {
  // 子账号加密示例
  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);
};

crypto

op.utils.crypto 提供了 MD5、SHA256、AES、TripleDES 等算法的使用。

示例代码:

const onSyncTask = async (config, env, op, eventData) => {
  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;
};