Exemple #1
0
        public bool VerifyAuthCookieV1(string cookie, string sign)
        {
            byte[] publicKey = this.GetPublicKey(KeyType.AuthCookieV1);

            using (var rsa = new RSACryptoServiceProvider())
            {
                byte[] originalData = System.Text.Encoding.UTF8.GetBytes(cookie);
                byte[] signedBytes  = Convert.FromBase64String(sign);

                try
                {
                    // Import the private key used for signing the message
                    rsa.ImportParameters(SecureSigningService.FromBinaryToRSAParameters(publicKey));

                    return(rsa.VerifyData(originalData, CryptoConfig.MapNameToOID("SHA512"), signedBytes));
                }
                catch (CryptographicException e)
                {
                    return(false);
                }
                finally
                {
                    //// Set the keycontainer to be cleared when rsa is garbage collected.
                    rsa.PersistKeyInCsp = false;
                }
            }
        }
Exemple #2
0
        public string SignAuthCookieV1(string cookie)
        {
            byte[] privateKey = this.GetPrivateKey(KeyType.AuthCookieV1);

            //// The array to store the signed message in bytes
            byte[] signedBytes;
            using (var rsa = new RSACryptoServiceProvider())
            {
                // Write the message to a byte array using UTF8 as the encoding.

                byte[] originalData = System.Text.Encoding.UTF8.GetBytes(cookie);

                try
                {
                    // Import the private key used for signing the message
                    rsa.ImportParameters(SecureSigningService.FromBinaryToRSAParameters(privateKey));

                    signedBytes = rsa.SignData(originalData, CryptoConfig.MapNameToOID("SHA512"));
                }
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.Message);
                    return(null);
                }
                finally
                {
                    //// Set the keycontainer to be cleared when rsa is garbage collected.
                    rsa.PersistKeyInCsp = false;
                }
            }

            // Convert the a base64 string before returning
            return(Convert.ToBase64String(signedBytes));
        }