-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAndroidKeyBridge.java
More file actions
84 lines (66 loc) · 2.16 KB
/
AndroidKeyBridge.java
File metadata and controls
84 lines (66 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package xyz.sequence;
import android.content.SharedPreferences;
import android.util.Log;
import android.content.Context;
import android.widget.Toast;
import androidx.security.crypto.EncryptedSharedPreferences;
import androidx.security.crypto.MasterKey;
import java.io.IOException;
import java.security.GeneralSecurityException;
public class AndroidKeyBridge {
private static AndroidKeyBridge instance = null;
private Context context = null;
private MasterKey masterKey = null;
private SharedPreferences sharedPreferences;
private AndroidKeyBridge()
{
}
public void init(Context context)
{
this.context = context;
if (masterKey == null) {
try {
masterKey = new MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
sharedPreferences = EncryptedSharedPreferences.create(
context,
"secret_shared_prefs",
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
} catch (GeneralSecurityException e){
Log.d("Exception", e.getMessage());
} catch (IOException e){
Log.d("Exception", e.getMessage());
}
}
}
public void destroy()
{
this.context = null;
}
public static AndroidKeyBridge getInstance()
{
if (instance == null)
instance = new AndroidKeyBridge();
return instance;
}
public static void SaveKeychainValue(String key, String value)
{
getInstance().RunSaveKeychainValue(key, value);
}
public static String GetKeychainValue(String key)
{
return getInstance().RunGetKeychainValue(key);
}
private void RunSaveKeychainValue(String key, String value)
{
sharedPreferences.edit().putString(key, value).apply();
}
private String RunGetKeychainValue(String key)
{
return sharedPreferences.getString(key, "");
}
}