encryption - 创建 CLI : What should I do with the password in the config file (and how)?

标签 encryption go command-line-interface

我正在使用 Go 编写 CLI,我想将密码保存到保存在用户主目录中的配置文件中。

我是否应该在保存时加密密码并在将密码发送到与 CLI 交互的服务器时解密?

如果是这样,go library 中是否有一个很好的内置库来做到这一点?我想让它尽可能简单,并且不使用任何额外的外部包。我看到的所有答案都非常复杂。

应该是这样的:

func Encrypt(password string) string
func Decrypt(password string) string

有什么想法吗?

最佳答案

免责声明:核心库不提供开箱即用的此功能。 Go 的第三方库(来自 Docker)可以提供帮助。

现代操作系统提供了以安全方式存储 secret 的工具和 API。

  • 苹果:钥匙串(keychain)
  • Linux:特勤局
  • Windows:凭据管理器 API

Git 和 Docker 使用这些工具来存储您的凭据。

David Calavera 贡献了 Docker credential helpers给Docker Engine v1.11写了一篇标题为Stop saving credential tokens in text files的文章2016 年。这篇文章描述了这个问题,一个简单的 Linux 实现,并展示了如何使用该库。

package main

import (
  "github.com/docker/docker-credential-helpers/client"
  "github.com/docker/docker-credential-helpers/credentials"
)

var nativeStore = client.NewShellProgramFunc("docker-credential-secretservice")

func main() {
  c := &credentials.Credentials{
    ServerURL: "https://api.github.com",
    Username: "token",
    Secret: "my-super-secret-token",
  }
  
  client.Store(nativeStore, c)
  storedCreds, err := client.Get(nativeStore, "https://api.github.com")
}

Docker credential helpers 的缺点是它希望您安装它的二进制文件并需要 CGo。我专门添加了 Docker 解决方案,因为它在评论中被提及但是有类似的库:

关于encryption - 创建 CLI : What should I do with the password in the config file (and how)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33183692/

相关文章:

c# - 在 Unity 中加密,在 Node.js 中解密

go - "go get"使用http_proxy时忽略GOPROXY

reflection - 如何反射(reflect)Golang中对象的动态方法名

bash - 在命令行上使用粘贴或 PR 加入列不起作用

azure - az vm open-port icmp,这可能吗?

javascript - CryptoJS AES 模式总是以 = 结尾

android - 如何使用node-rsa加密node.js中的数据并在android中解密加密的数据?

具有子域的 Apache2 多个 SSL 证书将不起作用

go - Beego和Newrelic集成的问题

python - 命令之间的共享选项和标志