function generateHashLink($service_domain, $file_path, $url_signing_key ,$expiry_timestamp = NULL){
// + and / are some of represented chars of based64 encoding (8 bits)
// + is 62 and / is 63 and these char should be replaced by other predefined chars
$search_chars = array('+','/');
$replace_chars = array('-', '_');
if($file_path[0] != '/'){
$file_path = "/{$file_path}";
}
if($pos = strpos($file_path, '?')){
$file_path = substr($file_path, 0, $pos);
}
$hash_string = $file_path.$url_signing_key;
if($expiry_timestamp){
$hash_string = $expiry_timestamp.$hash_string;
$expiry_timestamp = ",{$expiry_timestamp}";
}
return "http://{$service_domain}{$file_path}?secure=".
str_replace($search_chars, $replace_chars, base64_encode(md5($hash_string, TRUE))).
$expiry_timestamp;
}