Secure Token

Secure tokens protect your content by generating a unique hash that prevents unauthorised access to your content.

Supported hash algorithms include HMAC-SHA256, HMAC-SHA1 and MD5

Main Functions

  • Secure tokens generate uniquely hashed links to your content, these links contain a set expiration time to protect your content.
  • 2XX response are only sent to users who request correctly hashed URLs within the time period that you've set in your code.
  • Requests without a hashed URL return a 403 response. Requests with invalid or expired hashed URLs also return 403s.

Setting up Secure Token Protection

Specifying the secure token type

When activating secure tokens in your account, you're presented with the option to choose either Parameter or Path.

If you're using tokenized URLs to secure live streams, it is very important that you use the Path type. Only this way is the CDN able to properly secure and cache your live content.

Setup and maintenance instructions

To set up secure tokens for the first time you should first navigate to the CDN Resource you'd like to enable them on. From the CDN Resource settings:

  1. Click the "Access Protection" tab, then click "Change settings".
  2. Enter a label, generate a token with our generator or enter your own, choose your preferred hash algorithm.
  3. If you would like to create more than one, just click the "+ Add secure token" button and repeat step 2. above. Once you're ready, click "Save changes".

Migrating tokens from MD5 to HMAC-SHA256 or HMAC-SHA1

To migrate secure tokens to a different hash algorithm, follow the steps listed above using the same token type (Path or Parameter) that is in use now. Then:

  1. Wait a minimum of 10 minutes for the changes to propagate to all PoPs.
  2. After at least 10 minutes has passed, update your application(s) to sign URLs with the new token and its hash algorithm.
  3. Monitor the Reports section or your own tools to ensure there's no increase in HTTP 403s. We recommend monitoring at least 30 minutes since the change was saved.
  4. After you're satisfied the change to the new token has been fully implemented, delete the old token.

If you'd only like to rotate your token and keep the same hash algorithm, you can follow the same steps listed above

Generating the Tokenized URLs

We've provided a sample PHP generator below. It is possible to create a similar generator in another programming language of your choice, it's not limited to PHP alone.

Parameter tokenization

To generate a Parameter-based HMAC-SHA256 or HMAC-SHA1 hashed URL:

<?php
/**
 * Create hash link CDN resource (HMAC-SHA256)
 */
function getSignedUrlParameter(
	string $cdnResourceUrl,
	string $filePath,
	string $secureToken,
	?int $expiryTimestamp = null
): string {
	// Add slash to start of file path if missing
	if ($filePath === '' || $filePath[0] !== '/') {
		$filePath = '/' . $filePath;
	}
	// Cut the query string from file path (e.g. "/file/video.mp4?autoplay=true" changes to "/file/video.mp4")
	if ($positionOfStartQuery = strpos($filePath, '?')) {
		$filePath = substr($filePath, 0, $positionOfStartQuery);
	}
	$message = $filePath;
	if ($expiryTimestamp) {
		$message = $expiryTimestamp . $message;
		$expiryTimestamp = ',' . $expiryTimestamp;
	}
	$hash = hash_hmac('sha256', $message, $secureToken, true);
	// Replace invalid URL query string characters +, / with valid characters -, _
	$invalidChars = ['+', '/'];
	$validChars = ['-', '_'];
	$finalHash = str_replace($invalidChars, $validChars, base64_encode($hash));
	return 'https://' . $cdnResourceUrl . $filePath . '?secure=' . $finalHash . $expiryTimestamp;
}
?>

To generate a Parameter-based MD5 hashed URL:

<?php

/**
 * Create hash link CDN resource
 */
function getSignedUrlParameter(
	string $cdnResourceUrl,
	string $filePath,
	string $secureToken,
	?int $expiryTimestamp = null
): string {
	// Add slash to start of file path if missing
	if ($filePath[0] !== '/') {
		$filePath = '/' . $filePath;
	}

	// Cut the query string from file path (e.g. "/file/video.mp4?autoplay=true" changes to "/file/video.mp4")
	if ($positionOfStartQuery = strpos($filePath, '?')) {
		$filePath = substr($filePath, 0, $positionOfStartQuery);
	}

	$hash = $filePath . $secureToken;

	if ($expiryTimestamp) {
		$hash = $expiryTimestamp . $hash;
		$expiryTimestamp = ',' . $expiryTimestamp;
	}

	// Replace invalid URL query string characters +, / with valid characters -, _
	$invalidChars = ['+', '/'];
	$validChars = ['-', '_'];
	$finalHash = str_replace($invalidChars, $validChars, base64_encode(md5($hash, true)));

	return 'https://' . $cdnResourceUrl . $filePath . '?secure=' . $finalHash . $expiryTimestamp;
}

?>

Usage example

<?php

echo getSignedUrlParameter('1234456789.rsc.cdn77.org', '/file/video.mp4', 'ykX1QNTRvp3tfSn8', 1389183132);

// https://1234456789.rsc.cdn77.org/file/video.mp4?secure=29QpicPWKD6RpuYMfC8LfA==,1389183132

?>

Path tokenization

To generate a Path-based hashed HMAC-SHA256 or HMAC-SHA1 URL:

<?php
/**
 * Create hash link Path CDN Resource (HMAC-SHA256)
 */
function getSignedUrlPath(
	string $cdnResourceUrl,
	string $filePath,
	string $secureToken,
	?int $expiryTimestamp = null
): string {
	// Add slash to start of file path if missing
	if ($filePath === '' || $filePath[0] !== '/') {
		$filePath = '/' . $filePath;
	}
	// Cut the query string from file path
	if ($positionOfStartQuery = strpos($filePath, '?')) {
		$filePath = substr($filePath, 0, $positionOfStartQuery);
	}
	// Because of hls/dash, anything included after the last slash (e.g. playlist/{chunk}) shouldn't be part of the path string,
	// for which we generate the secure token. Because of that, everything included after the last slash is stripped.
	// For a file in the root (e.g. "/video.mp4") the stripped path is an empty string.
	$strippedPath = substr($filePath, 0, strrpos($filePath, '/'));
	$message = $strippedPath;
	if ($expiryTimestamp) {
		$message = $expiryTimestamp . $message;
		$expiryTimestamp = ',' . $expiryTimestamp;
	}
	$hash = hash_hmac('sha256', $message, $secureToken, true);
	// Replace invalid URL query string characters +, / with valid characters -, _
	$invalidChars = ['+', '/'];
	$validChars = ['-', '_'];
	$finalHash = str_replace($invalidChars, $validChars, base64_encode($hash));
	// The URL is however, intentionally returned with the previously stripped parts (eg. playlist/{chunk}..)
	return 'https://' . $cdnResourceUrl . '/' . $finalHash . $expiryTimestamp . $filePath;
}
?>

To generate a Path-based hashed MD5 URL:

<?php

/**
 * Create hash link Path CDN Resource
 */
function getSignedUrlPath(
	string $cdnResourceUrl,
	string $filePath,
	string $secureToken,
	?int $expiryTimestamp = null
): string {
	// Because of hls/dash, anything included after the last slash (e.g. playlist/{chunk}) shouldn't be part of the path string,
	// for which we generate the secure token. Because of that, everything included after the last slash is stripped.
	$strippedPath = substr($filePath, 0, strrpos($filePath, '/'));

	// Add slash to start of stripped path if missing
	if ($strippedPath[0] !== '/') {
		$strippedPath = '/' . $strippedPath;
		$filePath = '/' . $filePath;
	}

	// Cut the query string from stripped path
	if ($positionOfStartQuery = strpos($strippedPath, '?')) {
		$filePath = substr($strippedPath, 0, $positionOfStartQuery);
	}

	$hash = $strippedPath . $secureToken;

	if ($expiryTimestamp) {
		$hash = $expiryTimestamp . $hash;
		$expiryTimestamp = ',' . $expiryTimestamp;
	}

	// Replace invalid URL query string characters +, / with valid characters -, _
	$invalidChars = ['+', '/'];
	$validChars = ['-', '_'];
	$finalHash = str_replace($invalidChars, $validChars, base64_encode(md5($hash, true)));

	// The URL is however, intentionally returned with the previously stripped parts (eg. playlist/{chunk}..)
	return 'https://' . $cdnResourceUrl . '/' . $finalHash . $expiryTimestamp . $filePath;
}

?>

Usage example

<?php

echo getSignedUrlPath('1234456789.rsc.cdn77.org', '/file/playlist/d.m3u8', 'ykX1QNTRvp3tfSn8', 1389183132);

// https://1234456789.rsc.cdn77.org/z--FA_CsNsR2TOV2eg9q4w==,1389183132/file/playlist/d.m3u8

?>

Generate signed URL

Type
Expiration

Content will be provided only until a specified time.

Updated on 4th December, 2026