Esempio n. 1
0
        /// <summary>
        /// Sets a specific crypto service provider instance.
        /// </summary>
        /// <param name="privateKey">The RSA private key.</param>
        /// <param name="syncEncryptKey">true if set the secret encryption key from the crypto service provider; otherwise, false.</param>
        public void SetCrypto(string privateKey, bool syncEncryptKey = false)
        {
            var key = RSAParametersConvert.Parse(privateKey);

            if (!key.HasValue)
            {
                ClearCrypto(syncEncryptKey);
                return;
            }

            SetCrypto(key.Value, syncEncryptKey);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates an RSA hash signature provider.
        /// </summary>
        /// <param name="secret">The RSA key.</param>
        /// <param name="hashAlgorithmName">The hash algorithm name.</param>
        /// <param name="signAlgorithmName">The signature algorithm name.</param>
        /// <returns>An RSA hash signature provider instance.</returns>
        private static RSASignatureProvider Create(string secret, HashAlgorithmName hashAlgorithmName, string signAlgorithmName)
        {
            if (string.IsNullOrWhiteSpace(secret))
            {
                throw new ArgumentNullException(nameof(secret), "secret should not be null, empty or consists only of white-space characters.");
            }
            var p = RSAParametersConvert.Parse(secret);

            if (p == null)
            {
                throw new FormatException("secret is not a valid RSA key. A PEM string or XML string expected.");
            }
            return(new RSASignatureProvider(p.Value, hashAlgorithmName, signAlgorithmName));
        }
Esempio n. 3
0
 /// <summary>
 /// Gets the secret encrypted.
 /// </summary>
 /// <param name="key">The secret encryption key to use to override the original one set.</param>
 /// <param name="padding">The optional padding mode for decryption.</param>
 /// <returns>The Base64 string with secret encrypted.</returns>
 public string EncryptSecret(string key, RSAEncryptionPadding padding = null)
 {
     return(EncryptSecret(RSAParametersConvert.Parse(key), padding));
 }
Esempio n. 4
0
        protected override async Task OnProcessAsync(CancellationToken cancellationToken = default)
        {
            var codeTokenReq = new CodeTokenRequest(new CodeTokenRequestBody
            {
                Code = "hijklmn\r\nopq\trst"
            }, "abcd", "efg")
            {
                ScopeString = "test plain"
            };
            await Task.Run(() => { }, cancellationToken);

            var tokenUrl = codeTokenReq.ToJsonString();

            codeTokenReq = CodeTokenRequest.Parse(tokenUrl);
            tokenUrl     = codeTokenReq.ToQueryData().ToString();
            codeTokenReq = CodeTokenRequest.Parse(tokenUrl);
            tokenUrl     = codeTokenReq.ToJsonString();
            codeTokenReq = CodeTokenRequest.Parse(tokenUrl);
            Console.WriteLine(codeTokenReq.ToQueryData().ToString());
            Console.WriteLine();

            // JWT HS512
            var hs  = HashSignatureProvider.CreateHS512("a secret string");
            var jwt = new JsonWebToken <Net.HttpClientVerb.NameAndDescription>(new Net.HttpClientVerb.NameAndDescription
            {
                Name        = "abcd",
                Description = "efg"
            }, hs);
            var header = jwt.ToAuthenticationHeaderValue();

            jwt = JsonWebToken <Net.HttpClientVerb.NameAndDescription> .Parse(header.ToString(), hs);

            var jwtStr = jwt.ToEncodedString();

            Console.WriteLine(jwtStr != header.Parameter ? "Failed JWT HS512 testing." : jwtStr);
            Console.WriteLine();

            // RSA.
            var rsa        = RSA.Create();
            var privateKey = rsa.ExportParameters(true).ToPrivatePEMString(true);

            Console.WriteLine(privateKey);
            var publicKey = rsa.ExportParameters(false).ToPublicPEMString();

            Console.WriteLine(publicKey);
            var privateKeyP = RSAParametersConvert.Parse(privateKey).Value;
            var privateKeyS = privateKeyP.ToPrivatePEMString(true);
            var publicKeyP  = RSAParametersConvert.Parse(publicKey).Value;
            var publicKeyS  = publicKeyP.ToPublicPEMString();

            Console.WriteLine("They are {0}.", (privateKey == privateKeyS) && (publicKey == publicKeyS) ? "same" : "different");
            Console.WriteLine();

            // JWT RS512
            var rs = RSASignatureProvider.CreateRS512(rsa);

            jwt    = new JsonWebToken <Net.HttpClientVerb.NameAndDescription>(jwt.Payload, rs);
            header = jwt.ToAuthenticationHeaderValue();
            jwt    = JsonWebToken <Net.HttpClientVerb.NameAndDescription> .Parse(header.ToString(), rs);

            jwtStr = jwt.ToEncodedString();
            Console.WriteLine(jwtStr != header.Parameter ? "Failed JWT RS512 testing." : header.Parameter);
            Console.WriteLine();
        }