CODEARTS API-使用JS类库:使用方式

时间:2024-04-11 09:18:10

使用方式

/**
 * 示例一:该示例引入加密算法模块 crypto-js,并使用其中 AES 对 message 进行加解密 
 * @param message 需要加密的文本
 * @param key 秘钥
 */
function cryptoJSTest(message, key) {
    const CryptoJS = require('crypto-js');
    // 加密
    const ciphertext = CryptoJS.AES.encrypt(message, key).toString();
    // 解密
    const bytes  = CryptoJS.AES.decrypt(ciphertext, key);
    const originalText = bytes.toString(CryptoJS.enc.Utf8);
    console.log(originalText);
}
cryptoJSTest('test message', 'secret key');

/**
 * 示例二:该示例引入 uuid 模块生成一段 uuid,并引入 btoa 模块进行 Base64 编码
 */
function uuidBtoaTest() {
    const uuid = require('uuid');
    const btoa = require('btoa');
    const id = uuid.v4();
    const base64EncodedId = btoa(id);
    console.log(id);
    console.log(base64EncodedId);
}
uuidBtoaTest();

/**
 * 示例三:该示例引入 lodash 模块,并测试了其中的 uniq 对数组去重
 */
function lodashUniqTest() {
    const lodash = require('lodash');
    const arr =[1, 2, 1, 5, 1, 9]
    const uniqArr = lodash.uniq(arr);
    console.log(uniqArr.toString())
}
lodashUniqTest();
support.huaweicloud.com/usermanual-apiarts/apiarts_01_0031.html