generate static method

void generate(
  1. String input,
  2. String functionName
)

Implementation

static void generate(String input, String functionName) {
  String key;
  /// If input is an existing file, read its exact contents (multiline supported)
  final file = File(input);
  if (file.existsSync()) {
    key = file.readAsStringSync();
    print("📂 Loaded key from file: $input");
  } else {
    key = input;
    print("🔑 Using inline key: $input");
  }

  const mask = 137; // XOR mask
  final encoded = utf8.encode(key);
  final obfuscated = encoded.map((b) => b ^ mask).toList();

  /// Split into 4 byte chunks
  const chunkSize = 4;
  final chunks = <List<int>>[];
  for (var i = 0; i < obfuscated.length; i += chunkSize) {
    chunks.add(obfuscated.sublist(i, (i + chunkSize).clamp(0, obfuscated.length)));
  }

  // Path file target
  final secretFile = File("secretkey.dart");

  // If the file doesn't exist yet → create a skeleton class
  if (!secretFile.existsSync()) {
    secretFile.writeAsStringSync("""
import 'dart:convert';

/// Auto-generated by secret_key_secret_generator.
/// Never manually edit the code inside the fucntion in this file!
class SecretKey {
static const _mask = $mask;
}
""");
  }

  // Add a new method
  final method =
      """

static String $functionName() {
  const chunks = <List<int>>${jsonEncode(chunks)};
  final flat = chunks.expand((e) => e).map((b) => b ^ _mask).toList();
  return utf8.decode(flat);
}
""";

  // Append to SecretKey class
  var content = secretFile.readAsStringSync();

  // Check if function exist
  if (content.contains("static String $functionName()")) {
    print("⚠️ Function $functionName() already exists in secretkey.dart. Aborting.");
    return;
  }

  // add new method
  final classEndIndex = content.lastIndexOf("}");
  if (classEndIndex == -1) {
    throw Exception("Cannot find closing brace for SecretKey class.");
  }

  content = "${content.substring(0, classEndIndex)}$method\n}";

  secretFile.writeAsStringSync(content);

  print("✅ New obfuscated function $functionName() created in secretkey.dart (ROOT)");
}