Exemplo n.º 1
0
        /// <summary>
        /// Load a key by alias.  If it does not exist, a new one will be created and saved to the key ring
        /// </summary>
        public static PrivateKey LoadOrCreate(string alias)
        {
            if (!Directory.Exists(KeyRingPath))
            {
                Directory.CreateDirectory(KeyRingPath);
            }

            var keyId = AliasFile.Load(GetPath(AliasFilename)).GetId(alias);

            return(KeyExists(keyId)
                ? PrivateKeyFile.Load(GetPath(keyId))
                : Cryptography.GenerateKey(alias).Save());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load a key from the key ring.  If "aliasOrId" is a valid alias, the corresponding key will be used.  Otherwise, it will be treate as the id.
        /// </summary>
        public static PrivateKey Load(this string aliasOrId)
        {
            if (!Directory.Exists(KeyRingPath))
            {
                throw new Exception("No key ring exists on this machine for the current user");
            }

            var aliases = AliasFile.Load(GetPath(AliasFilename));
            var keyId   = aliases.GetId(aliasOrId);

            if (!KeyExists(keyId))
            {
                throw new Exception($"key {keyId} was not found on the key ring");
            }

            return(PrivateKeyFile.Load(GetPath(keyId)));
        }