diff --git a/CryptoExchange.Net/Authentication/ApiCredentials.cs b/CryptoExchange.Net/Authentication/ApiCredentials.cs index 836a5c5..2c6d65d 100644 --- a/CryptoExchange.Net/Authentication/ApiCredentials.cs +++ b/CryptoExchange.Net/Authentication/ApiCredentials.cs @@ -18,35 +18,19 @@ namespace CryptoExchange.Net.Authentication /// /// The private key to authenticate requests /// - public SecureString PrivateKey { get; } + public PrivateKey PrivateKey { get; } /// - /// Create Api credentials providing a private key for authenication + /// Create Api credentials providing a private key for authentication /// /// The private key used for signing - public ApiCredentials(SecureString privateKey) + public ApiCredentials(PrivateKey privateKey) { PrivateKey = privateKey; } /// - /// Create Api credentials providing a private key for authenication - /// - /// The private key used for signing - public ApiCredentials(string privateKey) - { - if(string.IsNullOrEmpty(privateKey)) - throw new ArgumentException("Private key can't be null/empty"); - - var securePrivateKey = new SecureString(); - foreach (var c in privateKey) - securePrivateKey.AppendChar(c); - securePrivateKey.MakeReadOnly(); - PrivateKey = securePrivateKey; - } - - /// - /// Create Api credentials providing a api key and secret for authenciation + /// Create Api credentials providing a api key and secret for authentication /// /// The api key used for identification /// The api secret used for signing @@ -57,7 +41,7 @@ namespace CryptoExchange.Net.Authentication } /// - /// Create Api credentials providing a api key and secret for authenciation + /// Create Api credentials providing a api key and secret for authentication /// /// The api key used for identification /// The api secret used for signing diff --git a/CryptoExchange.Net/Authentication/PrivateKey.cs b/CryptoExchange.Net/Authentication/PrivateKey.cs new file mode 100644 index 0000000..31372d9 --- /dev/null +++ b/CryptoExchange.Net/Authentication/PrivateKey.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Security; +using System.Text; + +namespace CryptoExchange.Net.Authentication +{ + public class PrivateKey : IDisposable + { + /// + /// The private key + /// + public SecureString Key { get; } + + /// + /// The private key's passphrase + /// + public SecureString Passphrase { get; } + + /// + /// Indicates if the private key is encrypted or not + /// + public bool IsEncrypted { get; } + + /// + /// Create a private key providing an encrypted key informations + /// + /// The private key used for signing + /// The private key's passphrase + public PrivateKey(SecureString key, SecureString passphrase) + { + Key = key; + Passphrase = passphrase; + + IsEncrypted = true; + } + + /// + /// Create a private key providing an encrypted key informations + /// + /// The private key used for signing + /// The private key's passphrase + public PrivateKey(string key, string passphrase) + { + if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(passphrase)) + throw new ArgumentException("Key and passphrase can't be null/empty"); + + var secureKey = new SecureString(); + foreach (var c in key) + secureKey.AppendChar(c); + secureKey.MakeReadOnly(); + Key = secureKey; + + var securePassphrase = new SecureString(); + foreach (var c in passphrase) + securePassphrase.AppendChar(c); + securePassphrase.MakeReadOnly(); + Passphrase = securePassphrase; + + IsEncrypted = true; + } + + /// + /// Create a private key providing an unencrypted key informations + /// + /// The private key used for signing + public PrivateKey(SecureString key) + { + Key = key; + + IsEncrypted = false; + } + + /// + /// Create a private key providing an encrypted key informations + /// + /// The private key used for signing + public PrivateKey(string key) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentException("Key can't be null/empty"); + + var secureKey = new SecureString(); + foreach (var c in key) + secureKey.AppendChar(c); + secureKey.MakeReadOnly(); + Key = secureKey; + + IsEncrypted = false; + } + + public void Dispose() + { + Key?.Dispose(); + Passphrase?.Dispose(); + } + } +}