extractDebugSha256 function

Future<String?> extractDebugSha256()

Extract debug SHA256 fingerprint from debug keystore

Implementation

Future<String?> extractDebugSha256() async {
  // Try default location first
  final homeDir =
      Platform.environment['HOME'] ?? Platform.environment['USERPROFILE'];
  if (homeDir == null) return null;

  final debugKeystorePath = '$homeDir/.android/debug.keystore';
  final debugKeystore = File(debugKeystorePath);

  if (!debugKeystore.existsSync()) {
    return null;
  }

  try {
    final result = await Process.run(
      'keytool',
      [
        '-list',
        '-v',
        '-keystore',
        debugKeystorePath,
        '-alias',
        'androiddebugkey',
        '-storepass',
        'android',
        '-keypass',
        'android',
      ],
    ).timeout(
      const Duration(seconds: 3),
      onTimeout: () {
        throw TimeoutException('keytool command timed out');
      },
    );

    if (result.exitCode == 0) {
      final output = result.stdout.toString();
      final regex = RegExp(r'SHA256:\s*([A-F0-9:]+)');
      final match = regex.firstMatch(output);

      if (match != null && match.groupCount >= 1) {
        return match.group(1);
      }
    }
  } catch (e) {
    // keytool not available or error occurred
  }

  return null;
}