Exemplo n.º 1
0
    public static RSA LoadDevelopment(string path, bool createIfMissing)
    {
        var fileExists = File.Exists(path);

        if (!fileExists && !createIfMissing)
        {
            throw new InvalidOperationException($"Couldn't find the file '{path}' and creation of a development key was not requested.");
        }

        if (fileExists)
        {
            var rsa = JsonConvert.DeserializeObject <RSAKeyParameters>(File.ReadAllText(path));
            return(rsa.GetRSA());
        }
        else
        {
            var parameters = RSAKeyParameters.Create();
            var directory  = Path.GetDirectoryName(path);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            File.WriteAllText(path, JsonConvert.SerializeObject(parameters));
            return(parameters.GetRSA());
        }
    }