-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.js
94 lines (84 loc) Β· 2.82 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
"use strict";
var crypto = require('crypto');
class PaytmChecksum {
static encrypt(input, key) {
var cipher = crypto.createCipheriv('AES-128-CBC', key, PaytmChecksum.iv);
var encrypted = cipher.update(input, 'binary', 'base64');
encrypted += cipher.final('base64');
return encrypted;
}
static decrypt(encrypted, key) {
var decipher = crypto.createDecipheriv('AES-128-CBC', key, PaytmChecksum.iv);
var decrypted = decipher.update(encrypted, 'base64', 'binary');
try {
decrypted += decipher.final('binary');
}
catch (e) {
console.log(e);
}
return decrypted;
}
static generateSignature(params, key) {
if (typeof params !== "object" && typeof params !== "string") {
var error = "string or object expected, " + (typeof params) + " given.";
return Promise.reject(error);
}
if (typeof params !== "string"){
params = PaytmChecksum.getStringByParams(params);
}
return PaytmChecksum.generateSignatureByString(params, key);
}
static verifySignature(params, key, checksum) {
if (typeof params !== "object" && typeof params !== "string") {
var error = "string or object expected, " + (typeof params) + " given.";
return Promise.reject(error);
}
if(params.hasOwnProperty("CHECKSUMHASH")){
delete params.CHECKSUMHASH
}
if (typeof params !== "string"){
params = PaytmChecksum.getStringByParams(params);
}
return PaytmChecksum.verifySignatureByString(params, key, checksum);
}
static async generateSignatureByString(params, key) {
var salt = await PaytmChecksum.generateRandomString(4);
return PaytmChecksum.calculateChecksum(params, key, salt);
}
static verifySignatureByString(params, key, checksum) {
var paytm_hash = PaytmChecksum.decrypt(checksum, key);
var salt = paytm_hash.substr(paytm_hash.length - 4);
return (paytm_hash === PaytmChecksum.calculateHash(params, salt));
}
static generateRandomString(length) {
return new Promise(function (resolve, reject) {
crypto.randomBytes((length * 3.0) / 4.0, function (err, buf) {
if (!err) {
var salt = buf.toString("base64");
resolve(salt);
}
else {
console.log("error occurred in generateRandomString: " + err);
reject(err);
}
});
});
}
static getStringByParams(params) {
var data = {};
Object.keys(params).sort().forEach(function(key,value) {
data[key] = (params[key] !== null && params[key].toLowerCase() !== "null") ? params[key] : "";
});
return Object.values(data).join('|');
}
static calculateHash(params, salt) {
var finalString = params + "|" + salt;
return crypto.createHash('sha256').update(finalString).digest('hex') + salt;
}
static calculateChecksum(params, key, salt) {
var hashString = PaytmChecksum.calculateHash(params, salt);
return PaytmChecksum.encrypt(hashString,key);
}
}
PaytmChecksum.iv = '@@@@&&&&####$$$$';
module.exports = PaytmChecksum;