I'm trying to find a suitable Node.js method of recreating the functionality found here: https://github.com/fastly/token-functions
Specifically, I'm getting hung up on the "pack()" function (I believe, could be my crypto function?) found across the various languages in the library. Here is my current implementation:
var key = atob('RmFzdGx5IFRva2VuIFRlc3Q='),
time = parseInt( new Date().getTime() / (60 * 1000)),
timeBuf = bufferpack.pack('<Q',time),
timeHash = timeBuf.toString('utf-8'),
hash = crypto.createHmac('sha256',key).update(timeHash).digest('base64');
'hash' always outputs:
RgpiUKREY9HHjlHPTu0T/93afFzSIpO+T3lduomkmg4=
even as 'time' and 'timeHash' change.
I'm using the bufferpack library found here: https://github.com/ryanrolds/bufferpack
The second argument to bufferpack.pack should be an array of values as the documentation in the readme states. bufferpack.pack('<Q',time) should be:
bufferpack.pack('<Q', [time])
Fastly Support responded with a script. Here it is for future reference until they add it to their repo.
var key = "Fastly Test Token";
var interval = 60;
var number = Math.round(Math.round(new Date().getTime() / 1000) / interval);
var l = (number & 0xffffffff00000000) >> 32;
var r = number & 0x00000000ffffffff;
var timeBuf = bufferpack.pack('<I<I',[r,l]);
var hash = crypto.createHmac('sha256',key).update(timeBuf).digest('base64');