installId function

String installId(
  1. String dataDir
)

A random id created once per install in dataDir; unlike a hash of the path, it reveals nothing about the user or the machine.

Implementation

String installId(String dataDir) {
  final file = io.File('$dataDir/install-id');
  try {
    final existing = file.readAsStringSync().trim();
    if (RegExp(r'^[0-9a-f]{24}$').hasMatch(existing)) return existing;
  } on Object {
    // Not created yet.
  }
  final rnd = Random.secure();
  final id = [for (var i = 0; i < 12; i++) rnd.nextInt(256).toRadixString(16).padLeft(2, '0')].join();
  try {
    io.Directory(dataDir).createSync(recursive: true);
    file.writeAsStringSync(id);
  } on Object {
    // Unwritable data dir: the id lives for this run only.
  }
  return id;
}