如何在生成密钥之前检查 StrongBox Keymaster 硬件的可用性?

How to check for StrongBox Keymaster hardware availability before key generation?

我正在寻找一种解决方案,如果设备中存在 StrongBox 硬件,我可以使用它来存储我的加密密钥。目前,我正在 KeyGenParameterSpec 构建器中创建具有 setIsStrongBoxBacked(true) 方法的密钥,当发生 StrongBoxUnavailableException 时,我回退以生成没有它的密钥。

您可以像这样检查设备是否具有"PackageManager.FEATURE_STRONGBOX_KEYSTORE"的功能:

boolean hasStrongBox() {
       return mActivity.getPackageManager()
           .hasSystemFeature(PackageManager.FEATURE_STRONGBOX_KEYSTORE)
}

boolean hasStrongBox;
hasStrongBox = getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_STRONGBOX_KEYSTORE);