What is the algorithm used to create the "Tiny links"

Hi,

I've seen this question: Is there a way to get a Confluence page's tiny link programmatically? and I have one in a similar vein.

What is the algorithm used to create the tiny links?

I'm assuming it's some kind of base 64/62 encoder of some sort? If anyone knows where in the source to look for the code, a path would be greatly appreciated.

Steve

4 answers

1 accepted

3 votes
Answer accepted
David Simpson Community Champion Sep 17, 2012

Check out the source of com.atlassian.confluence.pages.TinyUrl...

Base64.encodeBase64(longToByteArray(id));

id is the pageId.

I used the following functions in PHP based on the Steve's code:

function id2tinylink($id)
{
    $bytes = pack("L", (int)$id);  // convert the long to bytes
    $base64string=base64_encode($bytes);
    $base64string_escaped=strtr($base64string,'+/','_-');
    return rtrim($base64string_escaped, "A=");
}

function tinylink2id($tinylink){
    $base64string_encoded = str_pad($tinylink, 8, "A", STR_PAD_RIGHT);
    $base64string = strtr($base64string_encoded, '_-', '+/');
    $bytes = base64_decode($base64string);
    return unpack("L",$bytes)[1];
}

If anyone would like to emulate this functionality in PHP for whatever reason:

function tinylinkid($id)
{
	$bytes = pack("L", $id);  // convert the long to bytes
	$bytes = rtrim($bytes);  // trim any null characters
	$bytestring = "";
	$len = strlen($bytes); 
	for($i = 0; $i < $len; $i++) {
		$bytestring = $bytestring . $bytes[$i]; // build the byte string
	}
	$base64string = base64_encode($bytestring); // base64 encode the byte string
	$base64string = rtrim($base64string, "="); // trim any base64 padding characters
	return $base64string;
}

Thanks for David for the pointer.

Steve

Could you describe a method of decoding?

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 12, 2019 in Confluence

Confluence Admin Certification now $150 for Community Members

More and more people are building their careers with Atlassian, and we want you to be at the front of this wave! Important Dates Start the Certification Prep Course by 2 April 2019 Take your e...

1,610 views 4 13
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you