Browse Source

Security 检查参数是否为空

Roc 6 years ago
parent
commit
0bd331efe5

+ 50 - 0
src/Essensoft.AspNetCore.Payment.Security/AES.cs

@@ -8,6 +8,21 @@ namespace Essensoft.AspNetCore.Payment.Security
     {
         public static string Encrypt(string data, string key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (string.IsNullOrEmpty(key))
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
+            if (iv == null)
+            {
+                throw new ArgumentNullException(nameof(iv));
+            }
+
             using (var aes = Aes.Create())
             {
                 aes.Key = Encoding.UTF8.GetBytes(key);
@@ -25,6 +40,21 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static string Decrypt(string data, string key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (string.IsNullOrEmpty(key))
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
+            if (iv == null)
+            {
+                throw new ArgumentNullException(nameof(iv));
+            }
+
             using (var aes = Aes.Create())
             {
                 aes.Key = Encoding.UTF8.GetBytes(key);
@@ -42,6 +72,16 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static string Encrypt(string data, string key, CipherMode cipherMode, PaddingMode paddingMode)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (string.IsNullOrEmpty(key))
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             using (var aes = Aes.Create())
             {
                 aes.Key = Encoding.UTF8.GetBytes(key);
@@ -58,6 +98,16 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static string Decrypt(string data, string key, CipherMode cipherMode, PaddingMode paddingMode)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (string.IsNullOrEmpty(key))
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             using (var aes = Aes.Create())
             {
                 aes.Key = Encoding.UTF8.GetBytes(key);

+ 60 - 0
src/Essensoft.AspNetCore.Payment.Security/AES_CTR_NoPadding.cs

@@ -9,6 +9,21 @@ namespace Essensoft.AspNetCore.Payment.Security
     {
         public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
         {
+            if (data == null)
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
+            if (iv == null)
+            {
+                throw new ArgumentNullException(nameof(iv));
+            }
+
             var cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
             cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
             return cipher.DoFinal(data);
@@ -16,6 +31,21 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
         {
+            if (data == null)
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
+            if (iv == null)
+            {
+                throw new ArgumentNullException(nameof(iv));
+            }
+
             var cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
             cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
             return cipher.DoFinal(data);
@@ -23,12 +53,42 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static string Encrypt(string data, string key, byte[] iv)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
+            if (iv == null)
+            {
+                throw new ArgumentNullException(nameof(iv));
+            }
+
             var encData = Encrypt(Encoding.UTF8.GetBytes(data), Encoding.UTF8.GetBytes(key), iv);
             return Convert.ToBase64String(encData);
         }
 
         public static string Decrypt(string data, string key, byte[] iv)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
+            if (iv == null)
+            {
+                throw new ArgumentNullException(nameof(iv));
+            }
+
             var decData = Decrypt(Convert.FromBase64String(data), Encoding.UTF8.GetBytes(key), iv);
             return Encoding.UTF8.GetString(decData);
         }

+ 20 - 0
src/Essensoft.AspNetCore.Payment.Security/HMACSHA256.cs

@@ -7,6 +7,16 @@ namespace Essensoft.AspNetCore.Payment.Security
     {
         public static string Compute(string data, string key)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (string.IsNullOrEmpty(key))
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             using (var hmacSha256 = new System.Security.Cryptography.HMACSHA256(Encoding.UTF8.GetBytes(key)))
             {
                 var hsah = hmacSha256.ComputeHash(Encoding.UTF8.GetBytes(data));
@@ -16,6 +26,16 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static byte[] Compute(byte[] data, byte[] key)
         {
+            if (data == null)
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             using (var hmacSha256 = new System.Security.Cryptography.HMACSHA256(key))
             {
                 return hmacSha256.ComputeHash(data);

+ 5 - 0
src/Essensoft.AspNetCore.Payment.Security/MD5.cs

@@ -7,6 +7,11 @@ namespace Essensoft.AspNetCore.Payment.Security
     {
         public static string Compute(string data)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
             using (var md5 = System.Security.Cryptography.MD5.Create())
             {
                 var hsah = md5.ComputeHash(Encoding.UTF8.GetBytes(data));

+ 25 - 0
src/Essensoft.AspNetCore.Payment.Security/MD5WithRSA.cs

@@ -9,6 +9,16 @@ namespace Essensoft.AspNetCore.Payment.Security
     {
         public static string SignData(string data, ICipherParameters key)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             var signer = SignerUtilities.GetSigner("MD5WithRSA");
             signer.Init(true, key);
             var bytes = Encoding.UTF8.GetBytes(data);
@@ -18,6 +28,21 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static bool VerifyData(string data, string sign, ICipherParameters key)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (string.IsNullOrEmpty(sign))
+            {
+                throw new ArgumentNullException(nameof(sign));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             var verifier = SignerUtilities.GetSigner("MD5WithRSA");
             verifier.Init(false, key);
             var bytes = Encoding.UTF8.GetBytes(data);

+ 103 - 11
src/Essensoft.AspNetCore.Payment.Security/RSAUtilities.cs

@@ -13,33 +13,67 @@ namespace Essensoft.AspNetCore.Payment.Security
     {
         public static ICipherParameters GetKeyParameterFormPrivateKey(string privateKey)
         {
+            if (string.IsNullOrEmpty(privateKey))
+            {
+                throw new ArgumentNullException(nameof(privateKey));
+            }
+
             var keyStructure = RsaPrivateKeyStructure.GetInstance(Convert.FromBase64String(privateKey));
             return new RsaPrivateCrtKeyParameters(keyStructure.Modulus, keyStructure.PublicExponent, keyStructure.PrivateExponent, keyStructure.Prime1, keyStructure.Prime2, keyStructure.Exponent1, keyStructure.Exponent2, keyStructure.Coefficient);
         }
 
         public static ICipherParameters GetKeyParameterFormPublicKey(string publicKey)
         {
+            if (string.IsNullOrEmpty(publicKey))
+            {
+                throw new ArgumentNullException(nameof(publicKey));
+            }
+
             return PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
         }
 
-        public static RSAParameters GetRSAParametersFormPrivateKey(string privateKey)
+        /// <summary>
+        /// -----BEGIN RSA PRIVATE KEY-----
+        /// ...
+        /// -----END RSA PRIVATE KEY-----
+        /// </summary>
+        /// <param name="privateKey"></param>
+        /// <returns></returns>
+        public static RSAParameters GetRSAParametersFormRsaPrivateKey(string privateKey)
         {
-            var keyStructure = RsaPrivateKeyStructure.GetInstance(Convert.FromBase64String(privateKey));
+            if (string.IsNullOrEmpty(privateKey))
+            {
+                throw new ArgumentNullException(nameof(privateKey));
+            }
+
+            var key = RsaPrivateKeyStructure.GetInstance(Convert.FromBase64String(privateKey));
             return new RSAParameters
             {
-                Modulus = keyStructure.Modulus.ToByteArrayUnsigned(),
-                Exponent = keyStructure.PublicExponent.ToByteArrayUnsigned(),
-                D = keyStructure.PrivateExponent.ToByteArrayUnsigned(),
-                P = keyStructure.Prime1.ToByteArrayUnsigned(),
-                Q = keyStructure.Prime2.ToByteArrayUnsigned(),
-                DP = keyStructure.Exponent1.ToByteArrayUnsigned(),
-                DQ = keyStructure.Exponent2.ToByteArrayUnsigned(),
-                InverseQ = keyStructure.Coefficient.ToByteArrayUnsigned()
+                D = key.PrivateExponent.ToByteArrayUnsigned(),
+                DP = key.Exponent1.ToByteArrayUnsigned(),
+                DQ = key.Exponent2.ToByteArrayUnsigned(),
+                Exponent = key.PublicExponent.ToByteArrayUnsigned(),
+                InverseQ = key.Coefficient.ToByteArrayUnsigned(),
+                Modulus = key.Modulus.ToByteArrayUnsigned(),
+                P = key.Prime1.ToByteArrayUnsigned(),
+                Q = key.Prime2.ToByteArrayUnsigned(),
             };
         }
 
+        /// <summary>
+        /// -----BEGIN PUBLIC KEY-----
+        /// ...
+        /// -----END PUBLIC KEY-----
+        /// </summary>
+        /// <param name="publicKey"></param>
+        /// <returns></returns>
         public static RSAParameters GetRSAParametersFormPublicKey(string publicKey)
         {
+            if (string.IsNullOrEmpty(publicKey))
+            {
+                throw new ArgumentNullException(nameof(publicKey));
+            }
+
             var key = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
             return new RSAParameters
             {
@@ -48,10 +82,68 @@ namespace Essensoft.AspNetCore.Payment.Security
             };
         }
 
+        /// <summary>
+        /// -----BEGIN RSA PRIVATE KEY-----
+        /// ...
+        /// -----END RSA PRIVATE KEY-----
+        /// </summary>
+        /// <param name="privateKey"></param>
+        /// <returns></returns>
+        public static AsymmetricKeyParameter GetAsymmetricKeyParameterFormRsaPrivateKey(string privateKey)
+        {
+            if (string.IsNullOrEmpty(privateKey))
+            {
+                throw new ArgumentNullException(nameof(privateKey));
+            }
+
+            var key = RsaPrivateKeyStructure.GetInstance(Convert.FromBase64String(privateKey));
+            return new RsaPrivateCrtKeyParameters(key.Modulus, key.PublicExponent, key.PrivateExponent, key.Prime1, key.Prime2, key.Exponent1, key.Exponent2, key.Coefficient);
+        }
+
+        /// <summary>
+        /// -----BEGIN PUBLIC KEY-----
+        /// ...
+        /// -----END PUBLIC KEY-----
+        /// </summary>
+        /// <param name="publicKey"></param>
+        /// <returns></returns>
+        public static AsymmetricKeyParameter GetAsymmetricKeyParameterFormPublicKey(string publicKey)
+        {
+            if (string.IsNullOrEmpty(publicKey))
+            {
+                throw new ArgumentNullException(nameof(publicKey));
+            }
+
+            return PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
+        }
+
+        /// <summary>
+        /// -----BEGIN RSA PUBLIC KEY-----
+        /// ...
+        /// -----END RSA PUBLIC KEY-----
+        /// </summary>
+        /// <param name="publicKey"></param>
+        /// <returns></returns>
+        public static AsymmetricKeyParameter GetAsymmetricKeyParameterFormRsaPublicKey(string publicKey)
+        {
+            if (string.IsNullOrEmpty(publicKey))
+            {
+                throw new ArgumentNullException(nameof(publicKey));
+            }
+
+            var key = RsaPublicKeyStructure.GetInstance(Asn1Object.FromByteArray(Convert.FromBase64String(publicKey)));
+            return new RsaKeyParameters(false, key.Modulus, key.PublicExponent);
+        }
+
         public static ICipherParameters GetPublicKeyParameterFormAsn1PublicKey(string publicKey)
         {
+            if (string.IsNullOrEmpty(publicKey))
+            {
+                throw new ArgumentNullException(nameof(publicKey));
+            }
+
             var keyStructure = RsaPublicKeyStructure.GetInstance(Asn1Object.FromByteArray(Convert.FromBase64String(publicKey)));
             return new RsaKeyParameters(false, keyStructure.Modulus, keyStructure.PublicExponent);
         }
     }
-}
+}

+ 23 - 15
src/Essensoft.AspNetCore.Payment.Security/RSA_ECB_OAEPWithSHA1AndMGF1Padding.cs

@@ -7,30 +7,38 @@ namespace Essensoft.AspNetCore.Payment.Security
 {
     public class RSA_ECB_OAEPWithSHA1AndMGF1Padding
     {
-        public static byte[] Encrypt(byte[] data, ICipherParameters key)
-        {
-            var cipher = CipherUtilities.GetCipher("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
-            cipher.Init(true, key);
-            return cipher.DoFinal(data);
-        }
-
-        public static byte[] Decrypt(byte[] data, ICipherParameters key)
-        {
-            var cipher = CipherUtilities.GetCipher("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
-            cipher.Init(false, key);
-            return cipher.DoFinal(data);
-        }
+        public const string ALGORITHM = "RSA/ECB/OAEPWithSHA1AndMGF1Padding";
 
         public static string Encrypt(string data, ICipherParameters key)
         {
-            var cipher = CipherUtilities.GetCipher("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
+            var cipher = CipherUtilities.GetCipher(ALGORITHM);
             cipher.Init(true, key);
             return Convert.ToBase64String(cipher.DoFinal(Encoding.UTF8.GetBytes(data)));
         }
 
         public static string Decrypt(string data, ICipherParameters key)
         {
-            var cipher = CipherUtilities.GetCipher("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
+            var cipher = CipherUtilities.GetCipher(ALGORITHM);
             cipher.Init(false, key);
             return Encoding.UTF8.GetString(cipher.DoFinal(Convert.FromBase64String(data)));
         }

+ 40 - 0
src/Essensoft.AspNetCore.Payment.Security/RSA_ECB_PKCS1Padding.cs

@@ -9,6 +9,16 @@ namespace Essensoft.AspNetCore.Payment.Security
     {
         public static byte[] Encrypt(byte[] data, ICipherParameters key)
         {
+            if (data == null)
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             var cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
             cipher.Init(true, key);
             return cipher.DoFinal(data);
@@ -16,6 +26,16 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static byte[] Decrypt(byte[] data, ICipherParameters key)
         {
+            if (data == null)
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             var cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
             cipher.Init(false, key);
             return cipher.DoFinal(data);
@@ -23,6 +43,16 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static string Encrypt(string data, ICipherParameters key)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             var cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
             cipher.Init(true, key);
             return Convert.ToBase64String(cipher.DoFinal(Encoding.UTF8.GetBytes(data)));
@@ -30,6 +60,16 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static string Decrypt(string data, ICipherParameters key)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             var cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
             cipher.Init(false, key);
             return Encoding.UTF8.GetString(cipher.DoFinal(Convert.FromBase64String(data)));

+ 40 - 0
src/Essensoft.AspNetCore.Payment.Security/RSA_NONE_PKCS1Padding.cs

@@ -9,6 +9,16 @@ namespace Essensoft.AspNetCore.Payment.Security
     {
         public static byte[] Encrypt(byte[] data, ICipherParameters key)
         {
+            if (data == null)
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             var cipher = CipherUtilities.GetCipher("RSA/NONE/PKCS1Padding");
             cipher.Init(true, key);
             return cipher.DoFinal(data);
@@ -16,6 +26,16 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static byte[] Decrypt(byte[] data, ICipherParameters key)
         {
+            if (data == null)
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             var cipher = CipherUtilities.GetCipher("RSA/NONE/PKCS1Padding");
             cipher.Init(false, key);
             return cipher.DoFinal(data);
@@ -23,6 +43,16 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static string Encrypt(string data, ICipherParameters key)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             var cipher = CipherUtilities.GetCipher("RSA/NONE/PKCS1Padding");
             cipher.Init(true, key);
             return Convert.ToBase64String(cipher.DoFinal(Encoding.UTF8.GetBytes(data)));
@@ -30,6 +60,16 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static string Decrypt(string data, ICipherParameters key)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             var cipher = CipherUtilities.GetCipher("RSA/NONE/PKCS1Padding");
             cipher.Init(false, key);
             return Encoding.UTF8.GetString(cipher.DoFinal(Convert.FromBase64String(data)));

+ 5 - 0
src/Essensoft.AspNetCore.Payment.Security/SHA1.cs

@@ -7,6 +7,11 @@ namespace Essensoft.AspNetCore.Payment.Security
     {
         public static string Compute(string data)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
             var sha1 = System.Security.Cryptography.SHA1.Create();
             var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(data));
             return BitConverter.ToString(hash).Replace("-", "").ToLower();

+ 28 - 14
src/Essensoft.AspNetCore.Payment.Security/SHA1WithRSA.cs

@@ -1,28 +1,42 @@
 using System;
+using System.Security.Cryptography;
 using System.Text;
-using Org.BouncyCastle.Crypto;
-using Org.BouncyCastle.Security;
 
 namespace Essensoft.AspNetCore.Payment.Security
 {
     public class SHA1WithRSA
     {
-        public static string SignData(string data, ICipherParameters key)
+        public static string Sign(string data, RSAParameters privateKey)
         {
-            var signer = SignerUtilities.GetSigner("SHA1WithRSA");
-            signer.Init(true, key);
-            var bytes = Encoding.UTF8.GetBytes(data);
-            signer.BlockUpdate(bytes, 0, bytes.Length);
-            return Convert.ToBase64String(signer.GenerateSignature());
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            using (var rsa = RSA.Create())
+            {
+                rsa.ImportParameters(privateKey);
+                return Convert.ToBase64String(rsa.SignData(Encoding.UTF8.GetBytes(data), HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1));
+            }
         }
 
-        public static bool VerifyData(string data, string sign, ICipherParameters key)
+        public static bool Verify(string data, string sign, RSAParameters publicKey)
         {
-            var verifier = SignerUtilities.GetSigner("SHA1WithRSA");
-            verifier.Init(false, key);
-            var bytes = Encoding.UTF8.GetBytes(data);
-            verifier.BlockUpdate(bytes, 0, bytes.Length);
-            return verifier.VerifySignature(Convert.FromBase64String(sign));
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (string.IsNullOrEmpty(sign))
+            {
+                throw new ArgumentNullException(nameof(sign));
+            }
+
+            using (var rsa = RSA.Create())
+            {
+                rsa.ImportParameters(publicKey);
+                return rsa.VerifyData(Encoding.UTF8.GetBytes(data), Convert.FromBase64String(sign), HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
+            }
         }
     }
 }

+ 5 - 0
src/Essensoft.AspNetCore.Payment.Security/SHA256.cs

@@ -7,6 +7,11 @@ namespace Essensoft.AspNetCore.Payment.Security
     {
         public static string Compute(string data)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
             var sha256 = System.Security.Cryptography.SHA256.Create();
             var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(data));
             return BitConverter.ToString(hash).Replace("-", "").ToLower();

+ 59 - 0
src/Essensoft.AspNetCore.Payment.Security/SHA256WithRSA.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Security.Cryptography;
 using System.Text;
 using Org.BouncyCastle.Crypto;
 using Org.BouncyCastle.Security;
@@ -7,8 +8,51 @@ namespace Essensoft.AspNetCore.Payment.Security
 {
     public class SHA256WithRSA
     {
+        public static string Sign(string data, RSAParameters privateKey)
+        {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            using (var rsa = RSA.Create())
+            {
+                rsa.ImportParameters(privateKey);
+                return Convert.ToBase64String(rsa.SignData(Encoding.UTF8.GetBytes(data), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
+            }
+        }
+
+        public static bool Verify(string data, string sign, RSAParameters publicKey)
+        {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (string.IsNullOrEmpty(sign))
+            {
+                throw new ArgumentNullException(nameof(sign));
+            }
+
+            using (var rsa = RSA.Create())
+            {
+                rsa.ImportParameters(publicKey);
+                return rsa.VerifyData(Encoding.UTF8.GetBytes(data), Convert.FromBase64String(sign), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
+            }
+        }
+
         public static string SignData(string data, ICipherParameters key)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             var signer = SignerUtilities.GetSigner("SHA256WithRSA");
             signer.Init(true, key);
             var bytes = Encoding.UTF8.GetBytes(data);
@@ -18,6 +62,21 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static bool VerifyData(string data, string sign, ICipherParameters key)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (string.IsNullOrEmpty(sign))
+            {
+                throw new ArgumentNullException(nameof(sign));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
             var verifier = SignerUtilities.GetSigner("SHA256WithRSA");
             verifier.Init(false, key);
             var bytes = Encoding.UTF8.GetBytes(data);

+ 5 - 0
src/Essensoft.AspNetCore.Payment.Security/SM3.cs

@@ -9,6 +9,11 @@ namespace Essensoft.AspNetCore.Payment.Security
     {
         public static string Compute(string data)
         {
+            if (string.IsNullOrEmpty(data))
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
             var digest = new SM3Digest();
             var bytes = Encoding.UTF8.GetBytes(data);
             digest.BlockUpdate(bytes, 0, bytes.Length);

+ 32 - 1
src/Essensoft.AspNetCore.Payment.Security/TripleDES.cs

@@ -1,4 +1,5 @@
-using System.Security.Cryptography;
+using System;
+using System.Security.Cryptography;
 
 namespace Essensoft.AspNetCore.Payment.Security
 {
@@ -6,6 +7,21 @@ namespace Essensoft.AspNetCore.Payment.Security
     {
         public static byte[] Encode(byte[] data, byte[] key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode)
         {
+            if (data == null)
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
+            if (iv == null)
+            {
+                throw new ArgumentNullException(nameof(iv));
+            }
+
             using (var des = System.Security.Cryptography.TripleDES.Create())
             {
                 des.Key = key;
@@ -22,6 +38,21 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static byte[] Decode(byte[] data, byte[] key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode)
         {
+            if (data == null)
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+
+            if (key == null)
+            {
+                throw new ArgumentNullException(nameof(key));
+            }
+
+            if (iv == null)
+            {
+                throw new ArgumentNullException(nameof(iv));
+            }
+
             using (var des = System.Security.Cryptography.TripleDES.Create())
             {
                 des.Key = key;