MySQLPacketHandshakeResponse41.createWithCachingSha2Password constructor

MySQLPacketHandshakeResponse41.createWithCachingSha2Password({
  1. required String username,
  2. required String password,
  3. required MySQLPacketInitialHandshake initialHandshakePayload,
})

Cria uma resposta de handshake utilizando o método caching_sha2_password para autenticação.

  • username: Nome do usuário.
  • password: Senha do usuário.
  • initialHandshakePayload: Pacote do handshake inicial enviado pelo servidor.

O desafio (challenge) é construído de forma similar, mas a resposta de autenticação é calculada utilizando a função SHA256.

Implementation

factory MySQLPacketHandshakeResponse41.createWithCachingSha2Password({
  required String username,
  required String password,
  required MySQLPacketInitialHandshake initialHandshakePayload,
}) {
  // Concatena a parte 1 do desafio com os 12 primeiros bytes da parte 2
  final challenge = initialHandshakePayload.authPluginDataPart1 +
      initialHandshakePayload.authPluginDataPart2!.sublist(0, 12);

  // Verifica se o desafio possui 20 bytes
  assert(challenge.length == 20);

  // Converte a senha para bytes (UTF-8)
  final passwordBytes = utf8.encode(password);

  // Calcula a resposta de autenticação utilizando SHA256:
  // authResponse = xor(sha256(password), sha256(sha256(sha256(password)) + challenge))
  final authData = xor(
    sha256(passwordBytes),
    sha256(sha256(sha256(passwordBytes)) + challenge),
  );

  return MySQLPacketHandshakeResponse41(
    capabilityFlags: _supportedCapabitilies,
    maxPacketSize: 50 * 1024 * 1024,
    authPluginName: initialHandshakePayload.authPluginName!,
    characterSet: initialHandshakePayload.charset,
    authResponse: authData,
    username: username,
  );
}