Skip to content

Instantly share code, notes, and snippets.

@cannapages
Created March 11, 2018 22:30
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 cannapages/6658c3cf96b3bc6ca5e2428fd904ff0a to your computer and use it in GitHub Desktop.
Save cannapages/6658c3cf96b3bc6ca5e2428fd904ff0a to your computer and use it in GitHub Desktop.
Digital Ocean Spaces Signing Request
require 'openssl'
require 'base64'
require 'digest'
SECRET_KEY = "9qVrrCk/9OtL5tD9fLASOtFQvbDo74oRh6JUeb0zprQ"
ACCESS_KEY = "PXCXJJK6N7T3S4HSDKQH"
request_timestamp = Time.now.utc
canonicalRequest = ""
canonicalRequest += "GET\n"
canonicalRequest += "/\n"
canonicalRequest += "\n"
canonicalRequest += "host:cp-space-test.nyc3.digitaloceanspaces.com\n"
canonicalRequest += "x-amz-content-sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
canonicalRequest += "x-amz-date:#{request_timestamp.strftime("%Y%m%dT%H%M%SZ")}\n"
canonicalRequest += "\n"
canonicalRequest += "host;x-amz-content-sha256;x-amz-date\n"
canonicalRequest += "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
sha_of_canonicalRequest = (Digest::SHA256::new << canonicalRequest).hexdigest
stringToSign = "AWS4-HMAC-SHA256" + "\n"
stringToSign += request_timestamp.strftime("%F") + "\n"
stringToSign += request_timestamp.strftime("%Y%m%d") + "/nyc3/s3/aws4_request\n"
stringToSign += sha_of_canonicalRequest
dateKey = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), "AWS4#{SECRET_KEY}", request_timestamp.strftime("%Y%m%d"))
dateRegionKey = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), dateKey, "nyc3")
dateRegionServiceKey = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), dateRegionKey, "s3")
signingKey = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), dateRegionServiceKey, "aws4_request")
signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), signingKey, stringToSign)
puts "Request:"
puts
puts "GET /"
puts "Host: cp-space-test.nyc3.digitaloceanspaces.com"
puts "X-Amz-Content-Sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
puts "X-Amz-Date: #{request_timestamp.strftime("%Y%m%dT%H%M%SZ")}"
puts "Authorization: AWS4-HMAC-SHA256 Credential=#{ACCESS_KEY}/#{request_timestamp.strftime("%Y%m%d")}/nyc3/s3/aws4_request,SignedHeaders=host;x-amz-acl;x-amz-content-sha256;x-amz-date,Signature=#{signature}"
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment