I am trying to create a POST Policy and sign it using amazon access key, but I am not able to create the policy correctly. The steps for creating the signed policy are :
Step 1. Create a policy using UTF-8 encoding.
Step 2. Convert the UTF-8-encoded policy to Base64. The result is the string to sign.
Step 3. Create the signature as an HMAC-SHA256 hash of the string to sign. You will provide the signing key as key to the hash function.
Step 4. Encode the signature by using Base64.
The trick for testing :
I am following this document :
They have provided a base64 encoded policy, and using this policy, an encoded signature. I am reverse engineering these inputs to verify my program.
For the step 1 and 2:
I took an example base64 encoded string from Amazon tutorial and decoded it here
https://www.base64decode.org/. I used that JSON-Policy in my program to re-encode it in base64. The string I am receiving is different from the one I decoded initially.
package main
import "fmt"
import "encoding/base64"
func main() {
bytePolicy := []byte(`{ "expiration": "2013-08-07T12:00:00.000Z",
"conditions": [
{"bucket": "examplebucket"},
["starts-with", "$key", "user/user1/"],
{"acl": "public-read"},
["starts-with", "$Content-Type", "image/"],
{"x-amz-meta-uuid": "14365123651274"},
["starts-with", "$x-amz-meta-tag", ""],
{"x-amz-credential": "AKIAIOSFODNN7EXAMPLE/20130806/us-east-1/s3/aws4_request"},
{"x-amz-algorithm": "AWS4-HMAC-SHA256"},
{"x-amz-date": "20130806T000000Z" }
]
}`)
fmt.Println(base64.StdEncoding.EncodeToString(bytePolicy))
}
As, the JSON-based text is UTF-8 encoded, so my policy is getting created using UTF-8 encoding(or is it?). So, what am I missing here? Why my base64 encoded string is different from the one I used for encoding itself?
For the Step 3 and 4,
I used the example base64 provided in amazon document(which is correct) and signed it using the method provided here. Now, the answer which I am getting should match the signature already provided in amazon document. Golang Play - http://play.golang.org/p/Gh0igvyOzv These steps are independent from steps 1 and 2, for my verification of the program. Am I doing some mistake while signing the base64 encoded policy or while creating the signing key itself?