如何生成和验证节点JS中给定字符串的校验和
问题描述:
我想在节点Js中重写下面的函数来生成校验和并验证支付事务,而我在Node Js中编写代码还很新。如何生成和验证节点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中编写以下代码。
答
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 ;
}
看一看一些MD5实现这个[SO](https://stackoverflow.com/questions/14733374/how-to-generate-md5-file-hash-on-javascript)问题。 –