Skip to content

Instantly share code, notes, and snippets.

@samsonradu
Created August 22, 2017 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsonradu/9c8e6f96e627e4247b88f1fec2a91771 to your computer and use it in GitHub Desktop.
Save samsonradu/9c8e6f96e627e4247b88f1fec2a91771 to your computer and use it in GitHub Desktop.
LeaseWeb CDN
<?php
function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
function aes_encrypt($data, $aes_key, $sha_key) {
// apply PKCS#7 padding
$pad = 16-strlen($data)%16;
$data .= str_repeat(chr($pad),$pad);
// generate random IV
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM);
// apply AES-256-CBC
$crypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $aes_key, $data, MCRYPT_MODE_CBC, $iv);
// add SHA-256 HMAC
$sha = hash_hmac('sha256', $iv.$crypt, $sha_key, true);
// return everything
return $iv.$crypt.$sha;
}
$aes_key = pack('H*','abcdefghijklmnopqrstuvwqxyz0123456789');
$sha_key = pack('H*','abcdefghijklmnopqrstuvwqxyz0123456789');
$scheme = "http"; // http or https
$cname = 'your.cname.example.com'; // This is the CNAME of the CDN Zone.
$path = '/path/to/file.mp4'; // This is the path/file that is served to the visitor.
$expire = time()+3600; // At which point in time the file should expire. time() + x; would be the usual usage.
$remote_address="0.0.0.0/0"; // ipv4 or ipv6 network range that is allowed to access url.
$transfer_rate = "0"; // the maximum speed per connection at which an individual visitor can download.
$burst_size = "0"; // the data that can be downloaded before the transfer rate applies.
$args = "path=$path&expire=$expire&ip=$remote_address&ri=$burst_size&rs=$transfer_rate";
$token = base64url_encode(aes_encrypt($args, $aes_key, $sha_key));
$secret_url_final = "$scheme://$cname$path?key=$token";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment