Skip to content

Instantly share code, notes, and snippets.

@Leigh-
Created May 1, 2016 18:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leigh-/cf0dd4aee9d8259f08d299bff3cbb605 to your computer and use it in GitHub Desktop.
Save Leigh-/cf0dd4aee9d8259f08d299bff3cbb605 to your computer and use it in GitHub Desktop.
ColdFusion: AWS Task 2: Create a String to Sign for Signature Version 4
<!---
CFML translation of Amazon Web Services Example - Task 2:
http://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html
--->
<h1>Task 2: Create a String to Sign for Signature Version 4</h1>
<div>
<strong>Structure of string to sign</strong>
<pre>StringToSign = Algorithm + '\n' +
RequestDate + '\n' +
CredentialScope + '\n' +
HashedCanonicalRequest
</pre>
</div>
<div>
<strong>Example string to sign</strong>
<pre>
AWS4-HMAC-SHA256
20150830T123600Z
20150830/us-east-1/iam/aws4_request
f536975d06c0309214f805bb90ccff089219ecd68b2577efef23edd43b7e1a59
</pre>
</div>
<cfscript>
canonicalRequest = "";
/*
STEP 1: Start with the algorithm designation, followed by a newline character.
*/
// Hashing algorithm used to calculate digest in canonical request.
// For SHA256, AWS4-HMAC-SHA256 is the algorithm.
algorithm = "AWS4-HMAC-SHA256"& chr(10);
writeOutput("<br>algorithm: <code>"& algorithm &"</code>");
/*
STEP 2: Append the request date value, followed by a newline character.
*/
// For the example, use the sample date and time instead of now() in UTC
sampleDateTime = createDateTime(2015,8,30,12,36,0);
// Must be in ISO8601 format, ie YYYYMMDD'T'HHMMSS'Z' and match the value used in any previous steps.
requestDate = dateFormat(sampleDateTime, "YYYYMMDD") &"T"& timeFormat(sampleDateTime, "HHnnSS") &"Z"& chr(10);
writeOutput("<br>requestDate: <code>"& requestDate &"</code>");
/*
STEP 3: Append the credential scope value, followed by a newline character.
*/
// String including the date (only), region targeted, name of service requested, and termination string ("aws4_request")
// in lowercase characters. The region and service name strings must be UTF-8 encoded.
// Format: theDate/theRegionName/theServiceName/aws4_request
credentialScope = dateFormat(sampleDateTime, "YYYYMMDD") &"/us-east-1/iam/aws4_request"& chr(10);
writeOutput("<br>credentialScope: <code>"& credentialScope &"</code>");
/*
STEP 4: Append the hash of the canonical request that you created in
Task 1: Create a Canonical Request for Signature Version 4.
*/
// Copying expected Hash value generated in Task 1
hashOfCanonicalRequest = "f536975d06c0309214f805bb90ccff089219ecd68b2577efef23edd43b7e1a59";
writeOutput("<br>hashOfCanonicalRequest: <code>"& hashOfCanonicalRequest &"</code>");
stringToSign = algorithm & requestDate & credentialScope & hashOfCanonicalRequest;
writeOutput("<br>stringToSign: <pre>"& stringToSign &"</pre>");
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment