Example #1
0
        public byte[] Sign(byte[] securedInput, object key)
        {
            var privateKey = Ensure.Type <RSACryptoServiceProvider>(key, "RsaUsingSha with PSS padding alg expects key to be of RSACryptoServiceProvider type.");

            try
            {
                return(RsaPss.Sign(securedInput, RsaKey.New(privateKey.ExportParameters(true)), Hash, saltSize));
            }
            catch (CryptographicException e)
            {
                throw new JoseException("Unable to sign content.", e);
            }
        }
Example #2
0
        public byte[] Sign(byte[] securedInput, object key)
        {
            byte[] numArray;
            RSACryptoServiceProvider rSACryptoServiceProvider = Ensure.Type <RSACryptoServiceProvider>(key, "RsaUsingSha with PSS padding alg expects key to be of RSACryptoServiceProvider type.", new object[0]);

            try
            {
                numArray = RsaPss.Sign(securedInput, RsaKey.New(rSACryptoServiceProvider.ExportParameters(true)), this.Hash, this.saltSize);
            }
            catch (CryptographicException cryptographicException)
            {
                throw new JoseException("Unable to sign content.", cryptographicException);
            }
            return(numArray);
        }
Example #3
0
        public byte[] Sign(byte[] securedInput, object key)
        {
#if NET40
            var privateKey = Ensure.Type <RSACryptoServiceProvider>(key, "RsaUsingSha with PSS padding alg expects key to be of RSACryptoServiceProvider type.");

            try
            {
                return(RsaPss.Sign(securedInput, RsaKey.New(privateKey.ExportParameters(true)), Hash, saltSize));
            }
            catch (CryptographicException e)
            {
                throw new JoseException("Unable to sign content.", e);
            }
#elif NETSTANDARD1_4
            var privateKey = Ensure.Type <RSA>(key, "RsaUsingSha with PSS padding alg expects key to be of RSA type.");
            return(privateKey.SignData(securedInput, HashAlgorithm, RSASignaturePadding.Pss));
#endif
        }
Example #4
0
        public byte[] Sign(byte[] securedInput, object key)
        {
#if NET40 || NET461
            var privateKey = Ensure.Type <CngKey>(key, "RsaUsingSha with PSS padding alg expects key to be of CngKey type.");

            try
            {
                return(RsaPss.Sign(securedInput, privateKey, Hash, saltSize));
            }
            catch (CryptographicException e)
            {
                throw new JoseException("Unable to sign content.", e);
            }
#elif NETSTANDARD1_4
            var privateKey = Ensure.Type <RSA>(key, "RsaUsingSha with PSS padding alg expects key to be of RSA type.");
            return(privateKey.SignData(securedInput, HashAlgorithm, RSASignaturePadding.Pss));
#endif
        }
Example #5
0
        public byte[] Sign(byte[] securedInput, object key)
        {
    #if NET40
            if (key is CngKey)
            {
                var privateKey = (CngKey)key;

                try
                {
                    return(RsaPss.Sign(securedInput, privateKey, Hash, saltSize));
                }
                catch (CryptographicException e)
                {
                    throw new JoseException("Unable to sign content.", e);
                }
            }

            else if (key is RSACryptoServiceProvider)
            {
                //This is for backward compatibility only with 2.x
                //To be removed in 3.x
                var privateKey = RsaKey.New(((RSACryptoServiceProvider)key).ExportParameters(true));

                try
                {
                    return(RsaPss.Sign(securedInput, privateKey, Hash, saltSize));
                }
                catch (CryptographicException e)
                {
                    throw new JoseException("Unable to sign content.", e);
                }
            }

            throw new ArgumentException("RsaUsingSha with PSS padding alg expects key to be of CngKey type.");
    #elif NET461
            if (key is CngKey)
            {
                var privateKey = (CngKey)key;

                try
                {
                    return(RsaPss.Sign(securedInput, privateKey, Hash, saltSize));
                }
                catch (CryptographicException e)
                {
                    throw new JoseException("Unable to sign content.", e);
                }
            }

            if (key is RSACryptoServiceProvider)
            {
                //This is for backward compatibility only with 2.x
                //To be removed in 3.x
                var privateKey = RsaKey.New(((RSACryptoServiceProvider)key).ExportParameters(true));

                try
                {
                    return(RsaPss.Sign(securedInput, privateKey, Hash, saltSize));
                }
                catch (CryptographicException e)
                {
                    throw new JoseException("Unable to sign content.", e);
                }
            }

            if (key is RSA)
            {
                var privateKey = (RSA)key;
                return(privateKey.SignData(securedInput, HashAlgorithm, RSASignaturePadding.Pss));
            }

            throw new ArgumentException("RsaUsingSha with PSS padding alg expects key to be of either CngKey or RSA types.");
    #elif NETSTANDARD
            var privateKey = Ensure.Type <RSA>(key, "RsaUsingSha with PSS padding alg expects key to be of RSA type.");

            return(privateKey.SignData(securedInput, HashAlgorithm, RSASignaturePadding.Pss));
    #endif
        }
Example #6
0
        public byte[] Sign(byte[] securedInput, object key)
        {
#if NET40
            if (key is CngKey cngKey)
            {
                try
                {
                    return(RsaPss.Sign(securedInput, cngKey, Hash, saltSize));
                }
                catch (CryptographicException e)
                {
                    throw new JoseException("Unable to sign content.", e);
                }
            }
            else if (key is RSACryptoServiceProvider rsaKey)
            {
                //This is for backward compatibility only with 2.x
                //To be removed in 3.x
                var privateKey = RsaKey.New(rsaKey.ExportParameters(true));

                try
                {
                    return(RsaPss.Sign(securedInput, privateKey, Hash, saltSize));
                }
                catch (CryptographicException e)
                {
                    throw new JoseException("Unable to sign content.", e);
                }
            }

            throw new ArgumentException("RsaUsingSha with PSS padding alg expects key to be of CngKey type.");
#elif NET461 || NET472
            if (key is CngKey cngKey)
            {
                try
                {
                    return(RsaPss.Sign(securedInput, cngKey, Hash, saltSize));
                }
                catch (CryptographicException e)
                {
                    throw new JoseException("Unable to sign content.", e);
                }
            }
            else if (key is RSACryptoServiceProvider rsaKey)
            {
                //This is for backward compatibility only with 2.x
                //To be removed in 3.x
                var privateKey = RsaKey.New(rsaKey.ExportParameters(true));

                try
                {
                    return(RsaPss.Sign(securedInput, privateKey, Hash, saltSize));
                }
                catch (CryptographicException e)
                {
                    throw new JoseException("Unable to sign content.", e);
                }
            }
            else if (key is RSA rsa)
            {
                return(rsa.SignData(securedInput, HashAlgorithm, RSASignaturePadding.Pss));
            }
            else if (key is Jwk jwk)
            {
                if (jwk.Kty == Jwk.KeyTypes.RSA)
                {
                    return(jwk.RsaKey().SignData(securedInput, HashAlgorithm, RSASignaturePadding.Pss));
                }
            }

            throw new ArgumentException("RsaUsingSha with PSS padding alg expects key to be of either CngKey or RSA types or Jwk type with kty='RSA'");
#elif NETSTANDARD
            if (key is RSA rsa)
            {
                return(rsa.SignData(securedInput, HashAlgorithm, RSASignaturePadding.Pss));
            }
            else if (key is Jwk jwk)
            {
                if (jwk.Kty == Jwk.KeyTypes.RSA)
                {
                    return(jwk.RsaKey().SignData(securedInput, HashAlgorithm, RSASignaturePadding.Pss));
                }
            }

            throw new ArgumentException("RsaUsingSha with PSS padding alg expects key to be of either RSA type or Jwk type with kty='RSA'");
#endif
        }