public void SignatureKeyCheck() { DigitalSignatureUtils.RetrieveKeyPair("addr1"); var pubKey = DigitalSignatureUtils.RetrievePublicKey(); Assert.IsTrue(pubKey != null); }
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); }
public Block GenerateBlock(int senderId, int recipientId, float amount) { TransactionPoolRepository tpr = new TransactionPoolRepository(); TransactionPool tp = new TransactionPool { Timestamp = DateTime.Now }; int poolId = tpr.Add(tp); TransactionRepository tr = new TransactionRepository(); Transaction t = new Transaction { PoolId = poolId, SenderId = senderId, RecipientId = recipientId, Amount = amount, Status = TransactionStatus.confirmed.ToString(), Timestamp = DateTime.Now }; int transactionId = tr.Add(t); Block b = new Block { PoolId = poolId, PreviousHash = Blocks.Last().Hash, Timestamp = DateTime.Now }; b.AssignPool(); b.ComputeHash(); b.Signature = Convert.ToBase64String( DigitalSignatureUtils.SignData( Convert.FromBase64String( b.Hash ) ) ); return(b); }
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); }
private void OnLogin(object obj) { if (string.IsNullOrWhiteSpace(Username)) { Error = "Enter a username."; ErrorVisibility = Visibility.Visible; return; } User user = userRepo.Get(Username); if (user != null) { PasswordBox passwordBox = obj as PasswordBox; string password = passwordBox.Password; var passwordHash = HashUtils.ComputeHashSha256( Encoding.UTF8.GetBytes( password + user.Salt ) ); password = string.Empty; if (Convert.ToBase64String(passwordHash).Equals(user.Password)) { bool validOwner = DigitalSignatureUtils.RetrieveKeyPair(user.Address); if (validOwner) { Application.Current.Resources["SID"] = user.Id; ViewModelLocator.Main.CurrentViewModel = ViewModelLocator.Dashboard; Messenger.Default.Send(user); dialogCoordinator.ShowMessageAsync( this, "Login Successful", "You are now signed into your profile." ); } else { dialogCoordinator.ShowMessageAsync( this, "Security Breach - 3", "A valid key pair associated to this account wasn't found on your machine." ); } } else { dialogCoordinator.ShowMessageAsync( this, "Login Failed", "The credentials you provided are invalid. Please double check and retry." ); } } else { dialogCoordinator.ShowMessageAsync( this, "Login Failed", "The credentials you provided are invalid. Please double check and retry." ); } }
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; }