Encrypted messaging app
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.

27 lines
849 B

  1. import 'package:shared_preferences/shared_preferences.dart';
  2. import "package:pointycastle/export.dart";
  3. import '/utils/encryption/rsa_key_helper.dart';
  4. const rsaPrivateKeyName = 'rsaPrivateKey';
  5. void setPrivateKey(RSAPrivateKey key) async {
  6. String keyPem = RsaKeyHelper.encodePrivateKeyToPem(key);
  7. final prefs = await SharedPreferences.getInstance();
  8. prefs.setString(rsaPrivateKeyName, keyPem);
  9. }
  10. void unsetPrivateKey() async {
  11. final prefs = await SharedPreferences.getInstance();
  12. await prefs.remove(rsaPrivateKeyName);
  13. }
  14. Future<RSAPrivateKey> getPrivateKey() async {
  15. final prefs = await SharedPreferences.getInstance();
  16. String? keyPem = prefs.getString(rsaPrivateKeyName);
  17. if (keyPem == null) {
  18. throw Exception('No RSA private key set');
  19. }
  20. return RsaKeyHelper.parsePrivateKeyFromPem(keyPem);
  21. }