getSign method

String getSign(
  1. String method,
  2. String key, {
  3. Map<String, String?> headers = const {},
  4. Map<String, String?> params = const {},
  5. DateTime? signTime,
})
inherited

η”Ÿζˆη­Ύε

Implementation

String getSign(String method, String key,
    {Map<String, String?> headers = const {},
    Map<String, String?> params = const {},
    DateTime? signTime}) {
  if (_config.anonymous) {
    return "";
  } else {
    signTime = signTime ?? DateTime.now();
    int startSignTime = signTime.millisecondsSinceEpoch ~/ 1000 - 60;
    int stopSignTime = signTime.millisecondsSinceEpoch ~/ 1000 + 120;
    String keyTime = "$startSignTime;$stopSignTime";
    cosLog("keyTime=$keyTime");
    String signKey = hmacSha1(keyTime, _config.secretKey);
    cosLog("signKey=$signKey");

    var lap = getListAndParameters(params);
    String urlParamList = lap[0];
    String httpParameters = lap[1];
    cosLog("urlParamList=$urlParamList");
    cosLog("httpParameters=$httpParameters");

    lap = getListAndParameters(filterHeaders(headers));
    String headerList = lap[0];
    String httpHeaders = lap[1];
    cosLog("headerList=$headerList");
    cosLog("httpHeaders=$httpHeaders");

    String httpString =
        "${method.toLowerCase()}\n$key\n$httpParameters\n$httpHeaders\n";
    cosLog("httpString=$httpString");
    String stringToSign =
        "sha1\n$keyTime\n${hex.encode(sha1.convert(httpString.codeUnits).bytes)}\n";
    cosLog("stringToSign=$stringToSign");
    String signature = hmacSha1(stringToSign, signKey);
    cosLog("signature=$signature");
    String res =
        "q-sign-algorithm=sha1&q-ak=${_config.secretId}&q-sign-time=$keyTime&q-key-time=$keyTime&q-header-list=$headerList&q-url-param-list=$urlParamList&q-signature=$signature";
    cosLog("Authorization=$res");
    return res;
  }
}