持久化加密数据
小于 1 分钟
持久化加密数据
Credentials Store API 可以安全的存储敏感数据,例如密码,服务器地址等。
如何使用
使用PasswordSafe 存储证书
下列示例中所使用的公共方法
private CredentialAttributes createCredentialAttributes(String key) {
return new CredentialAttributes(
CredentialAttributesKt.generateServiceName("MySystem", key)
);
}
存储证书
CredentialAttributes credentialAttributes = createCredentialAttributes(serverId); // see previous sample
Credentials credentials = new Credentials(username, password);
PasswordSafe.getInstance().set(credentialAttributes, credentials);
//删除证书
PasswordSafe.getInstance().set(credentialAttributes, null);
取回存储的证书
String key = null; // e.g. serverURL, accountID
CredentialAttributes credentialAttributes = createCredentialAttributes(key);
Credentials credentials = PasswordSafe.getInstance().get(credentialAttributes);
if (credentials != null) {
String password = credentials.getPasswordAsString();
}
// or get password only
String password = PasswordSafe.getInstance().getPassword(credentialAttributes);
存储
操作系统不一样,默认存储格式也不一样
系统 | 存储 |
---|---|
windows | KeePass 格式文件 |
macOS | Security Framework |
linux | Secret Service API using libsecret |
用户可以在 Settings/Preferences | Appearance & Behavior | System Settings | Passwords. 中修改默认存储方式
Loading...