computeResponse function

Map<String, String?> computeResponse(
  1. String method,
  2. String path,
  3. String body,
  4. String? algorithm,
  5. String? qop,
  6. String? opaque,
  7. String realm,
  8. String? cnonce,
  9. String? nonce,
  10. int nc,
  11. String username,
  12. String password,
)

Implementation

Map<String, String?> computeResponse(
  String method,
  String path,
  String body,
  String? algorithm,
  String? qop,
  String? opaque,
  String realm,
  String? cnonce,
  String? nonce,
  int nc,
  String username,
  String password,
) {
  var ret = <String, String?>{};

  algorithm ??= 'MD5';
  final ha1 = _computeHA1(realm, algorithm, username, password, nonce, cnonce);

  String ha2;

  if (algorithm.startsWith('MD5')) {
    if (qop == 'auth-int') {
      final bodyHash = md5Hash(body);
      final token2 = '$method:$path:$bodyHash';
      ha2 = md5Hash(token2);
    } else {
      // qop in [null, auth]
      final token2 = '$method:$path';
      ha2 = md5Hash(token2);
    }
  } else {
    if (qop == 'auth-int') {
      final bodyHash = sha256Hash(body);
      final token2 = '$method:$path:$bodyHash';
      ha2 = sha256Hash(token2);
    } else {
      // qop in [null, auth]
      final token2 = '$method:$path';
      ha2 = sha256Hash(token2);
    }
  }

  final nonceCount = _formatNonceCount(nc);
  ret['username'] = username;
  ret['realm'] = realm;
  ret['nonce'] = nonce;
  ret['uri'] = path;
  if (qop != null) {
    ret['qop'] = qop;
  }
  ret['nc'] = nonceCount;
  ret['cnonce'] = cnonce;
  if (opaque != null) {
    ret['opaque'] = opaque;
  }
  ret['algorithm'] = algorithm;

  if (algorithm.startsWith('MD5')) {
    if (qop == null) {
      final token3 = '$ha1:$nonce:$ha2';
      ret['response'] = md5Hash(token3);
    } else if (kQop.contains(qop)) {
      final token3 = '$ha1:$nonce:$nonceCount:$cnonce:$qop:$ha2';
      ret['response'] = md5Hash(token3);
    }
  } else {
    if (qop == null) {
      final token3 = '$ha1:$nonce:$ha2';
      ret['response'] = sha256Hash(token3);
    } else if (kQop.contains(qop)) {
      final token3 = '$ha1:$nonce:$nonceCount:$cnonce:$qop:$ha2';
      ret['response'] = sha256Hash(token3);
    }
  }

  return ret;
}