Created
August 25, 2010 16:50
-
-
Save KOBA789/549840 to your computer and use it in GitHub Desktop.
This file contains 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
var crypto = require("crypto"); | |
var Evil = new Object(); | |
const NONCE_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; | |
Evil.getNonce = function (charsLength) { | |
var result = new String(); | |
for (var i = 0; i < charsLength; i++) { | |
var randomNumber = this.getRandom(NONCE_CHARS.length); | |
result += NONCE_CHARS.charAt(randomNumber); | |
} | |
return result; | |
} | |
Evil.getRandom = function (maxVal) { | |
return Math.floor(Math.random() * maxVal); | |
} | |
const REPLACE_MAP = { | |
"!": "%21", | |
"'": "%27", | |
"(": "%28", | |
")": "%29", | |
"*": "%2A" | |
} | |
Evil.escapeChars = function (str) { | |
var result = new String(); | |
if (!str) { | |
return result; | |
} | |
result = encodeURIComponent(str); | |
for (var origin in REPLACE_MAP) { | |
var destination = REPLACE_MAP[origin]; | |
result = result.replaceAll(origin, destination); | |
} | |
return result; | |
} | |
Evil.generateSignature = function (protocol, url, parameter, consumerSecret, tokenSecret) { | |
var result = new String(); | |
var e_protocol = this.escapeChars(protocol); | |
var e_url = this.escapeChars(url); | |
var e_parameter = this.escapeChars(parameter); | |
var key = consumerSecret + "&" + tokenSecret; | |
var jointed = e_protocol + "&" + e_url + "&" + e_parameter; | |
var hmac = crypto.createHmac("sha1", key); | |
hmac.update(jointed); | |
digest = hmac.digest("base64"); | |
result = this.escapeChars(digest); | |
return result; | |
} | |
Evil.generateParameter = function (parameters) { | |
var result = new String(); | |
var keys = new Array(); | |
for (var key in parameters) { | |
keys.push(key); | |
} | |
keys.sort(); | |
for (var index in keys) { | |
var key = keys[index]; | |
result += key + "=" + parameters[key] + "&" | |
} | |
result = result.substring(0, result.length - 1); | |
return result; | |
} | |
Evil.getTimestamp = function () { | |
var result = new Number(); | |
result = parseInt((new Date)/1000); | |
return result; | |
} | |
global.Evil = Evil; | |
String.prototype.replaceAll = function (origin, destination) { | |
var result = new String(); | |
result = this.split(origin).join(destination); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment