Skip to content

Instantly share code, notes, and snippets.

@zacyu
Created February 11, 2014 12:08
Show Gist options
  • Save zacyu/8933676 to your computer and use it in GitHub Desktop.
Save zacyu/8933676 to your computer and use it in GitHub Desktop.
Bilibili API Query Signature
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