-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathapi-redsys.js
More file actions
71 lines (60 loc) · 2.13 KB
/
api-redsys.js
File metadata and controls
71 lines (60 loc) · 2.13 KB
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
'use strict';
const crypto = require('crypto');
const base64url = require('base64url');
const { zeroPad, zeroUnpad } = require('./utils');
class Redsys {
encrypt3DES(str, key) {
const secretKey = Buffer.from(key, 'base64');
const iv = Buffer.alloc(8, 0);
const cipher = crypto.createCipheriv('des-ede3-cbc', secretKey, iv);
cipher.setAutoPadding(false);
const en_key = cipher.update(zeroPad(str, 8), 'utf8', 'binary') + cipher.final('binary');
const l = Math.ceil(str.length / 8) * 8;
return Buffer.from(en_key.substr(0, l), 'binary').toString('base64');
}
decrypt3DES(str, key) {
const secretKey = Buffer.from(key, 'base64');
const iv = Buffer.alloc(8, 0);
const cipher = crypto.createDecipheriv('des-ede3-cbc', secretKey, iv);
cipher.setAutoPadding(false);
const res = cipher.update(zeroUnpad(str, 8), 'base64', 'utf8') +
cipher.final('utf8');
return res.replace(/\0/g, '');
}
mac256(data, key) {
return crypto.createHmac('sha256', Buffer.from(key, 'base64'))
.update(data)
.digest('base64');
}
createMerchantParameters(data) {
return Buffer.from(JSON.stringify(data), 'utf8').toString('base64');
}
decodeMerchantParameters(data) {
const _data = JSON.parse(base64url.decode(data, 'utf8'));
const res = {};
for (var name in _data) {
res[decodeURIComponent(name)] = decodeURIComponent(_data[name]);
}
return res;
}
createMerchantSignature(key, data) {
const _data = this.createMerchantParameters(data);
const orderId = data.Ds_Merchant_Order || data.DS_MERCHANT_ORDER;
const orderKey = this.encrypt3DES(orderId, key);
return this.mac256(_data, orderKey);
}
createMerchantSignatureNotif(key, data) {
const _data = this.decodeMerchantParameters(data);
const orderId = _data.Ds_Order || _data.DS_ORDER;
const orderKey = this.encrypt3DES(orderId, key);
const res = this.mac256(data, orderKey);
return base64url.encode(res, 'base64');
}
merchantSignatureIsValid(signA, signB) {
return base64url.decode(signA, 'base64') ===
base64url.decode(signB, 'base64');
}
}
module.exports = {
Redsys,
};