public static Task<IAsymmetricKey> LoadKey(string path)
{
if (path == null)
throw new ArgumentNullException ("path");
return Task<IAsymmetricKey>.Factory.StartNew (() =>
{
if (!File.Exists (path) || new FileInfo (path).Length == 0)
{
var crypto = new RSACrypto();
var key = crypto.ExportKey (includePrivate: true);
Directory.CreateDirectory (Path.GetDirectoryName (path));
using (var fstream = File.OpenWrite (path))
{
var writer = new StreamValueWriter (fstream);
key.Serialize (null, writer);
}
return key;
}
var reader = new BufferValueReader (File.ReadAllBytes (path));
return new RSAAsymmetricKey (null, reader);
});
}