Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[csharp-netcore] To support Secret API key input as string #13276

Merged
merged 4 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,18 @@ namespace {{packageName}}.Client
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
if (!File.Exists(KeyFilePath))
string keyStr = string.Empty;
if (File.Exists(KeyFilePath))
{
throw new Exception("key file path does not exist.");
keyStr = File.ReadAllText(KeyFilePath);
}
else
{
keyStr = KeyFilePath;
}

const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
var keyStr = File.ReadAllText(KeyFilePath);
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
var keyBytes = System.Convert.FromBase64String(ecKeyBase64String);
var ecdsa = ECDsa.Create();
Expand Down Expand Up @@ -422,11 +426,15 @@ namespace {{packageName}}.Client
bool isPrivateKeyFile = true;
byte[] pemkey = null;

if (!File.Exists(pemfile))
string pemstr = string.Empty;
if (File.Exists(pemfile))
{
pemstr = File.ReadAllText(pemfile).Trim();
}
else
{
throw new Exception("private key file does not exist.");
pemstr = pemfile;
}
string pemstr = File.ReadAllText(pemfile).Trim();

if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
{
Expand Down Expand Up @@ -717,9 +725,16 @@ namespace {{packageName}}.Client
/// <returns>Private Key Type</returns>
private PrivateKeyType GetKeyType(string keyFilePath)
{
if (!File.Exists(keyFilePath))
string[] key = null;

if (File.Exists(keyFilePath))
{
key = File.ReadAllLines(keyFilePath);
}
else
{
throw new Exception("Key file path does not exist.");
// The ApiKeyFilePath is passed as string
key = new string[] { keyFilePath };
}

const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
Expand All @@ -729,7 +744,6 @@ namespace {{packageName}}.Client
//var pkcs8Header = "BEGIN PRIVATE KEY";
//var pkcs8Footer = "END PRIVATE KEY";
PrivateKeyType keyType;
var key = File.ReadAllLines(keyFilePath);

if (key[0].Contains(rsaPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
Expand All @@ -743,7 +757,7 @@ namespace {{packageName}}.Client
}
else
{
throw new Exception("Either the key is invalid or key is not supported");
throw new Exception("The key file path does not exist or key is invalid or key is not supported");
}
return keyType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,18 @@ private string GetRSASignature(byte[] stringToSign)
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
if (!File.Exists(KeyFilePath))
string keyStr = string.Empty;
if (File.Exists(KeyFilePath))
{
throw new Exception("key file path does not exist.");
keyStr = File.ReadAllText(KeyFilePath);
}
else
{
keyStr = KeyFilePath;
}

const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
var keyStr = File.ReadAllText(KeyFilePath);
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
var keyBytes = System.Convert.FromBase64String(ecKeyBase64String);
var ecdsa = ECDsa.Create();
Expand Down Expand Up @@ -430,11 +434,15 @@ private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, Secu
bool isPrivateKeyFile = true;
byte[] pemkey = null;

if (!File.Exists(pemfile))
string pemstr = string.Empty;
if (File.Exists(pemfile))
{
pemstr = File.ReadAllText(pemfile).Trim();
}
else
{
throw new Exception("private key file does not exist.");
pemstr = pemfile;
}
string pemstr = File.ReadAllText(pemfile).Trim();

if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
{
Expand Down Expand Up @@ -725,9 +733,16 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV)
/// <returns>Private Key Type</returns>
private PrivateKeyType GetKeyType(string keyFilePath)
{
if (!File.Exists(keyFilePath))
string[] key = null;

if (File.Exists(keyFilePath))
{
key = File.ReadAllLines(keyFilePath);
}
else
{
throw new Exception("Key file path does not exist.");
// The ApiKeyFilePath is passed as string
key = new string[] { keyFilePath };
}

const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
Expand All @@ -737,7 +752,6 @@ private PrivateKeyType GetKeyType(string keyFilePath)
//var pkcs8Header = "BEGIN PRIVATE KEY";
//var pkcs8Footer = "END PRIVATE KEY";
PrivateKeyType keyType;
var key = File.ReadAllLines(keyFilePath);

if (key[0].Contains(rsaPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
Expand All @@ -751,7 +765,7 @@ private PrivateKeyType GetKeyType(string keyFilePath)
}
else
{
throw new Exception("Either the key is invalid or key is not supported");
throw new Exception("The key file path does not exist or key is invalid or key is not supported");
}
return keyType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,18 @@ private string GetRSASignature(byte[] stringToSign)
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
if (!File.Exists(KeyFilePath))
string keyStr = string.Empty;
if (File.Exists(KeyFilePath))
{
throw new Exception("key file path does not exist.");
keyStr = File.ReadAllText(KeyFilePath);
}
else
{
keyStr = KeyFilePath;
}

const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
var keyStr = File.ReadAllText(KeyFilePath);
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
var keyBytes = System.Convert.FromBase64String(ecKeyBase64String);
var ecdsa = ECDsa.Create();
Expand Down Expand Up @@ -430,11 +434,15 @@ private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, Secu
bool isPrivateKeyFile = true;
byte[] pemkey = null;

if (!File.Exists(pemfile))
string pemstr = string.Empty;
if (File.Exists(pemfile))
{
pemstr = File.ReadAllText(pemfile).Trim();
}
else
{
throw new Exception("private key file does not exist.");
pemstr = pemfile;
}
string pemstr = File.ReadAllText(pemfile).Trim();

if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
{
Expand Down Expand Up @@ -725,9 +733,16 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV)
/// <returns>Private Key Type</returns>
private PrivateKeyType GetKeyType(string keyFilePath)
{
if (!File.Exists(keyFilePath))
string[] key = null;

if (File.Exists(keyFilePath))
{
key = File.ReadAllLines(keyFilePath);
}
else
{
throw new Exception("Key file path does not exist.");
// The ApiKeyFilePath is passed as string
key = new string[] { keyFilePath };
}

const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
Expand All @@ -737,7 +752,6 @@ private PrivateKeyType GetKeyType(string keyFilePath)
//var pkcs8Header = "BEGIN PRIVATE KEY";
//var pkcs8Footer = "END PRIVATE KEY";
PrivateKeyType keyType;
var key = File.ReadAllLines(keyFilePath);

if (key[0].Contains(rsaPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
Expand All @@ -751,7 +765,7 @@ private PrivateKeyType GetKeyType(string keyFilePath)
}
else
{
throw new Exception("Either the key is invalid or key is not supported");
throw new Exception("The key file path does not exist or key is invalid or key is not supported");
}
return keyType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,18 @@ private string GetRSASignature(byte[] stringToSign)
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
if (!File.Exists(KeyFilePath))
string keyStr = string.Empty;
if (File.Exists(KeyFilePath))
{
throw new Exception("key file path does not exist.");
keyStr = File.ReadAllText(KeyFilePath);
}
else
{
keyStr = KeyFilePath;
}

const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
var keyStr = File.ReadAllText(KeyFilePath);
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
var keyBytes = System.Convert.FromBase64String(ecKeyBase64String);
var ecdsa = ECDsa.Create();
Expand Down Expand Up @@ -430,11 +434,15 @@ private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, Secu
bool isPrivateKeyFile = true;
byte[] pemkey = null;

if (!File.Exists(pemfile))
string pemstr = string.Empty;
if (File.Exists(pemfile))
{
pemstr = File.ReadAllText(pemfile).Trim();
}
else
{
throw new Exception("private key file does not exist.");
pemstr = pemfile;
}
string pemstr = File.ReadAllText(pemfile).Trim();

if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
{
Expand Down Expand Up @@ -725,9 +733,16 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV)
/// <returns>Private Key Type</returns>
private PrivateKeyType GetKeyType(string keyFilePath)
{
if (!File.Exists(keyFilePath))
string[] key = null;

if (File.Exists(keyFilePath))
{
key = File.ReadAllLines(keyFilePath);
}
else
{
throw new Exception("Key file path does not exist.");
// The ApiKeyFilePath is passed as string
key = new string[] { keyFilePath };
}

const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
Expand All @@ -737,7 +752,6 @@ private PrivateKeyType GetKeyType(string keyFilePath)
//var pkcs8Header = "BEGIN PRIVATE KEY";
//var pkcs8Footer = "END PRIVATE KEY";
PrivateKeyType keyType;
var key = File.ReadAllLines(keyFilePath);

if (key[0].Contains(rsaPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
Expand All @@ -751,7 +765,7 @@ private PrivateKeyType GetKeyType(string keyFilePath)
}
else
{
throw new Exception("Either the key is invalid or key is not supported");
throw new Exception("The key file path does not exist or key is invalid or key is not supported");
}
return keyType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,18 @@ private string GetRSASignature(byte[] stringToSign)
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
if (!File.Exists(KeyFilePath))
string keyStr = string.Empty;
if (File.Exists(KeyFilePath))
{
throw new Exception("key file path does not exist.");
keyStr = File.ReadAllText(KeyFilePath);
}
else
{
keyStr = KeyFilePath;
}

const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
var keyStr = File.ReadAllText(KeyFilePath);
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
var keyBytes = System.Convert.FromBase64String(ecKeyBase64String);
var ecdsa = ECDsa.Create();
Expand Down Expand Up @@ -430,11 +434,15 @@ private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, Secu
bool isPrivateKeyFile = true;
byte[] pemkey = null;

if (!File.Exists(pemfile))
string pemstr = string.Empty;
if (File.Exists(pemfile))
{
pemstr = File.ReadAllText(pemfile).Trim();
}
else
{
throw new Exception("private key file does not exist.");
pemstr = pemfile;
}
string pemstr = File.ReadAllText(pemfile).Trim();

if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
{
Expand Down Expand Up @@ -725,9 +733,16 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV)
/// <returns>Private Key Type</returns>
private PrivateKeyType GetKeyType(string keyFilePath)
{
if (!File.Exists(keyFilePath))
string[] key = null;

if (File.Exists(keyFilePath))
{
key = File.ReadAllLines(keyFilePath);
}
else
{
throw new Exception("Key file path does not exist.");
// The ApiKeyFilePath is passed as string
key = new string[] { keyFilePath };
}

const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
Expand All @@ -737,7 +752,6 @@ private PrivateKeyType GetKeyType(string keyFilePath)
//var pkcs8Header = "BEGIN PRIVATE KEY";
//var pkcs8Footer = "END PRIVATE KEY";
PrivateKeyType keyType;
var key = File.ReadAllLines(keyFilePath);

if (key[0].Contains(rsaPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
Expand All @@ -751,7 +765,7 @@ private PrivateKeyType GetKeyType(string keyFilePath)
}
else
{
throw new Exception("Either the key is invalid or key is not supported");
throw new Exception("The key file path does not exist or key is invalid or key is not supported");
}
return keyType;
}
Expand Down
Loading