0

here is my code if anyone can help me this I would be grateful

Map<String, String> signRequest(String param) {
var method = "POST";
var uri = endpoint;
var secretKey = secretKey;
var accessKey = accessKey;
var region = awsRegion;
var service = serviceType;
var host = baseUrl;

var date = DateFormat("yyyyMMdd'T'HHmmss'Z'").format(DateTime.now().toUtc());
var date2 = DateFormat("yyyyMMdd").format(DateTime.now().toUtc());

var requestBody = utf8.encode(json.encode(param.toLowerCase()));
var hashedPayloads = sha256.convert(requestBody).toString().toLowerCase();

var canonicalUri = uri;
var canonicalQuerystring = "";
var canonicalHeaders =
    "content-type:application/json\nhost:$host\nx-amz-content-sha256:$hashedPayloads\nx-amz-date:$date\n";

var signedHeaders = "content-type;host;x-amz-content-sha256;x-amz-date";
var canonicalRequest =
    "$method\n$canonicalUri\n$canonicalQuerystring\n$canonicalHeaders\n$signedHeaders\n$hashedPayloads";

var credentialScope = "$date2/$region/$service/aws4_request";

var stringToSign =
    "${ApiConstants.hmacShaTypeString}\n$date\n$credentialScope\n${sha256.convert(utf8.encode(canonicalRequest))}";

var kSecret = "AWS4$secretKey";
var kDate = Hmac(sha256, utf8.encode(kSecret))
    .convert(utf8.encode(date2))
    .toString();
var kRegion = Hmac(sha256, utf8.encode(kDate))
    .convert(utf8.encode(region))
    .toString();
var kService = Hmac(sha256, utf8.encode(kRegion))
    .convert(utf8.encode(service))
    .toString();
var kSigning = Hmac(sha256, utf8.encode(kService))
    .convert(utf8.encode("aws4_request"))
    .toString();
var signature = Hmac(sha256, utf8.encode(kSigning))
    .convert(utf8.encode(stringToSign))
    .toString();

var authorizationHeader =
    "${ApiConstants.hmacShaTypeString} Credential=$accessKey/$credentialScope, SignedHeaders=$signedHeaders, Signature=$signature";


Map<String, String> headers = {
  "Content-Type": "application/json",
  "X-Amz-Date": date,
  "X-Amz-Content-Sha256": hashedPayloads,
  "Authorization": authorizationHeader
};
return headers;
}

Status code: 403

Got this message in response:

{"message":"The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.\n\nThe Canonical String for this request should have been"}

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
  • Does this answer your question? [Amazon S3 - How to fix 'The request signature we calculated does not match the signature' error?](https://stackoverflow.com/questions/30518899/amazon-s3-how-to-fix-the-request-signature-we-calculated-does-not-match-the-s) – Ahmed Ashour Feb 06 '23 at 11:57
  • that one is for php. i've code in laravel that is working perfectly fine but when I convert it into dart the hash is not matching – Muhammad Bilal Feb 06 '23 at 12:16

0 Answers0