Skip to content

Instantly share code, notes, and snippets.

@YkSix
Last active July 7, 2016 08:42
Show Gist options
  • Select an option

  • Save YkSix/a9a77b75220428f1d968447d6a893f37 to your computer and use it in GitHub Desktop.

Select an option

Save YkSix/a9a77b75220428f1d968447d6a893f37 to your computer and use it in GitHub Desktop.
function signUrl(url, key, keyindex) {
var debug_signurl = 0;
if (debug_signurl) console.log("Eric; signUrl; url=", url);
// 把 http:// 取代掉成空白
// $url =~ s/^http:\/\///;
// url = "http://bot1.ect.tp2.yahoo.com/echo/wssid";
// key = "TPiKKXkckSqj13OwXURLCKHAD7WXXHOh";
url = url.replace("http://", "");
url = url.replace("https://", "");
if (debug_signurl) console.log("Eric; After replace; url=", url);
string = "";
// Use the FQDN and all directory parts for signature verification.
useparts = "110";
algorithm = 1;
duration = 60;
part_active = 0;
i = 0;
active = 0;
j = 0;
inactive_parts = [];
result = "";
url.split("/").forEach(function(part) {
if (debug_signurl) console.log("part=", part);
// if ( length($useparts) > $i )
if (useparts.length > i) {
// $part_active = substr( $useparts, $i++, 1 );
if (debug_signurl) console.log("i=", i);
part_active = useparts.substr(i, 1);
i = i + 1;
}
// if ($part_active)
if (debug_signurl) console.log("part_active=", part_active);
if (part_active == "1") {
// $string .= $part . "/";
string += (part + "/");
if (debug_signurl) console.log("string=", string);
} else {
// $inactive_parts[$j] = $part;
inactive_parts[j] = part;
}
j++;
});
// my $urlHasParams = index($string,"?");
urlHasParams = string.indexOf("?");
if (debug_signurl) console.log("urlHasParams=", urlHasParams);
// chop($string);
string = string.substring(0, string.length - 1);
// time() the numbers of seconds that have elapsed since January 1, 1970.
currentTime = Math.round(new Date().getTime() / 1000);
if (urlHasParams > 0) {
string += ("&E=" + (currentTime + duration) + "&A=" + algorithm + "&K=" + keyindex + "&P=" + useparts + "&S=");
} else {
string += ("?E=" + (currentTime + duration) + "&A=" + algorithm + "&K=" + keyindex + "&P=" + useparts + "&S=");
}
if (debug_signurl) console.log('Eric; Ready to hmac = ', string);
// $digest = hmac_sha1_hex( $string, $key );
var shaObj = new jsSHA('SHA-1', 'TEXT');
shaObj.setHMACKey(key, 'TEXT');
shaObj.update(string);
digest = shaObj.getHMAC("HEX");
// outputFormat must be HEX, B64, BYTES, or ARRAYBUFFER
if (urlHasParams == -1) {
qstring = string.split("?")[1];
return new Array(qstring + digest, string, digest);
} else {
return new Array("QQ", string, digest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment