邮递员中的消息签名 sha256 错误(未定义 base64。)

errore with messageSignature sha256 in postman (base64 is notdefined.)

我正在测试一个 api,我必须根据文档在正文中发送这些数据:

studentId,
apiKey,
timeStamp (current iso time as a string),
messageSignature: apikey+studentId+timeStamp encrypted using sha256,

我在预请求脚本中使用 sha256 编写了一个脚本来生成消息签名。 预请求脚本:


    var dateIso = new Date().toISOString();
    pm.globals.set("isoDateTostring", dateIso);
    console.log('timestamp var is:', pm.globals.get("isoDateTostring"));
    
    let msg = "apiKeyvalue" + "studentId" + pm.globals.get("isoDateTostring");+ JSON.stringify(msg)
    
    const hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, "secretKey");
    hmac.update(msg);
    const messageSignature = hmac.finalize().toString();

详情(如截图): 我在正文中写道:

{
"studentId":"********",
"apiKey":"************",
"timeStamp":{{$isoDateTostring}},
"messageSignature": {{$messageSignature}}
}

当我发送请求时,我得到 base64 未定义。

代码如下:

标签正文中:

{{$isoDateTostring}} --> "{{isoDateTostring}}"

{{$messageSignature}} --> "{{messageSignature}}"

{
    "studentId": "123",
    "apiKey": "123abc",
    "timeStamp": "{{isoDateTostring}}",
    "messageSignature": "{{messageSignature}}"
}

在选项卡预请求中: 我伪造了 studentIdapiKey

const dateIso = new Date().toISOString();
pm.globals.set("isoDateTostring", dateIso);

const studentId = "123";
const apiKeyvalue = "123abc"
let msg = apiKeyvalue + studentId + pm.globals.get("isoDateTostring");

const messageSignature = CryptoJS.SHA256(msg).toString();
pm.globals.set("messageSignature", messageSignature);

结果:

{
    "studentId": "123",
    "apiKey": "123abc",
    "timeStamp": "2021-10-26T13:20:09.068Z",
    "messageSignature": "783e65ff1cfb2374fb5f84daa35c01d18b8a1898b3a1837e84934e91a3c0720d"
}