- Add the Codota plugin to your IDE and get smart completions
private void myMethod () {}
private void isInsideSecureHardware() { try { createKeyForTimeout(); } catch (Exception e) { Toast.makeText(this, "Could not create the key", Toast.LENGTH_LONG).show(); Log.e(getClass().getSimpleName(), "Exception creating key", e); return; } try { SecretKey key=(SecretKey)ks.getKey(KEY_NAME, null); KeyInfo info= (KeyInfo)SecretKeyFactory.getInstance(key.getAlgorithm(), KEYSTORE) .getKeySpec(key, KeyInfo.class); if (info.isInsideSecureHardware()) { Toast.makeText(this, "Key is inside secure hardware", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Key is only secured by software", Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(this, "Well, *that* didn't work...", Toast.LENGTH_LONG).show(); Log.e(getClass().getSimpleName(), "Exception getting key info", e); } } }
private boolean isInSecureHardware() { try { KeyFactory factory = KeyFactory.getInstance(privateKey.getAlgorithm(), keystoreName); KeyInfo keyInfo = factory.getKeySpec(privateKey, KeyInfo.class); return keyInfo.isInsideSecureHardware(); } catch (GeneralSecurityException e) { Log.w(TAG, "Could not determine if private key is in secure hardware or not"); } return false; }
public boolean isKeyProtectedEnforcedBySecureHardware() { try { //这里随便生成一个key,检查是不是受保护即可 generateKey("temp"); final SecretKey key = (SecretKey) mStore.getKey("temp", null); if (key == null) { return false; } SecretKeyFactory factory = SecretKeyFactory.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore"); KeyInfo keyInfo; keyInfo = (KeyInfo) factory.getKeySpec(key, KeyInfo.class); return keyInfo.isInsideSecureHardware() && keyInfo.isUserAuthenticationRequirementEnforcedBySecureHardware(); } catch (Exception e) { // Not an Android KeyStore key. return false; } } }
out.name("inside_secure_hardware").value(keyInfo.isInsideSecureHardware());