You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
loc-chain-app/loc_chain_app/lib/util/keyfile_manager.dart

51 lines
1.3 KiB

import 'package:openpgp/model/bridge.pb.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
class KeyFileManager {
static KeyPair keyPair = KeyPair();
static Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
static Future<File> get _pubKeyFile async {
final path = await _localPath;
return File('$path/rsa.pub');
}
static Future<File> get _privKeyFile async {
final path = await _localPath;
return File('$path/rsa');
}
static Future<KeyPair> readKeyPair() async {
if (keyPair.hasPublicKey() && keyPair.hasPrivateKey()) {
return keyPair;
}
try {
String privKey = await _privKeyFile.then(
(file) => file.readAsString(),
);
String pubKey = await _pubKeyFile.then(
(file) => file.readAsString(),
);
keyPair.publicKey = pubKey;
keyPair.privateKey = privKey;
return keyPair;
} catch (e) {
print(e);
return KeyPair();
}
}
static Future<void> writeKeyPair(KeyPair pair) async {
keyPair = pair;
final File privKeyFile = await _privKeyFile;
final File pubKeyFile = await _pubKeyFile;
pubKeyFile.writeAsString(keyPair.publicKey);
privKeyFile.writeAsString(keyPair.privateKey);
}
}