持久化加密数据

走着路睡觉小于 1 分钟

持久化加密数据

Credentials Store API 可以安全的存储敏感数据,例如密码,服务器地址等。

如何使用

使用PasswordSafeopen in new window 存储证书

下列示例中所使用的公共方法

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);

存储

操作系统不一样,默认存储格式也不一样

系统存储
windowsKeePassopen in new window 格式文件
macOSSecurity Frameworkopen in new window
linuxSecret Service APIopen in new window using libsecretopen in new window

用户可以在 Settings/Preferences | Appearance & Behavior | System Settings | Passwords. 中修改默认存储方式

上次编辑于:
贡献者: zhaojingbo
Loading...