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

This widget could not be displayed.
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.

This widget could not be displayed.

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];
}

This widget could not be displayed.

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

This widget could not be displayed.

Could you describe a method of decoding?

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Monday in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

477 views 6 6
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