如何在 Node JS 中生成并验证给定字符串的校验和

Rah*_*ani 4 javascript node.js express

我正在尝试在 Node Js 中重写以下函数,以生成校验和并验证支付交易,而且我对在 Node Js 中编写代码还很陌生。

我已经从服务提供者那里获得了代码,我需要将其转换为 Node Js。我使用 Express 作为我的后端。

    <?php

    function generateChecksum($transId,$sellingCurrencyAmount,$accountingCurrencyAmount,$status, $rkey,$key)
    {   
        $str = "$transId|$sellingCurrencyAmount|$accountingCurrencyAmount|$status|$rkey|$key";
        $generatedCheckSum = md5($str);
        return $generatedCheckSum;
    }

    function verifyChecksum($paymentTypeId, $transId, $userId, $userType, $transactionType, $invoiceIds, $debitNoteIds, $description, $sellingCurrencyAmount, $accountingCurrencyAmount, $key, $checksum)
    {
        $str = "$paymentTypeId|$transId|$userId|$userType|$transactionType|$invoiceIds|$debitNoteIds|$description|$sellingCurrencyAmount|$accountingCurrencyAmount|$key";
        $generatedCheckSum = md5($str);
//      echo $str."<BR>";
//      echo "Generated CheckSum: ".$generatedCheckSum."<BR>";
//      echo "Received Checksum: ".$checksum."<BR>";
        if($generatedCheckSum == $checksum)
            return true ;
        else
            return false ;
    }   
?>
Run Code Online (Sandbox Code Playgroud)

如何在 Javascript 中编写以下代码并传递参数。

小智 5

var crypto = require('crypto');
function generateChecksum(transId,sellingCurrencyAmount,accountingCurrencyAmount,status, rkey,key)
{   
    var str = `${transId}|${sellingCurrencyAmount}|${accountingCurrencyAmount}|${status}|${rkey}|${key}`;
    var generatedCheckSum = crypto.createHash('md5').update(str).digest("hex");
    return generatedCheckSum;
}

function verifyChecksum(paymentTypeId, transId, userId, userType, transactionType, invoiceIds, debitNoteIds, description, sellingCurrencyAmount, accountingCurrencyAmount, key, checksum)
{
    var str = `${paymentTypeId}|${transId}|${userId}|${userType}|${transactionType}|${invoiceIds}|${debitNoteIds}|${description}|${sellingCurrencyAmount}|${accountingCurrencyAmount}|${key}`;
    var generatedCheckSum = crypto.createHash('md5').update(str).digest("hex");

    if(generatedCheckSum == checksum)
        return true ;
    else
        return false ;
}  
Run Code Online (Sandbox Code Playgroud)