asymmetric_crypto_primitives 0.0.1 copy "asymmetric_crypto_primitives: ^0.0.1" to clipboard
asymmetric_crypto_primitives: ^0.0.1 copied to clipboard

Flutter plugin for signing and managing keys on Android devices.

A plugin for signing data using RSA or Ed25519. Enables the user to rotate the keys and clean unused ones. It is based on simple signing plugin.

Features #

  • Delivers EDDSA crypto primitive to Android, as it has no native support
  • Will allow in the future to deliver a next gen asymmetric crypto algo to an Android device if it will not have the native support for the algorithm
  • Allows the prerotation of keys as it generates 2 key pairs by default.

Getting started #

Using the RSA method for generating keys and data signing requires the screen lock to be enabled on the device. It can be easily checked by checkIfDeviceSecure method:

isDeviceSecure = await checkIfDeviceSecure(); //returns true if screen lock is set

Working with RSA algorithm without checking whether the screen lock is set may cause functions to throw the DeviceNotSecuredException.

To start working with the plugin it is necessary to initialize the signer object for Ed25519 or RSA method:

void main() async{  
  WidgetsFlutterBinding.ensureInitialized();
  var isDeviceSecure = await AsymmetricCryptoPrimitives.checkIfDeviceSecure();
  if (isDeviceSecure) {
    var signer = await AsymmetricCryptoPrimitives.establishForRSA();
    runApp(MyApp(
      signer: signer,
    ));
  }
}

Most of the plugin methods are available through signer object.

Usage #

Signing data

String strToSign = 'Sign me!';
signature = await signer.sign(strToSign);

Getting keys

String currentKey = '';  
String nextKey = '';
currentKey = await signer.getCurrentPubKey();  
nextKey = await signer.getNextPubKey();

Rotating keys

String currentKey = 'current key here!';  
String nextKey = 'next key here!';
await signer.rotateForEd25519();  
//To see the results of the rotation
currentKey = await signer.getCurrentPubKey();  
nextKey = await signer.getNextPubKey();

Warning: The rotation doesn't currently work for RSA algorithm. Work in progress.

Getting the signer's uique UUID

String uuid = '';
uuid = await signer.getUuid();

Getting a previously used signer object

void main() async{  
  WidgetsFlutterBinding.ensureInitialized();  
 var signer = await AsymmetricCryptoPrimitives.getEd25519SignerFromUuid('ecd886f1-1af6-4e62-a6b2-825e2b15ebd2');  //or getRSASignerFromUuid()
  runApp(MyApp(signer: signer,));  
}

This method will throw an IncorrectUuidException if no keys associated with the entered UUID were saved to the device.

Clean up

await AsymmetricCryptoPrimitives.cleanUp(signer);

Removes all the keys that were associated with this signer object.

Data storing functions #

//Writing data example
String _data = 'Data';
String _key = 'Key';
var result = await AsymmetricCryptoPrimitives.writeData(_key, _data); //returns true if everything goes fine. Can throw a SharedPreferencesException or DeviceNotSecuredException
//Reading data example
String _key = 'Key';
var result = await AsymmetricCryptoPrimitives.readData(_key); //returns data written under key if everything goes fine. Can throw a InvalidSignatureException, DeviceNotSecuredException or NoKeyInStorageException
//Deleting data example
String _key = 'Key';
var result = await AsymmetricCryptoPrimitives.deleteData(_key); //returns true if everything goes fine. Can throw a SharedPreferencesException or DeviceNotSecuredException
//Editing data example
String _data = 'Data';
String _key = 'Key';
var result = await AsymmetricCryptoPrimitives.editData(_key, _data); //returns true if everything goes fine. Can throw a SharedPreferencesException or DeviceNotSecuredException