Exemplo n.º 1
0
        public void Roundtrip_RndIV_Success()
        {
            const string vendorGuid = "abc";
            const bool   isRead     = false;

            var str = "{\"filters\": [{\"property_name\": \"vendor_id\",\"operator\": \"eq\",\"property_value\": \"VENDOR_GUID\"}],\"allowed_operations\": [ \"READ_OR_WRITE\" ]}";

            str = str.Replace("VENDOR_GUID", vendorGuid);

            str = str.Replace("READ_OR_WRITE", isRead ? "read" : "write");

            var rnd   = new System.Security.Cryptography.RNGCryptoServiceProvider();
            var bytes = new byte[16];

            rnd.GetBytes(bytes);

            var iv = String.Concat(bytes.Select(b => b.ToString("X2"))); Trace.WriteLine("IV: " + iv);

            Trace.WriteLine("plaintext: " + str);
            var scopedKey = ScopedKey.EncryptString(SettingsEnv.MasterKey, str, iv);

            Trace.WriteLine("encrypted: " + scopedKey);
            var decrypted = ScopedKey.Decrypt(SettingsEnv.MasterKey, scopedKey);

            Trace.WriteLine("decrypted: " + decrypted);
        }
Exemplo n.º 2
0
        public void AddEvent_ScopedKeyWrite_Success()
        {
            const string scope     = "{\"timestamp\": \"2014-02-25T22:09:27.320082\", \"allowed_operations\": [\"write\"]}";
            var          scopedKey = ScopedKey.EncryptString(SettingsEnv.MasterKey, scope);
            var          settings  = new ProjectSettingsProvider(masterKey: SettingsEnv.MasterKey, projectId: SettingsEnv.ProjectId, writeKey: scopedKey);

            var client = new KeenClient(settings);

            if (UseMocks)
            {
                client.EventCollection = new EventCollectionMock(settings,
                                                                 addEvent: (c, e, p) =>
                {
                    var key = JObject.Parse(ScopedKey.Decrypt(p.MasterKey, p.WriteKey));

                    if ((key["allowed_operations"].Values <string>().First() == "write") && (p == settings) && (c == "AddEventTest") && (e["AProperty"].Value <string>() == "CustomKey"))
                    {
                        return;
                    }

                    throw new Exception("Unexpected value");
                });
            }

            Assert.DoesNotThrow(() => client.AddEvent("AddEventTest", new { AProperty = "CustomKey" }));
        }
Exemplo n.º 3
0
        public void Roundtrip_RndIV_Success()
        {
            const string vendor_guid = "abc";
            const bool   isRead      = false;

            var str = "{\"filters\": [{\"property_name\": \"vendor_id\",\"operator\": \"eq\",\"property_value\": \"VENDOR_GUID\"}],\"allowed_operations\": [ \"READ_OR_WRITE\" ]}";

            str = str.Replace("VENDOR_GUID", vendor_guid);

            if (isRead)
            {
                str = str.Replace("READ_OR_WRITE", "read");
            }
            else
            {
                str = str.Replace("READ_OR_WRITE", "write");
            }

            var rnd = new System.Security.Cryptography.RNGCryptoServiceProvider();

            byte[] bytes = new byte[16];
            rnd.GetBytes(bytes);

            var IV = String.Concat(bytes.Select(b => b.ToString("X2"))); Trace.WriteLine("IV: " + IV);

            var scopedKey = ScopedKey.EncryptString(SettingsEnv.MasterKey, str, IV); //System.Text.Encoding.Default.GetString(bytes));
            var decrypted = ScopedKey.Decrypt(SettingsEnv.MasterKey, scopedKey);

            Trace.WriteLine("decrypted: " + decrypted);

            var settings = new ProjectSettingsProvider(SettingsEnv.ProjectId, writeKey: scopedKey);
            var client   = new KeenClient(settings);

            client.AddEvent("X", new { vendor_id = "abc", X = "123" });
        }
Exemplo n.º 4
0
        public void Roundtrip_RndIV_Success()
        {
            const string vendorGuid = "abc";
            const bool   isRead     = false;

            var str = "{\"filters\": [{\"property_name\": \"vendor_id\",\"operator\": \"eq\",\"property_value\": \"VENDOR_GUID\"}],\"allowed_operations\": [ \"READ_OR_WRITE\" ]}";

            str = str.Replace("VENDOR_GUID", vendorGuid);

            str = str.Replace("READ_OR_WRITE", isRead ? "read" : "write");

            var rnd   = new System.Security.Cryptography.RNGCryptoServiceProvider();
            var bytes = new byte[16];

            rnd.GetBytes(bytes);

            var iv = String.Concat(bytes.Select(b => b.ToString("X2"))); Trace.WriteLine("IV: " + iv);

            Trace.WriteLine("plaintext: " + str);
            var scopedKey = ScopedKey.EncryptString(SettingsEnv.MasterKey, str, iv);

            Trace.WriteLine("encrypted: " + scopedKey);
            var decrypted = ScopedKey.Decrypt(SettingsEnv.MasterKey, scopedKey);

            Trace.WriteLine("decrypted: " + decrypted);

            // Make sure the input string exactly matches the decrypted string. This input isn't of
            // a length that is a multiple of block size or key size, so this would have required
            // manual padding in the past. The decrypted string shouldn't have any padding now.
            Assert.AreEqual(str, decrypted);
        }