|
1 | 1 | import 'dart:convert'; |
2 | 2 | import 'dart:io'; |
| 3 | +import 'dart:math'; |
| 4 | +import 'dart:typed_data'; |
3 | 5 | import 'package:archive/archive.dart'; |
| 6 | +import 'package:cryptography/cryptography.dart'; |
4 | 7 | import 'package:hive/hive.dart'; |
5 | 8 | import 'package:path_provider/path_provider.dart'; |
6 | 9 | import 'package:share_plus/share_plus.dart'; |
@@ -89,6 +92,154 @@ class BackupService { |
89 | 92 | ); |
90 | 93 | } |
91 | 94 |
|
| 95 | + /// Erstellt ein passphrase-verschlüsseltes Backup (AES-256-GCM + PBKDF2-SHA256) |
| 96 | + Future<File> createEncryptedBackup(String passphrase) async { |
| 97 | + final archive = Archive(); |
| 98 | + |
| 99 | + final metadata = { |
| 100 | + 'version': backupVersion, |
| 101 | + 'createdAt': DateTime.now().toIso8601String(), |
| 102 | + 'appVersion': '0.1.0', |
| 103 | + }; |
| 104 | + archive.addFile(_createJsonFile('metadata.json', metadata)); |
| 105 | + |
| 106 | + final workBox = Hive.box<WorkEntry>('work'); |
| 107 | + archive.addFile(_createJsonFile('work_entries.json', |
| 108 | + workBox.values.map(_workEntryToJson).toList())); |
| 109 | + |
| 110 | + final vacationBox = Hive.box<Vacation>('vacations'); |
| 111 | + archive.addFile(_createJsonFile('vacations.json', |
| 112 | + vacationBox.values.map(_vacationToJson).toList())); |
| 113 | + |
| 114 | + final quotaBox = Hive.box<VacationQuota>('vacation_quotas'); |
| 115 | + archive.addFile(_createJsonFile('vacation_quotas.json', |
| 116 | + quotaBox.values.map(_quotaToJson).toList())); |
| 117 | + |
| 118 | + final settingsBox = Hive.box<Settings>('settings'); |
| 119 | + if (settingsBox.isNotEmpty) { |
| 120 | + archive.addFile(_createJsonFile('settings.json', |
| 121 | + _settingsToJson(settingsBox.getAt(0)!))); |
| 122 | + } |
| 123 | + |
| 124 | + final projectBox = Hive.box<Project>('projects'); |
| 125 | + archive.addFile(_createJsonFile('projects.json', |
| 126 | + projectBox.values.map(_projectToJson).toList())); |
| 127 | + |
| 128 | + final periodsBox = Hive.box<WeeklyHoursPeriod>('weekly_hours_periods'); |
| 129 | + archive.addFile(_createJsonFile('weekly_hours_periods.json', |
| 130 | + periodsBox.values.map(_periodToJson).toList())); |
| 131 | + |
| 132 | + final zonesBox = Hive.box<GeofenceZone>('geofence_zones'); |
| 133 | + archive.addFile(_createJsonFile('geofence_zones.json', |
| 134 | + zonesBox.values.map(_zoneToJson).toList())); |
| 135 | + |
| 136 | + final zipData = ZipEncoder().encode(archive); |
| 137 | + if (zipData == null) throw Exception('Failed to create ZIP archive'); |
| 138 | + |
| 139 | + // PBKDF2-SHA256 key derivation |
| 140 | + final rng = Random.secure(); |
| 141 | + final salt = Uint8List.fromList(List.generate(16, (_) => rng.nextInt(256))); |
| 142 | + final nonce = Uint8List.fromList(List.generate(12, (_) => rng.nextInt(256))); |
| 143 | + |
| 144 | + const kdfIterations = 200000; |
| 145 | + final pbkdf2 = Pbkdf2( |
| 146 | + macAlgorithm: Hmac.sha256(), |
| 147 | + iterations: kdfIterations, |
| 148 | + bits: 256, |
| 149 | + ); |
| 150 | + final secretKey = await pbkdf2.deriveKey( |
| 151 | + secretKey: SecretKey(utf8.encode(passphrase)), |
| 152 | + nonce: salt, |
| 153 | + ); |
| 154 | + |
| 155 | + // AES-256-GCM encryption |
| 156 | + final algorithm = AesGcm.with256bits(); |
| 157 | + final secretBox = await algorithm.encrypt( |
| 158 | + zipData, |
| 159 | + secretKey: secretKey, |
| 160 | + nonce: nonce, |
| 161 | + ); |
| 162 | + |
| 163 | + // JSON envelope |
| 164 | + final envelope = jsonEncode({ |
| 165 | + 'v': 1, |
| 166 | + 'alg': 'AES-256-GCM', |
| 167 | + 'kdf': 'PBKDF2-SHA256', |
| 168 | + 'iter': kdfIterations, |
| 169 | + 'salt': base64Encode(salt), |
| 170 | + 'nonce': base64Encode(secretBox.nonce), |
| 171 | + 'mac': base64Encode(secretBox.mac.bytes), |
| 172 | + 'data': base64Encode(secretBox.cipherText), |
| 173 | + }); |
| 174 | + |
| 175 | + final dir = await getApplicationDocumentsDirectory(); |
| 176 | + final timestamp = DateTime.now().toIso8601String().replaceAll(':', '-').split('.').first; |
| 177 | + final file = File('${dir.path}/vibedtracker_backup_$timestamp.enc'); |
| 178 | + await file.writeAsString(envelope); |
| 179 | + return file; |
| 180 | + } |
| 181 | + |
| 182 | + /// Teilt das verschlüsselte Backup über Share-Dialog |
| 183 | + Future<void> shareEncryptedBackup(String passphrase) async { |
| 184 | + final file = await createEncryptedBackup(passphrase); |
| 185 | + await Share.shareXFiles( |
| 186 | + [XFile(file.path)], |
| 187 | + subject: 'VibedTracker Backup (verschlüsselt)', |
| 188 | + text: 'VibedTracker Daten-Backup (verschlüsselt)', |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + /// Stellt Daten aus einem verschlüsselten Backup wieder her |
| 193 | + Future<BackupRestoreResult> restoreFromEncryptedFile( |
| 194 | + File encFile, String passphrase) async { |
| 195 | + try { |
| 196 | + final envelope = jsonDecode(await encFile.readAsString()) as Map<String, dynamic>; |
| 197 | + |
| 198 | + if (envelope['v'] != 1 || envelope['alg'] != 'AES-256-GCM') { |
| 199 | + return BackupRestoreResult( |
| 200 | + success: false, error: 'Unbekanntes Backup-Format'); |
| 201 | + } |
| 202 | + |
| 203 | + final salt = base64Decode(envelope['salt'] as String); |
| 204 | + final nonce = base64Decode(envelope['nonce'] as String); |
| 205 | + final mac = base64Decode(envelope['mac'] as String); |
| 206 | + final cipherText = base64Decode(envelope['data'] as String); |
| 207 | + final iterations = (envelope['iter'] as int?) ?? 200000; |
| 208 | + |
| 209 | + final pbkdf2 = Pbkdf2( |
| 210 | + macAlgorithm: Hmac.sha256(), |
| 211 | + iterations: iterations, |
| 212 | + bits: 256, |
| 213 | + ); |
| 214 | + final secretKey = await pbkdf2.deriveKey( |
| 215 | + secretKey: SecretKey(utf8.encode(passphrase)), |
| 216 | + nonce: salt, |
| 217 | + ); |
| 218 | + |
| 219 | + final algorithm = AesGcm.with256bits(); |
| 220 | + final List<int> zipData; |
| 221 | + try { |
| 222 | + zipData = await algorithm.decrypt( |
| 223 | + SecretBox(cipherText, nonce: nonce, mac: Mac(mac)), |
| 224 | + secretKey: secretKey, |
| 225 | + ); |
| 226 | + } on SecretBoxAuthenticationError { |
| 227 | + return BackupRestoreResult( |
| 228 | + success: false, error: 'Falsches Passwort oder beschädigtes Backup'); |
| 229 | + } |
| 230 | + |
| 231 | + // Temporäre ZIP-Datei schreiben und restore aufrufen |
| 232 | + final dir = await getApplicationDocumentsDirectory(); |
| 233 | + final tmpFile = File('${dir.path}/_restore_tmp.zip'); |
| 234 | + await tmpFile.writeAsBytes(zipData); |
| 235 | + final result = await restoreFromFile(tmpFile); |
| 236 | + await tmpFile.delete(); |
| 237 | + return result; |
| 238 | + } catch (e) { |
| 239 | + return BackupRestoreResult(success: false, error: e.toString()); |
| 240 | + } |
| 241 | + } |
| 242 | + |
92 | 243 | /// Stellt Daten aus einem Backup wieder her |
93 | 244 | Future<BackupRestoreResult> restoreFromFile(File zipFile) async { |
94 | 245 | try { |
|
0 commit comments