public void ComputeHash() { string body = ""; int records = Pool.Queue.Count; for (int i = 0; i < records; i++) { string thash = Pool.GetFirst().ComputeHash(); body += thash; } string header = Id + Timestamp.ToString() + PreviousHash; string blockHash = header + body; Hash = Convert.ToBase64String( HashUtils.ComputeHashSha256( Encoding.UTF8.GetBytes( blockHash ) ) ); }
public string ComputeHash() { string data = Id + PoolId + SenderId + RecipientId + Amount + Timestamp.ToString(); return(Convert.ToBase64String( HashUtils.ComputeHashSha256( Encoding.UTF8.GetBytes(data) ) )); }
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; }