/// <summary> /// Reads password from console displaying substitute characters instead of real ones /// into a sealed SecureBuffer /// </summary> public static SecureBuffer ReadPasswordToSecureBuffer(char substitute) { var result = new SecureBuffer(); while (true) { char c = System.Console.ReadKey(true).KeyChar; if (Char.IsControl(c)) { result.Seal(); return(result); } var buf = Encoding.UTF8.GetBytes(new char[] { c }); for (var i = 0; i < buf.Length; i++) { result.Push(buf[i]); } Array.Clear(buf, 0, buf.Length); if (substitute != (char)0) { System.Console.Write(substitute); } } }
private void tbPassword_TextChanged(object sender, EventArgs e) { var pm = App.SecurityManager.PasswordManager; var bytes = Encoding.UTF8.GetBytes(tbPassword.Text); using (var password = new SecureBuffer(bytes.Length)) { foreach (var b in bytes) { password.Push(b); } Array.Clear(bytes, 0, bytes.Length); password.Seal(); var hash = pm.ComputeHash(PasswordFamily.Text, password); Text = "Score: {0} Easy%: {1} Normal%: {2} Hard%: {3}".Args( pm.CalculateStrenghtScore(PasswordFamily.Text, password), pm.CalculateStrenghtPercent(PasswordFamily.Text, password, DefaultPasswordManager.TOP_SCORE_MINIMUM), pm.CalculateStrenghtPercent(PasswordFamily.Text, password, DefaultPasswordManager.TOP_SCORE_NORMAL), pm.CalculateStrenghtPercent(PasswordFamily.Text, password, DefaultPasswordManager.TOP_SCORE_MAXIMUM)); tbJSON.Text = hash.ToString(); } }