diff --git a/modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache b/modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache
index a1169150ea22..04a0518a630a 100644
--- a/modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache
+++ b/modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache
@@ -18,7 +18,6 @@ namespace {{packageName}}.Client
///
public class HttpSigningConfiguration
{
- #region
///
/// Initialize the HashAlgorithm and SigningAlgorithm to default value
///
@@ -27,9 +26,7 @@ namespace {{packageName}}.Client
HashAlgorithm = HashAlgorithmName.SHA256;
SigningAlgorithm = "PKCS1-v15";
}
- #endregion
- #region Properties
///
///Gets the Api keyId
///
@@ -70,18 +67,13 @@ namespace {{packageName}}.Client
///
public int SignatureValidityPeriod { get; set; }
- #endregion
-
- #region enum
private enum PrivateKeyType
{
None = 0,
RSA = 1,
ECDSA = 2,
}
- #endregion
- #region Methods
///
/// Gets the Headers for HttpSigning
///
@@ -183,12 +175,12 @@ namespace {{packageName}}.Client
if (HashAlgorithm == HashAlgorithmName.SHA256)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-256={0}", Convert.ToBase64String(bodyDigest));
}
else if (HashAlgorithm == HashAlgorithmName.SHA512)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-512={0}", Convert.ToBase64String(bodyDigest));
}
else
@@ -256,7 +248,7 @@ namespace {{packageName}}.Client
}
//Concatenate headers value separated by new line
var headerValuesString = string.Join("\n", headerValuesList);
- var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString);
+ var signatureStringHash = GetStringHash(HashAlgorithm, headerValuesString);
string headerSignatureStr = null;
var keyType = GetKeyType(KeyString);
@@ -292,11 +284,27 @@ namespace {{packageName}}.Client
return HttpSignedRequestHeader;
}
- private byte[] GetStringHash(string hashName, string stringToBeHashed)
+ private byte[] GetStringHash(HashAlgorithmName hashAlgorithmName, string stringToBeHashed)
{
- var hashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);
- var bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
- var stringHash = hashAlgorithm.ComputeHash(bytes);
+ HashAlgorithm{{nrt?}} hashAlgorithm = null;
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA1)
+ hashAlgorithm = SHA1.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA256)
+ hashAlgorithm = SHA256.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA512)
+ hashAlgorithm = SHA512.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.MD5)
+ hashAlgorithm = MD5.Create();
+
+ if (hashAlgorithm == null)
+ throw new NullReferenceException($"{ nameof(hashAlgorithm) } was null.");
+
+ byte[] bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
+ byte[] stringHash = hashAlgorithm.ComputeHash(bytes);
return stringHash;
}
@@ -661,7 +669,7 @@ namespace {{packageName}}.Client
Array.Copy(salt, 0, data00, psbytes.Length, salt.Length); //concatenate the salt bytes
// ---- do multi-hashing and concatenate results D1, D2 ... into keymaterial bytes ----
- MD5 md5 = new MD5CryptoServiceProvider();
+ MD5 md5 = MD5.Create();
byte[] result = null;
byte[] hashtarget = new byte[HASHLENGTH + data00.Length]; //fixed length initial hashtarget
@@ -774,6 +782,5 @@ namespace {{packageName}}.Client
}
return apiKeyString;
}
- #endregion
}
}
diff --git a/samples/client/petstore/csharp/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 05442e501a96..2f04603b7f96 100644
--- a/samples/client/petstore/csharp/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
+++ b/samples/client/petstore/csharp/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
@@ -26,7 +26,6 @@ namespace Org.OpenAPITools.Client
///
public class HttpSigningConfiguration
{
- #region
///
/// Initialize the HashAlgorithm and SigningAlgorithm to default value
///
@@ -35,9 +34,7 @@ namespace Org.OpenAPITools.Client
HashAlgorithm = HashAlgorithmName.SHA256;
SigningAlgorithm = "PKCS1-v15";
}
- #endregion
- #region Properties
///
///Gets the Api keyId
///
@@ -78,18 +75,13 @@ namespace Org.OpenAPITools.Client
///
public int SignatureValidityPeriod { get; set; }
- #endregion
-
- #region enum
private enum PrivateKeyType
{
None = 0,
RSA = 1,
ECDSA = 2,
}
- #endregion
- #region Methods
///
/// Gets the Headers for HttpSigning
///
@@ -191,12 +183,12 @@ namespace Org.OpenAPITools.Client
if (HashAlgorithm == HashAlgorithmName.SHA256)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-256={0}", Convert.ToBase64String(bodyDigest));
}
else if (HashAlgorithm == HashAlgorithmName.SHA512)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-512={0}", Convert.ToBase64String(bodyDigest));
}
else
@@ -264,7 +256,7 @@ namespace Org.OpenAPITools.Client
}
//Concatenate headers value separated by new line
var headerValuesString = string.Join("\n", headerValuesList);
- var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString);
+ var signatureStringHash = GetStringHash(HashAlgorithm, headerValuesString);
string headerSignatureStr = null;
var keyType = GetKeyType(KeyString);
@@ -300,11 +292,27 @@ namespace Org.OpenAPITools.Client
return HttpSignedRequestHeader;
}
- private byte[] GetStringHash(string hashName, string stringToBeHashed)
+ private byte[] GetStringHash(HashAlgorithmName hashAlgorithmName, string stringToBeHashed)
{
- var hashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);
- var bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
- var stringHash = hashAlgorithm.ComputeHash(bytes);
+ HashAlgorithm hashAlgorithm = null;
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA1)
+ hashAlgorithm = SHA1.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA256)
+ hashAlgorithm = SHA256.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA512)
+ hashAlgorithm = SHA512.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.MD5)
+ hashAlgorithm = MD5.Create();
+
+ if (hashAlgorithm == null)
+ throw new NullReferenceException($"{ nameof(hashAlgorithm) } was null.");
+
+ byte[] bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
+ byte[] stringHash = hashAlgorithm.ComputeHash(bytes);
return stringHash;
}
@@ -669,7 +677,7 @@ namespace Org.OpenAPITools.Client
Array.Copy(salt, 0, data00, psbytes.Length, salt.Length); //concatenate the salt bytes
// ---- do multi-hashing and concatenate results D1, D2 ... into keymaterial bytes ----
- MD5 md5 = new MD5CryptoServiceProvider();
+ MD5 md5 = MD5.Create();
byte[] result = null;
byte[] hashtarget = new byte[HASHLENGTH + data00.Length]; //fixed length initial hashtarget
@@ -782,6 +790,5 @@ namespace Org.OpenAPITools.Client
}
return apiKeyString;
}
- #endregion
}
}
diff --git a/samples/client/petstore/csharp/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 05442e501a96..2f04603b7f96 100644
--- a/samples/client/petstore/csharp/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
+++ b/samples/client/petstore/csharp/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
@@ -26,7 +26,6 @@ namespace Org.OpenAPITools.Client
///
public class HttpSigningConfiguration
{
- #region
///
/// Initialize the HashAlgorithm and SigningAlgorithm to default value
///
@@ -35,9 +34,7 @@ namespace Org.OpenAPITools.Client
HashAlgorithm = HashAlgorithmName.SHA256;
SigningAlgorithm = "PKCS1-v15";
}
- #endregion
- #region Properties
///
///Gets the Api keyId
///
@@ -78,18 +75,13 @@ namespace Org.OpenAPITools.Client
///
public int SignatureValidityPeriod { get; set; }
- #endregion
-
- #region enum
private enum PrivateKeyType
{
None = 0,
RSA = 1,
ECDSA = 2,
}
- #endregion
- #region Methods
///
/// Gets the Headers for HttpSigning
///
@@ -191,12 +183,12 @@ namespace Org.OpenAPITools.Client
if (HashAlgorithm == HashAlgorithmName.SHA256)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-256={0}", Convert.ToBase64String(bodyDigest));
}
else if (HashAlgorithm == HashAlgorithmName.SHA512)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-512={0}", Convert.ToBase64String(bodyDigest));
}
else
@@ -264,7 +256,7 @@ namespace Org.OpenAPITools.Client
}
//Concatenate headers value separated by new line
var headerValuesString = string.Join("\n", headerValuesList);
- var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString);
+ var signatureStringHash = GetStringHash(HashAlgorithm, headerValuesString);
string headerSignatureStr = null;
var keyType = GetKeyType(KeyString);
@@ -300,11 +292,27 @@ namespace Org.OpenAPITools.Client
return HttpSignedRequestHeader;
}
- private byte[] GetStringHash(string hashName, string stringToBeHashed)
+ private byte[] GetStringHash(HashAlgorithmName hashAlgorithmName, string stringToBeHashed)
{
- var hashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);
- var bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
- var stringHash = hashAlgorithm.ComputeHash(bytes);
+ HashAlgorithm hashAlgorithm = null;
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA1)
+ hashAlgorithm = SHA1.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA256)
+ hashAlgorithm = SHA256.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA512)
+ hashAlgorithm = SHA512.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.MD5)
+ hashAlgorithm = MD5.Create();
+
+ if (hashAlgorithm == null)
+ throw new NullReferenceException($"{ nameof(hashAlgorithm) } was null.");
+
+ byte[] bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
+ byte[] stringHash = hashAlgorithm.ComputeHash(bytes);
return stringHash;
}
@@ -669,7 +677,7 @@ namespace Org.OpenAPITools.Client
Array.Copy(salt, 0, data00, psbytes.Length, salt.Length); //concatenate the salt bytes
// ---- do multi-hashing and concatenate results D1, D2 ... into keymaterial bytes ----
- MD5 md5 = new MD5CryptoServiceProvider();
+ MD5 md5 = MD5.Create();
byte[] result = null;
byte[] hashtarget = new byte[HASHLENGTH + data00.Length]; //fixed length initial hashtarget
@@ -782,6 +790,5 @@ namespace Org.OpenAPITools.Client
}
return apiKeyString;
}
- #endregion
}
}
diff --git a/samples/client/petstore/csharp/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 05442e501a96..2f04603b7f96 100644
--- a/samples/client/petstore/csharp/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
+++ b/samples/client/petstore/csharp/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
@@ -26,7 +26,6 @@ namespace Org.OpenAPITools.Client
///
public class HttpSigningConfiguration
{
- #region
///
/// Initialize the HashAlgorithm and SigningAlgorithm to default value
///
@@ -35,9 +34,7 @@ namespace Org.OpenAPITools.Client
HashAlgorithm = HashAlgorithmName.SHA256;
SigningAlgorithm = "PKCS1-v15";
}
- #endregion
- #region Properties
///
///Gets the Api keyId
///
@@ -78,18 +75,13 @@ namespace Org.OpenAPITools.Client
///
public int SignatureValidityPeriod { get; set; }
- #endregion
-
- #region enum
private enum PrivateKeyType
{
None = 0,
RSA = 1,
ECDSA = 2,
}
- #endregion
- #region Methods
///
/// Gets the Headers for HttpSigning
///
@@ -191,12 +183,12 @@ namespace Org.OpenAPITools.Client
if (HashAlgorithm == HashAlgorithmName.SHA256)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-256={0}", Convert.ToBase64String(bodyDigest));
}
else if (HashAlgorithm == HashAlgorithmName.SHA512)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-512={0}", Convert.ToBase64String(bodyDigest));
}
else
@@ -264,7 +256,7 @@ namespace Org.OpenAPITools.Client
}
//Concatenate headers value separated by new line
var headerValuesString = string.Join("\n", headerValuesList);
- var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString);
+ var signatureStringHash = GetStringHash(HashAlgorithm, headerValuesString);
string headerSignatureStr = null;
var keyType = GetKeyType(KeyString);
@@ -300,11 +292,27 @@ namespace Org.OpenAPITools.Client
return HttpSignedRequestHeader;
}
- private byte[] GetStringHash(string hashName, string stringToBeHashed)
+ private byte[] GetStringHash(HashAlgorithmName hashAlgorithmName, string stringToBeHashed)
{
- var hashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);
- var bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
- var stringHash = hashAlgorithm.ComputeHash(bytes);
+ HashAlgorithm hashAlgorithm = null;
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA1)
+ hashAlgorithm = SHA1.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA256)
+ hashAlgorithm = SHA256.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA512)
+ hashAlgorithm = SHA512.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.MD5)
+ hashAlgorithm = MD5.Create();
+
+ if (hashAlgorithm == null)
+ throw new NullReferenceException($"{ nameof(hashAlgorithm) } was null.");
+
+ byte[] bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
+ byte[] stringHash = hashAlgorithm.ComputeHash(bytes);
return stringHash;
}
@@ -669,7 +677,7 @@ namespace Org.OpenAPITools.Client
Array.Copy(salt, 0, data00, psbytes.Length, salt.Length); //concatenate the salt bytes
// ---- do multi-hashing and concatenate results D1, D2 ... into keymaterial bytes ----
- MD5 md5 = new MD5CryptoServiceProvider();
+ MD5 md5 = MD5.Create();
byte[] result = null;
byte[] hashtarget = new byte[HASHLENGTH + data00.Length]; //fixed length initial hashtarget
@@ -782,6 +790,5 @@ namespace Org.OpenAPITools.Client
}
return apiKeyString;
}
- #endregion
}
}
diff --git a/samples/client/petstore/csharp/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 05442e501a96..2f04603b7f96 100644
--- a/samples/client/petstore/csharp/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
+++ b/samples/client/petstore/csharp/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
@@ -26,7 +26,6 @@ namespace Org.OpenAPITools.Client
///
public class HttpSigningConfiguration
{
- #region
///
/// Initialize the HashAlgorithm and SigningAlgorithm to default value
///
@@ -35,9 +34,7 @@ namespace Org.OpenAPITools.Client
HashAlgorithm = HashAlgorithmName.SHA256;
SigningAlgorithm = "PKCS1-v15";
}
- #endregion
- #region Properties
///
///Gets the Api keyId
///
@@ -78,18 +75,13 @@ namespace Org.OpenAPITools.Client
///
public int SignatureValidityPeriod { get; set; }
- #endregion
-
- #region enum
private enum PrivateKeyType
{
None = 0,
RSA = 1,
ECDSA = 2,
}
- #endregion
- #region Methods
///
/// Gets the Headers for HttpSigning
///
@@ -191,12 +183,12 @@ namespace Org.OpenAPITools.Client
if (HashAlgorithm == HashAlgorithmName.SHA256)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-256={0}", Convert.ToBase64String(bodyDigest));
}
else if (HashAlgorithm == HashAlgorithmName.SHA512)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-512={0}", Convert.ToBase64String(bodyDigest));
}
else
@@ -264,7 +256,7 @@ namespace Org.OpenAPITools.Client
}
//Concatenate headers value separated by new line
var headerValuesString = string.Join("\n", headerValuesList);
- var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString);
+ var signatureStringHash = GetStringHash(HashAlgorithm, headerValuesString);
string headerSignatureStr = null;
var keyType = GetKeyType(KeyString);
@@ -300,11 +292,27 @@ namespace Org.OpenAPITools.Client
return HttpSignedRequestHeader;
}
- private byte[] GetStringHash(string hashName, string stringToBeHashed)
+ private byte[] GetStringHash(HashAlgorithmName hashAlgorithmName, string stringToBeHashed)
{
- var hashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);
- var bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
- var stringHash = hashAlgorithm.ComputeHash(bytes);
+ HashAlgorithm hashAlgorithm = null;
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA1)
+ hashAlgorithm = SHA1.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA256)
+ hashAlgorithm = SHA256.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA512)
+ hashAlgorithm = SHA512.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.MD5)
+ hashAlgorithm = MD5.Create();
+
+ if (hashAlgorithm == null)
+ throw new NullReferenceException($"{ nameof(hashAlgorithm) } was null.");
+
+ byte[] bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
+ byte[] stringHash = hashAlgorithm.ComputeHash(bytes);
return stringHash;
}
@@ -669,7 +677,7 @@ namespace Org.OpenAPITools.Client
Array.Copy(salt, 0, data00, psbytes.Length, salt.Length); //concatenate the salt bytes
// ---- do multi-hashing and concatenate results D1, D2 ... into keymaterial bytes ----
- MD5 md5 = new MD5CryptoServiceProvider();
+ MD5 md5 = MD5.Create();
byte[] result = null;
byte[] hashtarget = new byte[HASHLENGTH + data00.Length]; //fixed length initial hashtarget
@@ -782,6 +790,5 @@ namespace Org.OpenAPITools.Client
}
return apiKeyString;
}
- #endregion
}
}
diff --git a/samples/client/petstore/csharp/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 05442e501a96..e42e333d3751 100644
--- a/samples/client/petstore/csharp/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
+++ b/samples/client/petstore/csharp/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
@@ -26,7 +26,6 @@ namespace Org.OpenAPITools.Client
///
public class HttpSigningConfiguration
{
- #region
///
/// Initialize the HashAlgorithm and SigningAlgorithm to default value
///
@@ -35,9 +34,7 @@ namespace Org.OpenAPITools.Client
HashAlgorithm = HashAlgorithmName.SHA256;
SigningAlgorithm = "PKCS1-v15";
}
- #endregion
- #region Properties
///
///Gets the Api keyId
///
@@ -78,18 +75,13 @@ namespace Org.OpenAPITools.Client
///
public int SignatureValidityPeriod { get; set; }
- #endregion
-
- #region enum
private enum PrivateKeyType
{
None = 0,
RSA = 1,
ECDSA = 2,
}
- #endregion
- #region Methods
///
/// Gets the Headers for HttpSigning
///
@@ -191,12 +183,12 @@ namespace Org.OpenAPITools.Client
if (HashAlgorithm == HashAlgorithmName.SHA256)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-256={0}", Convert.ToBase64String(bodyDigest));
}
else if (HashAlgorithm == HashAlgorithmName.SHA512)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-512={0}", Convert.ToBase64String(bodyDigest));
}
else
@@ -264,7 +256,7 @@ namespace Org.OpenAPITools.Client
}
//Concatenate headers value separated by new line
var headerValuesString = string.Join("\n", headerValuesList);
- var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString);
+ var signatureStringHash = GetStringHash(HashAlgorithm, headerValuesString);
string headerSignatureStr = null;
var keyType = GetKeyType(KeyString);
@@ -300,11 +292,27 @@ namespace Org.OpenAPITools.Client
return HttpSignedRequestHeader;
}
- private byte[] GetStringHash(string hashName, string stringToBeHashed)
+ private byte[] GetStringHash(HashAlgorithmName hashAlgorithmName, string stringToBeHashed)
{
- var hashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);
- var bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
- var stringHash = hashAlgorithm.ComputeHash(bytes);
+ HashAlgorithm? hashAlgorithm = null;
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA1)
+ hashAlgorithm = SHA1.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA256)
+ hashAlgorithm = SHA256.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA512)
+ hashAlgorithm = SHA512.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.MD5)
+ hashAlgorithm = MD5.Create();
+
+ if (hashAlgorithm == null)
+ throw new NullReferenceException($"{ nameof(hashAlgorithm) } was null.");
+
+ byte[] bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
+ byte[] stringHash = hashAlgorithm.ComputeHash(bytes);
return stringHash;
}
@@ -669,7 +677,7 @@ namespace Org.OpenAPITools.Client
Array.Copy(salt, 0, data00, psbytes.Length, salt.Length); //concatenate the salt bytes
// ---- do multi-hashing and concatenate results D1, D2 ... into keymaterial bytes ----
- MD5 md5 = new MD5CryptoServiceProvider();
+ MD5 md5 = MD5.Create();
byte[] result = null;
byte[] hashtarget = new byte[HASHLENGTH + data00.Length]; //fixed length initial hashtarget
@@ -782,6 +790,5 @@ namespace Org.OpenAPITools.Client
}
return apiKeyString;
}
- #endregion
}
}
diff --git a/samples/client/petstore/csharp/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 05442e501a96..2f04603b7f96 100644
--- a/samples/client/petstore/csharp/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
+++ b/samples/client/petstore/csharp/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
@@ -26,7 +26,6 @@ namespace Org.OpenAPITools.Client
///
public class HttpSigningConfiguration
{
- #region
///
/// Initialize the HashAlgorithm and SigningAlgorithm to default value
///
@@ -35,9 +34,7 @@ namespace Org.OpenAPITools.Client
HashAlgorithm = HashAlgorithmName.SHA256;
SigningAlgorithm = "PKCS1-v15";
}
- #endregion
- #region Properties
///
///Gets the Api keyId
///
@@ -78,18 +75,13 @@ namespace Org.OpenAPITools.Client
///
public int SignatureValidityPeriod { get; set; }
- #endregion
-
- #region enum
private enum PrivateKeyType
{
None = 0,
RSA = 1,
ECDSA = 2,
}
- #endregion
- #region Methods
///
/// Gets the Headers for HttpSigning
///
@@ -191,12 +183,12 @@ namespace Org.OpenAPITools.Client
if (HashAlgorithm == HashAlgorithmName.SHA256)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-256={0}", Convert.ToBase64String(bodyDigest));
}
else if (HashAlgorithm == HashAlgorithmName.SHA512)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-512={0}", Convert.ToBase64String(bodyDigest));
}
else
@@ -264,7 +256,7 @@ namespace Org.OpenAPITools.Client
}
//Concatenate headers value separated by new line
var headerValuesString = string.Join("\n", headerValuesList);
- var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString);
+ var signatureStringHash = GetStringHash(HashAlgorithm, headerValuesString);
string headerSignatureStr = null;
var keyType = GetKeyType(KeyString);
@@ -300,11 +292,27 @@ namespace Org.OpenAPITools.Client
return HttpSignedRequestHeader;
}
- private byte[] GetStringHash(string hashName, string stringToBeHashed)
+ private byte[] GetStringHash(HashAlgorithmName hashAlgorithmName, string stringToBeHashed)
{
- var hashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);
- var bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
- var stringHash = hashAlgorithm.ComputeHash(bytes);
+ HashAlgorithm hashAlgorithm = null;
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA1)
+ hashAlgorithm = SHA1.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA256)
+ hashAlgorithm = SHA256.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA512)
+ hashAlgorithm = SHA512.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.MD5)
+ hashAlgorithm = MD5.Create();
+
+ if (hashAlgorithm == null)
+ throw new NullReferenceException($"{ nameof(hashAlgorithm) } was null.");
+
+ byte[] bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
+ byte[] stringHash = hashAlgorithm.ComputeHash(bytes);
return stringHash;
}
@@ -669,7 +677,7 @@ namespace Org.OpenAPITools.Client
Array.Copy(salt, 0, data00, psbytes.Length, salt.Length); //concatenate the salt bytes
// ---- do multi-hashing and concatenate results D1, D2 ... into keymaterial bytes ----
- MD5 md5 = new MD5CryptoServiceProvider();
+ MD5 md5 = MD5.Create();
byte[] result = null;
byte[] hashtarget = new byte[HASHLENGTH + data00.Length]; //fixed length initial hashtarget
@@ -782,6 +790,5 @@ namespace Org.OpenAPITools.Client
}
return apiKeyString;
}
- #endregion
}
}
diff --git a/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 05442e501a96..2f04603b7f96 100644
--- a/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
+++ b/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
@@ -26,7 +26,6 @@ namespace Org.OpenAPITools.Client
///
public class HttpSigningConfiguration
{
- #region
///
/// Initialize the HashAlgorithm and SigningAlgorithm to default value
///
@@ -35,9 +34,7 @@ namespace Org.OpenAPITools.Client
HashAlgorithm = HashAlgorithmName.SHA256;
SigningAlgorithm = "PKCS1-v15";
}
- #endregion
- #region Properties
///
///Gets the Api keyId
///
@@ -78,18 +75,13 @@ namespace Org.OpenAPITools.Client
///
public int SignatureValidityPeriod { get; set; }
- #endregion
-
- #region enum
private enum PrivateKeyType
{
None = 0,
RSA = 1,
ECDSA = 2,
}
- #endregion
- #region Methods
///
/// Gets the Headers for HttpSigning
///
@@ -191,12 +183,12 @@ namespace Org.OpenAPITools.Client
if (HashAlgorithm == HashAlgorithmName.SHA256)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-256={0}", Convert.ToBase64String(bodyDigest));
}
else if (HashAlgorithm == HashAlgorithmName.SHA512)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-512={0}", Convert.ToBase64String(bodyDigest));
}
else
@@ -264,7 +256,7 @@ namespace Org.OpenAPITools.Client
}
//Concatenate headers value separated by new line
var headerValuesString = string.Join("\n", headerValuesList);
- var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString);
+ var signatureStringHash = GetStringHash(HashAlgorithm, headerValuesString);
string headerSignatureStr = null;
var keyType = GetKeyType(KeyString);
@@ -300,11 +292,27 @@ namespace Org.OpenAPITools.Client
return HttpSignedRequestHeader;
}
- private byte[] GetStringHash(string hashName, string stringToBeHashed)
+ private byte[] GetStringHash(HashAlgorithmName hashAlgorithmName, string stringToBeHashed)
{
- var hashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);
- var bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
- var stringHash = hashAlgorithm.ComputeHash(bytes);
+ HashAlgorithm hashAlgorithm = null;
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA1)
+ hashAlgorithm = SHA1.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA256)
+ hashAlgorithm = SHA256.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA512)
+ hashAlgorithm = SHA512.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.MD5)
+ hashAlgorithm = MD5.Create();
+
+ if (hashAlgorithm == null)
+ throw new NullReferenceException($"{ nameof(hashAlgorithm) } was null.");
+
+ byte[] bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
+ byte[] stringHash = hashAlgorithm.ComputeHash(bytes);
return stringHash;
}
@@ -669,7 +677,7 @@ namespace Org.OpenAPITools.Client
Array.Copy(salt, 0, data00, psbytes.Length, salt.Length); //concatenate the salt bytes
// ---- do multi-hashing and concatenate results D1, D2 ... into keymaterial bytes ----
- MD5 md5 = new MD5CryptoServiceProvider();
+ MD5 md5 = MD5.Create();
byte[] result = null;
byte[] hashtarget = new byte[HASHLENGTH + data00.Length]; //fixed length initial hashtarget
@@ -782,6 +790,5 @@ namespace Org.OpenAPITools.Client
}
return apiKeyString;
}
- #endregion
}
}
diff --git a/samples/client/petstore/csharp/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 05442e501a96..e42e333d3751 100644
--- a/samples/client/petstore/csharp/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
+++ b/samples/client/petstore/csharp/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
@@ -26,7 +26,6 @@ namespace Org.OpenAPITools.Client
///
public class HttpSigningConfiguration
{
- #region
///
/// Initialize the HashAlgorithm and SigningAlgorithm to default value
///
@@ -35,9 +34,7 @@ namespace Org.OpenAPITools.Client
HashAlgorithm = HashAlgorithmName.SHA256;
SigningAlgorithm = "PKCS1-v15";
}
- #endregion
- #region Properties
///
///Gets the Api keyId
///
@@ -78,18 +75,13 @@ namespace Org.OpenAPITools.Client
///
public int SignatureValidityPeriod { get; set; }
- #endregion
-
- #region enum
private enum PrivateKeyType
{
None = 0,
RSA = 1,
ECDSA = 2,
}
- #endregion
- #region Methods
///
/// Gets the Headers for HttpSigning
///
@@ -191,12 +183,12 @@ namespace Org.OpenAPITools.Client
if (HashAlgorithm == HashAlgorithmName.SHA256)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-256={0}", Convert.ToBase64String(bodyDigest));
}
else if (HashAlgorithm == HashAlgorithmName.SHA512)
{
- var bodyDigest = GetStringHash(HashAlgorithm.ToString(), requestBody);
+ var bodyDigest = GetStringHash(HashAlgorithm, requestBody);
Digest = string.Format("SHA-512={0}", Convert.ToBase64String(bodyDigest));
}
else
@@ -264,7 +256,7 @@ namespace Org.OpenAPITools.Client
}
//Concatenate headers value separated by new line
var headerValuesString = string.Join("\n", headerValuesList);
- var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString);
+ var signatureStringHash = GetStringHash(HashAlgorithm, headerValuesString);
string headerSignatureStr = null;
var keyType = GetKeyType(KeyString);
@@ -300,11 +292,27 @@ namespace Org.OpenAPITools.Client
return HttpSignedRequestHeader;
}
- private byte[] GetStringHash(string hashName, string stringToBeHashed)
+ private byte[] GetStringHash(HashAlgorithmName hashAlgorithmName, string stringToBeHashed)
{
- var hashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);
- var bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
- var stringHash = hashAlgorithm.ComputeHash(bytes);
+ HashAlgorithm? hashAlgorithm = null;
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA1)
+ hashAlgorithm = SHA1.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA256)
+ hashAlgorithm = SHA256.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.SHA512)
+ hashAlgorithm = SHA512.Create();
+
+ if (hashAlgorithmName == HashAlgorithmName.MD5)
+ hashAlgorithm = MD5.Create();
+
+ if (hashAlgorithm == null)
+ throw new NullReferenceException($"{ nameof(hashAlgorithm) } was null.");
+
+ byte[] bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
+ byte[] stringHash = hashAlgorithm.ComputeHash(bytes);
return stringHash;
}
@@ -669,7 +677,7 @@ namespace Org.OpenAPITools.Client
Array.Copy(salt, 0, data00, psbytes.Length, salt.Length); //concatenate the salt bytes
// ---- do multi-hashing and concatenate results D1, D2 ... into keymaterial bytes ----
- MD5 md5 = new MD5CryptoServiceProvider();
+ MD5 md5 = MD5.Create();
byte[] result = null;
byte[] hashtarget = new byte[HASHLENGTH + data00.Length]; //fixed length initial hashtarget
@@ -782,6 +790,5 @@ namespace Org.OpenAPITools.Client
}
return apiKeyString;
}
- #endregion
}
}