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

时间:2017-10-21 06:40:54

标签: 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 ;
    }   
?>

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

1 个答案:

答案 0 :(得分:1)

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 ;
}