has Key in KeyStore - Android java.security

Android examples for java.security:KeyStore

Description

has Key in KeyStore

Demo Code


//package com.java2s;

import java.io.IOException;

import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import java.security.cert.CertificateException;

public class Main {
    public static final String KEY_STORE_TYPE = "AndroidKeyStore";

    public static boolean hasKey(String alias) {
        try {//from w ww  .j a v a  2  s.  c  o m
            KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE);
            keyStore.load(null);
            return keyStore.containsAlias(alias)
                    && keyStore.isKeyEntry(alias);
        } catch (KeyStoreException | CertificateException | IOException
                | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return false;
    }
}

Related Tutorials