Ejemplo n.º 1
0
        public static byte[] AsymmetricEndecrypt<TAsym>(Func<TAsym, byte[]> endecryptFactory)
            where TAsym : AsymmetricAlgorithm
        {
            endecryptFactory.NotNull(nameof(endecryptFactory));

            var algo = AsymmetricAlgorithms.GetOrAdd(typeof(TAsym).Name,
                key => AsymmetricAlgorithm.Create(key));

            return ExtensionSettings.Preference.RunLocker(() => endecryptFactory.Invoke(algo as TAsym));
        }
Ejemplo n.º 2
0
        public static bool IsSignedBy(TmodFile mod, string xmlPublicKey)
        {
            var f = new RSAPKCS1SignatureDeformatter();
            var v = AsymmetricAlgorithm.Create("RSA");

            f.SetHashAlgorithm("SHA1");
            v.FromXmlString(xmlPublicKey);
            f.SetKey(v);
            return(f.VerifySignature(mod.hash, mod.signature));
        }
Ejemplo n.º 3
0
        public static void NamedCreate_Unknown()
        {
            const string UnknownAlgorithmName = "XYZZY";

            Assert.Null(AsymmetricAlgorithm.Create(UnknownAlgorithmName));
            Assert.Null(HashAlgorithm.Create(UnknownAlgorithmName));
            Assert.Null(KeyedHashAlgorithm.Create(UnknownAlgorithmName));
            Assert.Null(HMAC.Create(UnknownAlgorithmName));
            Assert.Null(SymmetricAlgorithm.Create(UnknownAlgorithmName));
        }
Ejemplo n.º 4
0
        public static void DefaultStaticCreateMethods()
        {
#pragma warning disable SYSLIB0007 // These methods are marked as Obsolete
            // .NET Core does not allow the base classes to pick an algorithm.
            Assert.Throws <PlatformNotSupportedException>(() => AsymmetricAlgorithm.Create());
            Assert.Throws <PlatformNotSupportedException>(() => HashAlgorithm.Create());
            Assert.Throws <PlatformNotSupportedException>(() => KeyedHashAlgorithm.Create());
            Assert.Throws <PlatformNotSupportedException>(() => HMAC.Create());
            Assert.Throws <PlatformNotSupportedException>(() => SymmetricAlgorithm.Create());
#pragma warning restore SYSLIB0007
        }
Ejemplo n.º 5
0
        public void WrongVerifyWithEC()
        {
            JsonWebSignature jws = new JsonWebSignature
            {
                Protected = "eyJhbGciOiJFUzI1NiIsImp3ayI6eyJrdHkiOiJFQyIsImNydiI6IlAtMjU2Iiwia2V5X29wcyI6WyJ2ZXJpZnkiXSwiZXh0Ijp0cnVlLCJ4IjoibXdhdW9YbWIwcllBTzBKWTJxNlgxV0hiVS1nVm5aY2pjcE5oLVRNVXotMCIsInkiOiJ5VlhySjZZeXd0aUtkVVNPb2hVVDUyVEhsYnQ4QVl0V2lLV1gtNldYYjUwIn0sInVybCI6Imh0dHBzOi8vYWNtZS50ZXN0LmNvbS9wYXRoIiwibm9uY2UiOiJkR1Z6ZENCdFpYTnpZV2RsIn0",
                Payload   = "eyJvbmx5UmV0dXJuRXhpc3RpbmciOnRydWV9",
                Signature = "l8B0vPu_ZlfnIU00BKrFZPXRHzQd2qfGKPNCQqkUt7q8vmwdTPzvlFYRaLA2Q25GtP6K9IFUr72UpEPKBc1auw",
            };

            AsymmetricAlgorithm key = AsymmetricAlgorithm.Create("ECDsa");

            Assert.False(jws.Verify(key));
        }
Ejemplo n.º 6
0
        public void WrongVerifyWithRSA()
        {
            JsonWebSignature jws = new JsonWebSignature
            {
                Protected = "eyJhbGciOiJSUzI1NiIsImp3ayI6eyJrdHkiOiJSU0EiLCJhbGciOiJSUzI1NiIsImtleV9vcHMiOlsidmVyaWZ5Il0sImV4dCI6dHJ1ZSwibiI6InNxVUJubG9nM1dFMWh5RUc5QlNSZjhqNEJ6TjY3UHJvekpEUUpSU3NGVWJmVDhiQ2x6ekxtTlo1VTFVaVN3bEQxUUhhVml6VWx4SXZ5Tl9QeHZBTHZzenAwOVhfbnRodXRUQjYyeXRtQVN1NnY3SG95RGVabURKeUdaTzI3bGNoaXJPQ1hNVFIzZ0FyMGlYTEhwaTdfQXN4YTZuWlNsZlhqV2JILWNvd296cjJXX1NKSXExOGNNUi0yQlJ6RW4xQzNUOThnQU1LcWJ3WEJnYzhHWFg1N1ZMcTNvQjlCUkhVTUJKdEFldFdRUW1aTE9qdkk5RXNfQVVfWEVTaUNJaFJWYWh5Sk5RRllrS21NMkowcHhOZTl0WTZMRFdLX211bXVfeFVxbEZxOGlNTWtEUm9jMXdqdVMteDFJWGo5cmpTWGVERXRzY0NkY1UyeHM2Um4tVGVZdyIsImUiOiJBUUFCIn0sInVybCI6Imh0dHBzOi8vYWNtZS50ZXN0LmNvbS9wYXRoIiwibm9uY2UiOiJkR1Z6ZENCdFpYTnpZV2RsIn0",
                Payload   = "",
                Signature = "GoZY7BqVtUcrhrMaX1ETzs_NOu6p08sDIl4ZYpFBa-REeUYxuHimJRCIx2Z7p-59UYO9LmRP15VFT7jdku6qIURhJADugd57WQ53WvmburXi3fXGW2Gmx6qfPb9mvPs_-A9U9W0I9DPa4w6Qee408nFO8_XzyYgs5v3hHqSef1n-0lFtO3RiAOFynWf12Y5Bd-rEqCmW3OHw-HTUPz2I8u_uFEDn5ebWQ_yMb2gtxTuKTYP_sbmWJe3Kt3BpVD3FqZrdgyGfhNxqlnhzPMzaV7MldZnZ1z0sodlEZdAXmudgQMoppOGwBw-WRgtf6H4yAqBeKTfZX1-iU01IgKqUKA",
            };

            AsymmetricAlgorithm key = AsymmetricAlgorithm.Create("RSA");

            Assert.False(jws.Verify(key));
        }
Ejemplo n.º 7
0
 public static void StaticCreateMethods()
 {
     // These are not supported because CryptoConfig exists in Algorithms assembly.
     // CryptoConfig exists in Algorithms partly because it requires the Oid class in Encoding assembly.
     Assert.Throws <PlatformNotSupportedException>(() => AsymmetricAlgorithm.Create());
     Assert.Throws <PlatformNotSupportedException>(() => AsymmetricAlgorithm.Create(null));
     Assert.Throws <PlatformNotSupportedException>(() => HashAlgorithm.Create());
     Assert.Throws <PlatformNotSupportedException>(() => HashAlgorithm.Create(null));
     Assert.Throws <PlatformNotSupportedException>(() => KeyedHashAlgorithm.Create());
     Assert.Throws <PlatformNotSupportedException>(() => KeyedHashAlgorithm.Create(null));
     Assert.Throws <PlatformNotSupportedException>(() => HMAC.Create());
     Assert.Throws <PlatformNotSupportedException>(() => HMAC.Create(null));
     Assert.Throws <PlatformNotSupportedException>(() => SymmetricAlgorithm.Create());
     Assert.Throws <PlatformNotSupportedException>(() => SymmetricAlgorithm.Create(null));
 }
Ejemplo n.º 8
0
 public void DsaCalls()
 {
     using (var dsa = new DSACryptoServiceProvider()) // Noncompliant
     {
     }
     using (var dsa = DSA.Create()) // Noncompliant
     {
     }
     using (var dsa = (AsymmetricAlgorithm)CryptoConfig.CreateFromName("DSA")) // Noncompliant
     {
     }
     using (var dsa = AsymmetricAlgorithm.Create("DSA")) // Noncompliant
     {
     }
 }
Ejemplo n.º 9
0
        public void SignWithRSA()
        {
            JsonWebSignature jws = new JsonWebSignature();

            var @protected = new JsonWebSignatureProtected
            {
                Algorithm = AlgorithmsEnum.RS256,
                Key       = new JsonWebKey
                {
                    Algorithm = AlgorithmsEnum.RS256,
                    KeyType   = KeyTypesEnum.RSA,
                }
            };

            jws.SetProtected(@protected);

            AsymmetricAlgorithm key = AsymmetricAlgorithm.Create("RSA");

            jws.Sign(key);

            Assert.True(jws.Verify(key));
        }
Ejemplo n.º 10
0
 public static void NamedAsymmetricAlgorithmCreate_DSA_NotSupported(string identifier)
 {
     Assert.Null(AsymmetricAlgorithm.Create(identifier));
 }
        static void Main(string[] args)
        {
            SignatueFormatterDeformatter sigfd = new SignatueFormatterDeformatter("SHA1", AsymmetricAlgorithm.Create("DSA"));
            string message = "this is a test message";

            byte[] sign      = sigfd.Sign(message);
            bool   fVerified = sigfd.Verify(message, sign);

            KeyExchangeFormatterDeformatter kefd = new KeyExchangeFormatterDeformatter(AsymmetricAlgorithm.Create("RSA"));
            string secretkey = "secretkey";              //In real applications this should be a symmetric key

            byte[] envelope   = kefd.CreateEnvelope(secretkey);
            string secretkey2 = kefd.GetSecretKey(envelope);
            bool   fSame      = secretkey == secretkey2;
        }
Ejemplo n.º 12
0
 protected override void SetUp()
 {
     _algo = AsymmetricAlgorithm.Create();
 }
Ejemplo n.º 13
0
 public void SetUp()
 {
     _algo = AsymmetricAlgorithm.Create();
 }
        public IHttpActionResult Asymmetric()
        {
            var asymmetric = AsymmetricAlgorithm.Create();

            return(Json(asymmetric));
        }
Ejemplo n.º 15
0
 private void Form1_Click(object sender, EventArgs e)
 {
     SymmetricAlgorithm  sa = SymmetricAlgorithm.Create();
     AsymmetricAlgorithm aa = AsymmetricAlgorithm.Create();
     HashAlgorithm       ha = HashAlgorithm.Create();
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Returns a AsymmetricAlgorithm object specified in the profile
        /// </summary>
        /// <param name="profile">profile name</param>
        /// <returns>AsymmetricAlgorithm object</returns>
        public AsymmetricAlgorithm GetAymmetricAlgorithmProvider(string profile)
        {
            XmlNode xml = SearchCryptoInfoByProfileName(profile);

            return(AsymmetricAlgorithm.Create(xml.Attributes["algorithm"].Value));
        }