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
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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jul 10, 2018 in Confluence

We want to see the templates you've created in Confluence!

Hi Community, Jessica here from the Confluence Product Marketing team!  July’s community challenge is all about sharing pictures  — and as an extension of our first post on what ...

742 views 21 12
Join discussion

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