From 6f958e14be6fc2683684aea2a11744a96f67f2ea Mon Sep 17 00:00:00 2001 From: Jannis R Date: Mon, 15 Jan 2018 00:45:40 +0100 Subject: [PATCH] mic & mac calculation thanks to @alexander-albers! see https://github.com/schildbach/public-transport-enabler/issues/187#issuecomment-357436079 --- lib/default-profile.js | 1 + lib/request.js | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/default-profile.js b/lib/default-profile.js index 1e767cbc..7abcf60d 100644 --- a/lib/default-profile.js +++ b/lib/default-profile.js @@ -28,6 +28,7 @@ const id = x => x const defaultProfile = { salt: null, addChecksum: false, + addMicMac: false, transformReqBody: id, transformReq: id, diff --git a/lib/request.js b/lib/request.js index d4494f91..cc5192a9 100644 --- a/lib/request.js +++ b/lib/request.js @@ -26,15 +26,24 @@ const request = (profile, data) => { query: {} }) - if (profile.addChecksum) { + if (profile.addChecksum || profile.addMicMac) { if (!Buffer.isBuffer(profile.salt)) { throw new Error('profile.salt must be a Buffer.') } - const checksum = md5(Buffer.concat([ - Buffer.from(req.body, 'utf8'), - profile.salt - ])) - req.query.checksum = checksum.toString('hex') + if (profile.addChecksum) { + const checksum = md5(Buffer.concat([ + Buffer.from(req.body, 'utf8'), + profile.salt + ])) + req.query.checksum = checksum.toString('hex') + } + if (profile.addMicMac) { + const mic = md5(Buffer.from(req.body, 'utf8')) + req.query.mic = mic.toString('hex') + + const mac = md5(Buffer.concat([mic, profile.salt])) + req.query.mac = mac.toString('hex') + } } const url = profile.endpoint +'?' + stringify(req.query)