-
Notifications
You must be signed in to change notification settings - Fork 8
/
checksum.js
executable file
·144 lines (126 loc) · 3.98 KB
/
checksum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"use strict";
var crypt = require('./crypt');
var util = require('util');
var crypto = require('crypto');
//mandatory flag: when it set, only mandatory parameters are added to checksum
function paramsToString(params, mandatoryflag) {
var data = '';
var tempKeys = Object.keys(params);
tempKeys.sort();
tempKeys.forEach( function(key) {
var n = params[key].includes("REFUND");
var m = params[key].includes("|");
if(n === true )
{
params[key] = "";
}
if(m === true)
{
params[key] = "";
}
if (key !== 'CHECKSUMHASH' ) {
if (params[key] === 'null') params[key] = '';
if (!mandatoryflag || mandatoryParams.indexOf(key) !== -1) {
data += (params[key] + '|');
}
}
});
return data;
}
function genchecksum(params, key, cb) {
var data = paramsToString(params);
crypt.gen_salt(4, function (err, salt) {
if(err){
console.log(err);
}
var sha256 = crypto.createHash('sha256').update(data + salt).digest('hex');
var check_sum = sha256 + salt;
var encrypted = crypt.encrypt(check_sum, key);
params.CHECKSUMHASH = encrypted;
cb(undefined, params);
});
}
function genchecksumbystring(params, key, cb) {
crypt.gen_salt(4, function (err, salt) {
if(err){
console.log(err);
}
var sha256 = crypto.createHash('sha256').update(params + '|' + salt).digest('hex');
var check_sum = sha256 + salt;
var encrypted = crypt.encrypt(check_sum, key);
var CHECKSUMHASH = encodeURIComponent(encrypted);
CHECKSUMHASH = encrypted;
cb(undefined, CHECKSUMHASH);
});
}
function verifychecksum(params, key) {
var data = paramsToString(params, false);
//TODO: after PG fix on thier side remove below two lines
if (params.CHECKSUMHASH) {
params.CHECKSUMHASH = params.CHECKSUMHASH.replace('\n', '');
params.CHECKSUMHASH = params.CHECKSUMHASH.replace('\r', '');
var temp = decodeURIComponent(params.CHECKSUMHASH);
var checksum = crypt.decrypt(temp, key);
var salt = checksum.substr(checksum.length - 4);
var sha256 = checksum.substr(0, checksum.length - 4);
var hash = crypto.createHash('sha256').update(data + salt).digest('hex');
if (hash === sha256) {
return true;
} else {
util.log("checksum is wrong");
return false;
}
} else {
util.log("checksum not found");
return false;
}
}
function verifychecksumbystring(params, key,checksumhash) {
var checksum = crypt.decrypt(checksumhash, key);
var salt = checksum.substr(checksum.length - 4);
var sha256 = checksum.substr(0, checksum.length - 4);
var hash = crypto.createHash('sha256').update(params + '|' + salt).digest('hex');
if (hash === sha256) {
return true;
} else {
util.log("checksum is wrong");
return false;
}
}
function genchecksumforrefund(params, key, cb) {
var data = paramsToStringrefund(params);
crypt.gen_salt(4, function (err, salt) {
if(err){
console.log(err);
}
var sha256 = crypto.createHash('sha256').update(data + salt).digest('hex');
var check_sum = sha256 + salt;
var encrypted = crypt.encrypt(check_sum, key);
params.CHECKSUM = encodeURIComponent(encrypted);
cb(undefined, params);
});
}
function paramsToStringrefund(params, mandatoryflag) {
var data = '';
var tempKeys = Object.keys(params);
tempKeys.sort();
tempKeys.forEach(function (key) {
var m = params[key].includes("|");
if(m === true)
{
params[key] = "";
}
if (key !== 'CHECKSUMHASH' ) {
if (params[key] === 'null') params[key] = '';
if (!mandatoryflag || mandatoryParams.indexOf(key) !== -1) {
data += (params[key] + '|');
}
}
});
return data;
}
module.exports.genchecksum = genchecksum;
module.exports.verifychecksum = verifychecksum;
module.exports.verifychecksumbystring = verifychecksumbystring;
module.exports.genchecksumbystring = genchecksumbystring;
module.exports.genchecksumforrefund = genchecksumforrefund;