Rahul Sadarangani
Rahul Sadarangani

Reputation: 150

How to Generate and Verify Checksum of the given string in Node JS

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.

Upvotes: 5

Views: 11521

Answers (1)

Ayush Mittal
Ayush Mittal

Reputation: 559

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

Upvotes: 6

Related Questions