52 lines
1.7 KiB
Dart
52 lines
1.7 KiB
Dart
// Encrypts tool/club_data.json into assets/club.enc using the club code.
|
|
//
|
|
// dart run tool/encrypt_club_data.dart
|
|
//
|
|
// The code is asked interactively (not echoed, not saved in shell history).
|
|
// tool/club_data.json holds the plain contacts and is gitignored — keep it private.
|
|
// Re-run after editing contacts or to change the club code, then rebuild the app.
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'package:sdk_kovinar/services/club_crypto.dart';
|
|
|
|
const _input = 'tool/club_data.json';
|
|
const _output = 'assets/club.enc';
|
|
|
|
Future<void> main() async {
|
|
final source = File(_input);
|
|
if (!source.existsSync()) {
|
|
stderr.writeln('Missing $_input');
|
|
exit(1);
|
|
}
|
|
final plaintext = source.readAsStringSync();
|
|
final contacts = (jsonDecode(plaintext) as Map<String, dynamic>)['contacts'] as List;
|
|
|
|
final code = _ask('Club code: ');
|
|
if (ClubCrypto.normalize(code).length < 8) {
|
|
stderr.writeln('Use at least 8 characters — short codes can be guessed by brute force.');
|
|
exit(1);
|
|
}
|
|
if (_ask('Repeat club code: ') != code) {
|
|
stderr.writeln('Codes do not match.');
|
|
exit(1);
|
|
}
|
|
|
|
final encrypted = await ClubCrypto.encrypt(plaintext, code);
|
|
if (await ClubCrypto.decrypt(encrypted, code) != plaintext) {
|
|
stderr.writeln('Self-check failed, nothing written.');
|
|
exit(1);
|
|
}
|
|
File(_output).writeAsStringSync(encrypted);
|
|
stdout.writeln('Wrote $_output with ${contacts.length} contacts.');
|
|
}
|
|
|
|
String _ask(String prompt) {
|
|
stdout.write(prompt);
|
|
final terminal = stdin.hasTerminal;
|
|
if (terminal) stdin.echoMode = false;
|
|
final value = stdin.readLineSync() ?? '';
|
|
if (terminal) stdin.echoMode = true;
|
|
stdout.writeln();
|
|
return value;
|
|
}
|