Created
September 9, 2013 21:53
-
-
Save deserat/6502045 to your computer and use it in GitHub Desktop.
A working javascript implementation CLRAuth for API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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