保密性

CI

swift-secrecy 是一个简单的类型包装器,可以帮助你避免意外地暴露敏感信息。 这个包深受 secrecy Rust crate 的启发。

用法

如果你的一个类型持有某种敏感信息,很容易不小心将该值暴露出去。

例如,如果你使用一个类型来保存身份验证信息

struct Authentication {
  var username: String
  var token: String
}

也许你后来在打印调试信息来识别问题

let auth = Authentication(username: "fake", password: "abc123")
print(auth)

现在,你的密码将在日志中以明文形式打印出来。

Authentication(username: "fake", password: "abc123")

相反,通过使用 Secret,你可以避免这些错误。 通过将类型定义更改为

struct Authentication {
  var username: String
  @Secret var password: String
}

相同的代码

let auth = Authentication(username: "fake", password: "abc123")
print(auth)

将产生以下日志

Authentication(username: "fake", _password: Secret([REDACTED String]))

保护你免受意外错误。

如果你想访问底层值,你可以使用 wrappedValue 属性来访问。

auth.token.wrappedValue // This will expose the underlying `String` 

Codable 支持

该软件包开箱即用地提供对 Encodable 的支持。 要获得 Decodable 支持,你必须提供有关如何编辑该值的其他信息。 你可以通过确认 RedactableForDecodable 协议轻松地为你的类型添加支持。 例如,要自动支持你的 Secret<String>Decodable,你可以添加

extension String: RedactableForDecodable {
  public static var redactor: Redactor<Self> { .default }
}

请注意,这并不能保证秘密不会被泄露(例如,通过以纯文本形式将其编码到磁盘),但你始终可以使用专用的 Codable 一致性来创建自定义类型。

许可证

该库是在 MIT 许可证下发布的。 有关详细信息,请参阅 LICENSE