Beispiel #1
0
        public async Task <NodeKeysDto> GetNodeKeysAsync(long keyId)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                NodeKeys nodeKeys = await context.NodesKeys.FirstOrDefaultAsync(keys => keys.KeyId == keyId).ConfigureAwait(false);

                if (nodeKeys != null)
                {
                    return(new NodeKeysDto
                    {
                        ExpirationTime = nodeKeys.ExpirationTime,
                        GenerationTime = nodeKeys.GenerationTime,
                        KeyId = nodeKeys.KeyId,
                        NodeId = nodeKeys.NodeId,
                        PrivateKey = nodeKeys.PrivateKey,
                        PublicKey = nodeKeys.PublicKey,
                        SignPrivateKey = nodeKeys.SignPrivateKey,
                        SignPublicKey = nodeKeys.SignPublicKey,
                        SymmetricKey = nodeKeys.SymmetricKey
                    });
                }

                return(null);
            }
        }
Beispiel #2
0
        public async Task <NodeKeysDto> SaveNodePublicKeyAsync(long nodeId, byte[] publicKey, long keyId)
        {
            if (nodeId == NodeSettings.Configs.Node.Id)
            {
                throw new InvalidOperationException();
            }
            using (MessengerDbContext context = contextFactory.Create())
            {
                var nodeKeys = await context.NodesKeys.FirstOrDefaultAsync(opt => opt.KeyId == keyId && opt.NodeId == nodeId).ConfigureAwait(false);

                if (nodeKeys == null)
                {
                    nodeKeys = new NodeKeys
                    {
                        KeyId     = keyId,
                        PublicKey = publicKey,
                        NodeId    = nodeId
                    };
                    await context.NodesKeys.AddAsync(nodeKeys).ConfigureAwait(false);

                    await context.SaveChangesAsync().ConfigureAwait(false);
                }
                return(new NodeKeysDto
                {
                    NodeId = nodeId,
                    KeyId = keyId,
                    PublicKey = publicKey
                });
            }
        }
Beispiel #3
0
    private void OnEnable()
    {
        success = inAll = 0;
        nextAssignment.SetActive(false);

        nextKey = RandomKey();
        App.Instance.currentGame.progress = 0;
        playing = true;
    }
Beispiel #4
0
    public void Update()
    {
        if (!playing)
        {
            return;
        }


        foreach (NodeKeys node in nodes)
        {
            if (Input.GetKeyDown(node.key))
            {
                node.source       = AudioController.PlaySound(clip).GetComponent <AudioSource>();
                node.source.pitch = node.pitch;

                if (node.key == nextKey.key)
                {
                    success++;
                    App.Instance.stats.soundProgress++;
                }

                inAll++;


                nextKey        = RandomKey();
                node.img.color = pressColor;
                App.Instance.currentGame.progress += PROGRESSINCREASE;
            }
            else if (Input.GetKeyUp(node.key))
            {
                node.source.Stop();
                node.img.color = normalColor;
            }

            if (App.Instance.currentGame.progress >= 100)
            {
                node.img.color = normalColor;
            }
        }

        if (App.Instance.currentGame.progress >= 100)
        {
            playing = false;
            float c = (float)(success + 1f) / (float)(inAll + 1f);
            App.Instance.currentGame.soundQuality *= c * App.Instance.stats.SoundLevel;

            nextAssignment.SetActive(true);
            App.Instance.currentGame.state = App.Game.State.cover;
        }
    }
Beispiel #5
0
        public async Task <NodeKeysDto> GetNodeKeysAsync(long nodeId, long keyId)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                var nodeKey = await context.NodesKeys
                              .FirstOrDefaultAsync(key => key.KeyId == keyId && key.NodeId == nodeId)
                              .ConfigureAwait(false);

                if (nodeKey != null)
                {
                    return(new NodeKeysDto
                    {
                        ExpirationTime = nodeKey.ExpirationTime,
                        GenerationTime = nodeKey.GenerationTime,
                        KeyId = nodeKey.KeyId,
                        NodeId = nodeKey.NodeId,
                        PublicKey = nodeKey.PublicKey,
                        SignPublicKey = nodeKey.SignPublicKey
                    });
                }
                if (nodeId != NodeSettings.Configs.Node.Id)
                {
                    var connection = connectionsService.GetNodeConnection(nodeId);
                    if (connection != null)
                    {
                        var loadedKey = await nodeRequestSender.GetNodePublicKeyAsync(connection, keyId).ConfigureAwait(false);

                        if (loadedKey == null)
                        {
                            return(null);
                        }
                        var newKey = new NodeKeys
                        {
                            ExpirationTime = loadedKey.ExpirationTime,
                            GenerationTime = loadedKey.GenerationTime,
                            KeyId          = loadedKey.KeyId,
                            NodeId         = loadedKey.NodeId,
                            PublicKey      = loadedKey.PublicKey,
                            SignPublicKey  = loadedKey.SignPublicKey
                        };
                        await context.NodesKeys.AddAsync(newKey).ConfigureAwait(false);

                        await context.SaveChangesAsync().ConfigureAwait(false);

                        return(loadedKey);
                    }
                }
            }
            return(null);
        }
        private async Task HandleNewKeysNodeBlockSegmentAsync(BlockSegmentVm segment)
        {
            using (MessengerDbContext _context = CreateContext())
            {
                using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false))
                {
                    try
                    {
                        NewKeysNodeBlockData blockData = (NewKeysNodeBlockData)segment.PublicData;
                        NodeKeys             nodeKeys  = new NodeKeys
                        {
                            GenerationTime = blockData.NodeKey.GenerationTime,
                            ExpirationTime = blockData.NodeKey.Lifetime != null
                                ? blockData.NodeKey.GenerationTime + blockData.NodeKey.Lifetime.Value
                                : long.MaxValue,
                            KeyId     = blockData.NodeKey.KeyId,
                            NodeId    = segment.NodeId,
                            PublicKey = blockData.NodeKey.Type == KeyType.EncryptionAsymKey
                                ? blockData.NodeKey.Data
                                : null,
                            SignPublicKey = blockData.NodeKey.Type == KeyType.SignAsymKey
                                ? blockData.NodeKey.Data
                                : null,
                        };
                        if (!await _context.NodesKeys.AnyAsync(key => key.KeyId == nodeKeys.KeyId && key.NodeId == nodeKeys.NodeId).ConfigureAwait(false))
                        {
                            await _context.NodesKeys.AddRangeAsync(nodeKeys).ConfigureAwait(false);

                            await _context.SaveChangesAsync().ConfigureAwait(false);

                            transaction.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        AddErrorMessage(nameof(HandleNewKeysNodeBlockSegmentAsync), ex.ToString());
                        transaction.Rollback();
                    }
                }
            }
        }
Beispiel #7
0
    private NodeKeys RandomKey()
    {
        KeyCode oldKey = KeyCode.None;

        if (nextKey != null)
        {
            nextKey.img.color = normalColor;
            oldKey            = nextKey.key;
        }


        NodeKeys n = nodes[Random.Range(0, nodes.Length)];

        while (n.key == oldKey)
        {
            n = nodes[Random.Range(0, nodes.Length)];
        }

        n.img.color = nextKeyColor;


        return(n);
    }
Beispiel #8
0
        NodeKeys ParseString(string keydef)
        {
            var keys = new NodeKeys();

            keys.Keys = new Dictionary <string, byte[]>();
            if (string.IsNullOrEmpty(keydef))
            {
                return(keys);
            }

            var pairs = keydef.Split(new char[] { '/' });

            foreach (var pair in pairs)
            {
                var p = pair.Split(new char[] { ':' });
                keys.Keys.Add(p[0], Transport.Decode(p[1]));
            }
            if (keys.Keys.ContainsKey(userId))
            {
                keys.EncryptedKey = keys.Keys[userId];
                keys.DecryptedKey = Crypto.Decrypt(alg, keys.Keys[userId]);
            }
            return(keys);
        }
Beispiel #9
0
        public async Task <NodeKeysDto> CreateNewNodeKeysAsync(long?nodeId, KeyLength keyLength, uint lifetime)
        {
            byte[] password;
            using (SHA256 sha256 = SHA256.Create())
            {
                password = sha256.ComputeHash(Encoding.UTF8.GetBytes(NodeSettings.Configs.Password));
            }
            long keyId = RandomExtensions.NextInt64();

            using (MessengerDbContext context = contextFactory.Create())
            {
                var nodeKeysId = await context.NodesKeys
                                 .Where(opt => opt.NodeId == nodeId)
                                 .Select(opt => opt.KeyId)
                                 .ToListAsync()
                                 .ConfigureAwait(false);

                while (nodeKeysId.Contains(keyId))
                {
                    keyId = RandomExtensions.NextInt64();
                }
                byte[] symmetricKey = Encryptor.GetSymmetricKey(256, keyId, lifetime, password);
                Encryptor.KeyLengthType encKeysLength;
                Encryptor.KeyLengthType signKeysLength;
                switch (keyLength)
                {
                case KeyLength.Short:
                    encKeysLength  = Encryptor.KeyLengthType.EncryptShort;
                    signKeysLength = Encryptor.KeyLengthType.SignShort;
                    break;

                case KeyLength.Medium:
                    encKeysLength  = Encryptor.KeyLengthType.EncryptMedium;
                    signKeysLength = Encryptor.KeyLengthType.SignMedium;
                    break;

                case KeyLength.Long:
                default:
                    encKeysLength  = Encryptor.KeyLengthType.EncryptLong;
                    signKeysLength = Encryptor.KeyLengthType.SignLong;
                    break;
                }
                var      asymKeys       = Encryptor.GenerateAsymmetricKeys(keyId, lifetime, encKeysLength, password);
                var      signAsymKeys   = Encryptor.GenerateAsymmetricKeys(keyId, lifetime, signKeysLength, password);
                long     generationTime = DateTime.UtcNow.ToUnixTime();
                NodeKeys nodeKeys       = new NodeKeys
                {
                    GenerationTime = generationTime,
                    ExpirationTime = generationTime + lifetime,
                    KeyId          = keyId,
                    NodeId         = nodeId.GetValueOrDefault(),
                    PublicKey      = asymKeys.FirstValue,
                    PrivateKey     = asymKeys.SecondValue,
                    SymmetricKey   = symmetricKey,
                    SignPublicKey  = signAsymKeys.FirstValue,
                    SignPrivateKey = signAsymKeys.SecondValue
                };
                await context.AddAsync(nodeKeys).ConfigureAwait(false);

                await context.SaveChangesAsync().ConfigureAwait(false);

                return(new NodeKeysDto
                {
                    ExpirationTime = nodeKeys.ExpirationTime,
                    GenerationTime = nodeKeys.GenerationTime,
                    KeyId = nodeKeys.KeyId,
                    NodeId = nodeKeys.NodeId,
                    PrivateKey = nodeKeys.PrivateKey,
                    PublicKey = nodeKeys.PublicKey,
                    SymmetricKey = nodeKeys.SymmetricKey,
                    SignPublicKey = nodeKeys.SignPublicKey,
                    SignPrivateKey = nodeKeys.SignPrivateKey,
                    Password = password
                });
            }
        }