mic & mac calculation

thanks to @alexander-albers!
see https://github.com/schildbach/public-transport-enabler/issues/187#issuecomment-357436079
This commit is contained in:
Jannis R 2018-01-15 00:45:40 +01:00
parent 904f890442
commit 6f958e14be
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
2 changed files with 16 additions and 6 deletions

View file

@ -28,6 +28,7 @@ const id = x => x
const defaultProfile = {
salt: null,
addChecksum: false,
addMicMac: false,
transformReqBody: id,
transformReq: id,

View file

@ -26,16 +26,25 @@ 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.')
}
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)
return fetch(url, req)