Created
October 5, 2022 05:24
-
-
Save kzkn/0e93397da7bf90cf0c9e42c02637b93e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'openssl' | |
require 'json' | |
require 'base64' | |
key_paid_id = 'CLOUD_FRONT_PUBLIC_KEY_ID' | |
url = 'https://CLOUD_FRONT_DOMAIN/path/to/contents' | |
expires_at_epoch = (Time.new + 3600).to_i | |
policy = { | |
"Statement" => [ | |
{ | |
"Resource" => url, | |
"Condition" => { | |
"DateLessThan" => { | |
"AWS:EpochTime" => expires_at_epoch | |
} | |
} | |
} | |
] | |
} | |
policy_text = JSON.dump(policy) | |
key = OpenSSL::PKey.read(File.open('private_key.pem')) | |
sign = key.sign('SHA1', policy_text) | |
signature = Base64.strict_encode64(sign).tr('+=/', '-_~') | |
puts "#{url}?Expires=#{expires_at_epoch}&Signature=#{signature}&Key-Pair-Id=#{key_paid_id}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment