· 6 years ago · Apr 18, 2019, 09:40 PM
1/**
2 * 지문 ì¸ì‹ Util - êµ¬ë²„ì „
3 * @author 나비ì´ìœì´
4 * @since 2019.03.28
5 */
6@RequiresApi(api = Build.VERSION_CODES.M)
7public class FingerFactory {
8
9 /**
10 * Static 변수
11 ************************************************************************************************************************************************/
12 private static final String KEYNAME = "skill_up"; // FingerPrint Key
13 private static final String ANDROIDKEYSTORE = "AndroidKeyStore"; // AndroidKeyStore
14 private static final String SLASH = "/"; // "/"
15
16 /**
17 * ìƒì„±ìž 변수
18 ************************************************************************************************************************************************/
19 private Context mContext;
20 private FingerprintManager fingerprintManager;
21 private FingerprintManager.CryptoObject cryptoObject;
22 private KeyStore keyStore;
23 private KeyGenerator keyGenerator;
24 private Cipher cipher;
25
26 /**
27 * ìƒì„±ìž
28 ************************************************************************************************************************************************/
29 public FingerFactory(Context mContext) {
30 this.mContext = mContext;
31 setKeyStore();
32 }
33
34 /**
35 * FingerFactory callback 초기화
36 ************************************************************************************************************************************************/
37 public void initalize(FingerprintManager.AuthenticationCallback callback) {
38 CancellationSignal cancellationSignal = new CancellationSignal();
39 fingerprintManager.authenticate(cryptoObject, cancellationSignal, 0, callback, null);
40 }
41
42 /**
43 * 하드웨어 ì§€ì› ê°€ëŠ¥ 여부
44 ************************************************************************************************************************************************/
45 public boolean isFingerHardWare() {
46 return fingerprintManager.isHardwareDetected();
47 }
48
49 /**
50 * 하드웨어 현재 지문 여부
51 ************************************************************************************************************************************************/
52 public boolean isFingerPassCode() {
53 return fingerprintManager.hasEnrolledFingerprints();
54 }
55
56 /**
57 * KeyStore Setting
58 ************************************************************************************************************************************************/
59 private void setKeyStore() {
60 if(mContext != null) {
61 // FingerPrint Manager Setting
62 fingerprintManager = (FingerprintManager) mContext.getSystemService(Context.FINGERPRINT_SERVICE);
63 cryptoObject = new FingerprintManager.CryptoObject(cipher);
64 }
65
66 // keyStore
67 try {
68 keyStore = KeyStore.getInstance(ANDROIDKEYSTORE);
69 } catch (KeyStoreException e) {
70 e.printStackTrace();
71 } catch (Exception e) {
72 e.printStackTrace();
73 }
74
75 // keyGenerator
76 try {
77 keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROIDKEYSTORE);
78 } catch (NoSuchAlgorithmException e) {
79 e.printStackTrace();
80 } catch (Exception e) {
81 e.printStackTrace();
82 }
83
84 // keyGenerator initalize
85 try {
86 keyStore.load(null);
87
88 keyGenerator.init(new KeyGenParameterSpec.Builder(KEYNAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
89 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
90 .setUserAuthenticationRequired(true)
91 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
92 .build());
93
94 keyGenerator.generateKey();
95 } catch (NoSuchAlgorithmException e) {
96 e.printStackTrace();
97 } catch (Exception e) {
98 e.printStackTrace();
99 }
100
101 // cipher initalize
102 try {
103 cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + SLASH + KeyProperties.BLOCK_MODE_CBC + SLASH + KeyProperties.ENCRYPTION_PADDING_PKCS7);
104 } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
105 e.printStackTrace();
106 } catch (Exception e) {
107 e.printStackTrace();
108 }
109
110 // cipher set Key
111 try {
112 keyStore.load(null);
113 SecretKey key = (SecretKey) keyStore.getKey(KEYNAME, null);
114 cipher.init(Cipher.ENCRYPT_MODE, key);
115 } catch (KeyPermanentlyInvalidatedException e) {
116 e.printStackTrace();
117 } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
118 e.printStackTrace();
119 }
120 }
121}