如何处理屏幕旋转上的密码输入

What to do with a password entry on screen rotate

在我的应用程序中,我有一个用户输入密码的片段。这个片段可以旋转。在旋转时,我希望使用 onSaveInstanceStateonActivityCreated 保存和恢复大部分文本字段。通常,当我将密码保存到磁盘时,我会使用单向散列来限制此信息泄露时可能造成的损害程度。但是,如果密码只写了一半,那我就不能做单向散列了……它需要是可恢复的。

所以我的问题是:是否将密码放入 Bundle 保险箱?或者如果片段被销毁,我应该简单地销毁该值吗?以下代码是否对我的用户构成安全风险?

public override void OnSaveInstanceState(Bundle savedInstanceState)
{
    base.OnSaveInstanceState(savedInstanceState);
    savedInstanceState.PutString("passEditText", _passEditText.Text);
    ...
}

public override void OnActivityCreated(Bundle savedInstanceState)
{
    base.OnActivityCreated(savedInstanceState);
    if (savedInstanceState != null)
    {
        _passEditText.Text = savedInstanceState.GetString("passEditText", "");
        ...
    }
}

Fragments 有一个 SaveFragmentInstanceState 方法。

Save the current instance state of the given Fragment. This can be used later when creating a new instance of the Fragment and adding it to the fragment manager, to have it create itself to match the current state returned here.

参考:Xamarin:SaveFragmentInstanceState 参考:Android:saveFragmentInstanceState

安全吗?

它与内存中的任何对象一样安全,或不安全,包括其中具有 partial/full 密码的 EditText。不要将包内容序列化到磁盘或使用 PersistableBundle 保存安全信息...