/// <summary>
        /// Generates a NATS Ed25519 keypair, used to sign server nonces, from a
        /// private credentials file.
        /// </summary>
        /// <param name="path">The credentials file, could be a "*.nk" or "*.creds" file.</param>
        /// <returns>A NATS Ed25519 KeyPair</returns>
        public static NkeyPair LoadNkeyPairFromSeedFile(string path)
        {
            NkeyPair     kp     = null;
            string       text   = null;
            string       line   = null;
            string       seed   = null;
            StringReader reader = null;

            try
            {
                text = File.ReadAllText(path).Trim();
                if (string.IsNullOrEmpty(text))
                {
                    throw new NATSException("Credentials file is empty");
                }

                // if it's a nk file, it only has the nkey
                if (text.StartsWith("SU"))
                {
                    kp = Nkeys.FromSeed(text);
                    return(kp);
                }

                // otherwise assume it's a creds file.
                reader = new StringReader(text);
                for (line = reader.ReadLine(); line != null; line = reader.ReadLine())
                {
                    if (line.Contains("-----BEGIN USER NKEY SEED-----"))
                    {
                        seed = reader.ReadLine();
                        kp   = Nkeys.FromSeed(seed);
                        Nkeys.Wipe(seed);
                    }
                    Nkeys.Wipe(line);
                }

                if (kp == null)
                {
                    throw new NATSException("Seed not found in credentials file.");
                }
                else
                {
                    return(kp);
                }
            }
            finally
            {
                Nkeys.Wipe(line);
                Nkeys.Wipe(text);
                Nkeys.Wipe(seed);
                reader?.Dispose();
            }
        }
Exemple #2
0
 /// <summary>
 /// Creates an NkeyPair from a private seed String.
 /// </summary>
 /// <param name="seed"></param>
 /// <returns>A NATS Ed25519 Keypair</returns>
 public static NkeyPair FromSeed(string seed)
 {
     byte[] userSeed = DecodeSeed(seed);
     try
     {
         var kp = new NkeyPair(userSeed);
         return(kp);
     }
     finally
     {
         Wipe(ref userSeed);
     }
 }