mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-08 00:16:27 +00:00
Merge pull request #8 from Zaliro/master
Encrypted private keys can now be passed in Api Credentials
This commit is contained in:
commit
3dd89a9ba5
@ -18,35 +18,19 @@ namespace CryptoExchange.Net.Authentication
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The private key to authenticate requests
|
/// The private key to authenticate requests
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SecureString PrivateKey { get; }
|
public PrivateKey PrivateKey { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create Api credentials providing a private key for authenication
|
/// Create Api credentials providing a private key for authentication
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="privateKey">The private key used for signing</param>
|
/// <param name="privateKey">The private key used for signing</param>
|
||||||
public ApiCredentials(SecureString privateKey)
|
public ApiCredentials(PrivateKey privateKey)
|
||||||
{
|
{
|
||||||
PrivateKey = privateKey;
|
PrivateKey = privateKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create Api credentials providing a private key for authenication
|
/// Create Api credentials providing a api key and secret for authentication
|
||||||
/// </summary>
|
|
||||||
/// <param name="privateKey">The private key used for signing</param>
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Create Api credentials providing a api key and secret for authenciation
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="key">The api key used for identification</param>
|
/// <param name="key">The api key used for identification</param>
|
||||||
/// <param name="secret">The api secret used for signing</param>
|
/// <param name="secret">The api secret used for signing</param>
|
||||||
@ -57,7 +41,7 @@ namespace CryptoExchange.Net.Authentication
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create Api credentials providing a api key and secret for authenciation
|
/// Create Api credentials providing a api key and secret for authentication
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="key">The api key used for identification</param>
|
/// <param name="key">The api key used for identification</param>
|
||||||
/// <param name="secret">The api secret used for signing</param>
|
/// <param name="secret">The api secret used for signing</param>
|
||||||
|
98
CryptoExchange.Net/Authentication/PrivateKey.cs
Normal file
98
CryptoExchange.Net/Authentication/PrivateKey.cs
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Security;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace CryptoExchange.Net.Authentication
|
||||||
|
{
|
||||||
|
public class PrivateKey : IDisposable
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The private key
|
||||||
|
/// </summary>
|
||||||
|
public SecureString Key { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The private key's passphrase
|
||||||
|
/// </summary>
|
||||||
|
public SecureString Passphrase { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Indicates if the private key is encrypted or not
|
||||||
|
/// </summary>
|
||||||
|
public bool IsEncrypted { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a private key providing an encrypted key informations
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">The private key used for signing</param>
|
||||||
|
/// <param name="passphrase">The private key's passphrase</param>
|
||||||
|
public PrivateKey(SecureString key, SecureString passphrase)
|
||||||
|
{
|
||||||
|
Key = key;
|
||||||
|
Passphrase = passphrase;
|
||||||
|
|
||||||
|
IsEncrypted = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a private key providing an encrypted key informations
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">The private key used for signing</param>
|
||||||
|
/// <param name="passphrase">The private key's passphrase</param>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a private key providing an unencrypted key informations
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">The private key used for signing</param>
|
||||||
|
public PrivateKey(SecureString key)
|
||||||
|
{
|
||||||
|
Key = key;
|
||||||
|
|
||||||
|
IsEncrypted = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a private key providing an encrypted key informations
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">The private key used for signing</param>
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user