<?php function url_safe_base64_encode($value) { $encoded = base64_encode($value); // replace unsafe characters +, = and / with // the safe characters -, _ and ~ return str_replace(array('+', '=', '/'), array('-', '_', '~'), $encoded); } function rsa_sha1_sign($policy) { $priv_key = file_get_contents("/Users/joelsaltzman/Desktop/privatekey"); $pkeyid = openssl_get_privatekey($priv_key); openssl_sign($policy, $signature, $pkeyid); openssl_free_key($pkeyid); return $signature; } $signature = rsa_sha1_sign("testing"); $encodedSignature = url_safe_base64_encode($signature); echo $encodedSignature; //correct results
function create_signed_url($asset_path, $private_key_filename, $key_pair_id, $expires) { // Build the policy. $canned_policy = '{"Statement":[{"Resource":"' . $asset_path . '","Condition":{"DateLessThan":{"AWS:EpochTime":' . $expires . '}}}]}'; /*$canned_policy = ' { "Id": "Policy1440586376040", "Version": "2012-10-17", "Statement": [ { "Sid": "Stmt1440586363543", "Action": [ "s3:GetObject" ], "Effect": "Allow", "Resource": "arn:aws:s3:::direct2consumer/*", "Principal": { "CanonicalUser": [ "8a09356196995b1dc7ea047cf369b33d7ecdeb9c0e65fbdebcd3f52d17c2979c6fc14c8e7f7afa5f39d8ed644c677480" ] } } ] }'; */ // Sign the policy. $signature = rsa_sha1_sign($canned_policy, $private_key_filename); // Make the signature contains only characters that // can be included in a URL. $encoded_signature = url_safe_base64_encode($signature); // Combine the above into a properly formed URL name return $asset_path . '?Expires=' . $expires . '&Signature=' . $encoded_signature . '&Key-Pair-Id=' . $key_pair_id; }
function cloudFrontCannedPolicyURLSign($mediaFilePath) { // this policy is well known by CloudFront, but you still need to sign it, // since it contains your parameters //Location of the CLoudFront key on the server: $private_key_filename = '/var/AWSKeys/CloudFront/pk-APKAI3O4WVSJRO2O3K4Q.pem'; //The CloudFront key pair ID. NOTE CLOUDFRONT IS SEPERATE TO STANDARD IAM STUFF, WHY? WHO KNOWS. $key_pair_id = 'APKAI3O4WVSJRO2O3K4Q'; //60 Second live time on the links that are generated. $expires = time() + 60; $canned_policy = '{"Statement":[{"Resource":"' . $mediaFilePath . '","Condition":{"DateLessThan":{"AWS:EpochTime":' . $expires . '}}}]}'; // sign the canned policy $signature = rsa_sha1_sign($canned_policy, $private_key_filename); // make the signature safe to be included in a url $encoded_signature = url_safe_base64_encode($signature); // combine the above into a stream name $stream_name = create_stream_name($mediaFilePath, null, $encoded_signature, $key_pair_id, $expires); // url-encode the query string characters to work around a flash player bug //return 'https://cdn.ro5635.co.uk/'. encode_query_params($stream_name); return $stream_name; }