Esempio n. 1
0
        public void RepositoriesFullInsert()
        {
            DigitalSignatureUtils.AssignKeyPair("addr3");

            TransactionPoolRepository tpr = new TransactionPoolRepository();

            TransactionPool tp = new TransactionPool
            {
                Timestamp = DateTime.Now
            };

            int poolId = tpr.Add(tp);

            Assert.AreEqual(poolId, tpr.Get(poolId).Id);

            TransactionRepository tr = new TransactionRepository();

            Transaction t = new Transaction
            {
                PoolId      = poolId,
                SenderId    = 6,
                RecipientId = 1,
                Amount      = 10,
                Status      = TransactionStatus.confirmed.ToString(),
                Timestamp   = DateTime.Now
            };

            int transactionId = tr.Add(t);

            Assert.AreEqual(transactionId, tr.Get(transactionId).Id);

            BlockRepository br = new BlockRepository();

            Block b = new Block
            {
                PoolId       = poolId,
                PreviousHash = br.Get().Last().Hash,
                Timestamp    = DateTime.Now
            };

            b.AssignPool();

            b.ComputeHash();

            b.Signature = Convert.ToBase64String(
                DigitalSignatureUtils.SignData(
                    Convert.FromBase64String(
                        b.Hash
                        )
                    )
                );

            int blockId = br.Add(b);

            Assert.AreEqual(blockId, br.Get(blockId).Id);
        }
Esempio n. 2
0
        public void SignatureValidationBetweenTwoUsers()
        {
            //User1
            DigitalSignatureUtils.AssignKeyPair("addr1");

            //User1 public key
            var pubKey = DigitalSignatureUtils.RetrievePublicKey();

            //Hashed data
            var hash = HashUtils.ComputeHashSha256(Encoding.UTF8.GetBytes("TestData"));

            //User1 signatured data
            var signature = DigitalSignatureUtils.SignData(hash);

            //User2
            DigitalSignatureUtils.AssignKeyPair("addr2");

            //Data validation
            var isValid = DigitalSignatureUtils.VerifySignature(hash, signature, pubKey);

            Assert.IsTrue(isValid);
        }
Esempio n. 3
0
        private void OnRegister(object obj)
        {
            if (string.IsNullOrWhiteSpace(Firstname))
            {
                Error           = "Enter a firstname.";
                ErrorVisibility = Visibility.Visible;
                return;
            }

            if (string.IsNullOrWhiteSpace(Lastname))
            {
                Error           = "Enter a lastname.";
                ErrorVisibility = Visibility.Visible;
                return;
            }

            if (string.IsNullOrWhiteSpace(Username))
            {
                Error           = "Enter a username.";
                ErrorVisibility = Visibility.Visible;
                return;
            }

            PasswordBox passwordBox = obj as PasswordBox;
            string      password    = passwordBox.Password;

            var salt         = HashUtils.GetSalt();
            var passwordHash = HashUtils.ComputeHashSha256(
                Encoding.UTF8.GetBytes(
                    password +
                    Convert.ToBase64String(salt)
                    )
                );

            password = string.Empty;

            Guid gAddress = Guid.NewGuid();

            User user = new User
            {
                Firstname = Firstname,
                Lastname  = Lastname,
                Username  = Username,
                Password  = Convert.ToBase64String(passwordHash),
                Salt      = Convert.ToBase64String(salt),
                Address   = gAddress.ToString("N"),
                Timestamp = DateTime.Now
            };

            userRepo.Add(user);

            DigitalSignatureUtils.AssignKeyPair(user.Address);

            dialogCoordinator.ShowMessageAsync(
                this,
                "Register Successful",
                "Your private key has been generated and you should keep it for yourself at ALL times."
                );

            Firstname = string.Empty;
            Lastname  = string.Empty;
            Username  = string.Empty;

            ViewModelLocator.Main.CurrentViewModel = ViewModelLocator.Login;
        }