· 9 years ago · Jan 11, 2017, 10:34 PM
1let KeyStore = java.security.KeyStore
2let Cipher = javax.crypto.Cipher
3let KeyGenerator = javax.crypto.KeyGenerator
4let KeyProperties = android.security.keystore.KeyProperties
5let KeyGenParameterSpec = android.security.keystore.KeyGenParameterSpec
6let FingerprintManager = android.hardware.fingerprint.FingerprintManager
7
8export class TestClass {
9 private keyguardManager = utils.ad.getApplicationContext().getSystemService("keyguard")
10 private fingerprintManager = utils.ad.getApplicationContext().getSystemService("fingerprint")
11 private REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 1001
12 private KEY_NAME = 'fingerprintauth'
13 private KEYSTORE_NAME = 'AndroidKeyStore'
14
15 allInOneTest() {
16 let keyStore = KeyStore.getInstance(this.KEYSTORE_NAME)
17 keyStore.load(null)
18 var keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, this.KEYSTORE_NAME)
19 keyGenerator.init(
20 new KeyGenParameterSpec.Builder(this.KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
21 .setBlockModes([KeyProperties.BLOCK_MODE_CBC])
22 .setUserAuthenticationRequired(false)
23 .setEncryptionPaddings([KeyProperties.ENCRYPTION_PADDING_PKCS7])
24 .build()
25 )
26
27 keyGenerator.generateKey()
28 let secretKey = keyStore.getKey(this.KEY_NAME, null)
29 let cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7)
30 cipher.init(Cipher.ENCRYPT_MODE, secretKey)
31
32 let callbackClass = FingerprintManager.AuthenticationCallback.extend({
33 onAuthenticationError(errorCode, errString) {
34 console.log("error")
35 },
36 onAuthenticationHelp(helpCode, helpString) {
37
38 },
39 onAuthenticationSucceeded(result) {
40
41 },
42 onAuthenticationFailed() {
43
44 }
45 })
46
47 let callbackInstance = new callbackClass()
48
49 let cancellationSignal = new android.os.CancellationSignal()
50 let cryptoObject = new FingerprintManager.CryptoObject(cipher)
51
52 this.fingerprintManager.authenticate(cryptoObject, cancellationSignal, 0, callbackInstance, null)
53 }
54}