Skip to content

Instantly share code, notes, and snippets.

@deserat
Created September 9, 2013 21:53
Show Gist options
  • Save deserat/6502045 to your computer and use it in GitHub Desktop.
Save deserat/6502045 to your computer and use it in GitHub Desktop.
A working javascript implementation CLRAuth for API
GameClient.prototype.buildToken = function (body) {
var dt = Math.round(new Date().getTime() * .001)
var line = this.identity + "." + dt
var data = line
if (body) {
data = line + "." + body.trim()
}
var salt = Math.floor( Math.random() * 10000000000 )
salt = this.padString(salt.toString(), 10, '.');
var key = salt + this.secret
var hash = crypto.createHmac('sha256', key).update(data).digest()
var hexhash = crypto.createHmac('sha256', key).update(data).digest('hex')
log.debug("HASH", hexhash)
var saltBuf = new Buffer(salt.toString(),'ascii')
var hashBuf = new Buffer(hash)
var signature = Buffer.concat([saltBuf, hashBuf]);
var token = "CLRAUTH " + this.identity + " " + URLSafeBase64.encode(signature) + " " + URLSafeBase64.encode(new Buffer(line))
return token
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment