     isFnCalled = true
     fn.apply scope, args
 
+urlSafeBase64decode = (encoded)->
+  encoded = encoded.replace(/\-/g, '+').replace(/_/g, '/')
+  new Buffer(encoded, 'base64')
+
+sha256 = (s)->
+  shasum = crypto.createHash 'sha256'
+  shasum.update(s)
+  shasum.digest('binary')
+
+decrypt = (encodedToken, kodingKey)->
+# token = 'u3IsfJXb9gdzfqskbGj_lQK-_Aw1BhX8eWUBZzbqRFAi-Q-TCxmd7KILfL6Pvjrh117ZYHOdB2yy9YcTId9cMV3TN6oKk6zhjr5x8_QViuGqckmNP6OSKMGh5cIFOehi6oxpeHRb2pQEeVtyCHxVHOZE7v2GX6LaQz1IPwbyCDja9Mw='
+# kodingKey = 'NPhFRpZPYuWA4kFA7Y0ewEIQy7qdgj3ij6tegRQ9sd2s-g2-Debdi20fStRiIM5Z'
+# plaintext = decrypt(token, kodingKey)
+  decodedToken = urlSafeBase64decode encodedToken
+  iv = (new Buffer decodedToken.slice(0, 16))
+  kodingKey = sha256(urlSafeBase64decode(kodingKey))
+  ciphertext = decodedToken.slice(16, decodedToken.length)
+  # Decipher encrypted data
+  decipher = crypto.createDecipheriv('aes-256-cfb', kodingKey, iv);
+  decrypted = decipher.update(ciphertext, 'binary') + decipher.final('binary');
+  new Buffer(decrypted, 'binary').toString('utf8');
+
 class Environment
   constructor: (@options={})->
     @basePath = path.join env.HOME || env.HOMEPATH || env.USERPROFILE, ".kd"

os = require 'os'
path = require 'path'
uuid = require 'node-uuid'
WebSocket = require 'ws'
WebSocketServer = require('ws').Server
{env} = process

    @options.environment  or= "development"
    @options.region       or= "localhost"
    @options.kontrolAddress or= 'ws://localhost:4000/dnode'
    #
    @_kodingKey = null

  kodingKey:()->
    if not @_kodingKey
      @_kodingKey = @readKeyFile()
    return @_kodingKey

  readKeyFile: ()->
    kodingKey = fs.readFileSync path.join @basePath, "koding.key"
