Created
February 11, 2014 12:08
-
-
Save zacyu/8933676 to your computer and use it in GitHub Desktop.
Bilibili API Query Signature
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'), | |
_ = require('underscore'), | |
APP_KEY = false, | |
APP_SECRET = false; | |
function getSignedQueryString(query) { | |
if (!APP_SECRET) { | |
throw 'ERROR: App-Secret not provided'; | |
return false; | |
} | |
var queryKeys = [], | |
sortedQuery = {}; | |
for (var key in query) { | |
if (query.hasOwnProperty(key) && query[key]) { | |
queryKeys.push(key); | |
} | |
} | |
queryKeys.sort(); | |
var signedQueryString = '', | |
queryString = ''; | |
_.each(queryKeys, function(queryKey) { | |
queryString += queryKey + '=' + query[queryKey] + '&'; | |
signedQueryString += encodeURIComponent(queryKey) + '=' + encodeURIComponent(query[queryKey]) + '&'; | |
}); | |
queryString = queryString.substr(0, queryString.length - 1); | |
signedQueryString = signedQueryString.substr(0, signedQueryString.length - 1); | |
signedQueryString += '&sign=' + crypto.createHash('md5').update(new Buffer(signedQueryString + APP_SECRET, 'utf-8')).digest('hex'); | |
return signedQueryString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment