5

I am trying to rewrite the following function in Node Js for checksum generation and verifying for a payment transaction and I quite new to writing code in Node Js.

I have got the code from the Service Provide which I need to convert into Node Js. I am using express as my backend.

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

How Can I write the following code in Javascript with passing the parameter.

1
  • Have a look at this SO question for some MD5 implementations. Commented Oct 21, 2017 at 6:50

1 Answer 1

6
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 ;
}  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.