Security 如何使用SOP(机密操作)和Go加密从JSON文件导入的值?
我有一个JSON文件,如下所示Security 如何使用SOP(机密操作)和Go加密从JSON文件导入的值?,security,go,mozilla-sops,Security,Go,Mozilla Sops,我有一个JSON文件,如下所示 package main import ( "encoding/json" "fmt" "io/ioutil" "go.mozilla.org/sops" ) type secretValue struct { Value string `json:"secret"` } func main() { file, _ := ioutil.ReadFile("secret.json") getSecret
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"go.mozilla.org/sops"
)
type secretValue struct {
Value string `json:"secret"`
}
func main() {
file, _ := ioutil.ReadFile("secret.json")
getSecretValue := secretValue{}
_ = json.Unmarshal([]byte(file), &getSecretValue)
encryptedValue, err := sops.Tree.Encrypt([]byte(getSecretValue.Value), file)
if err != nil {
panic(err)
}
fmt.Println(encryptedValue)
}
secret.json:
{
"secret": "strongPassword"
}
我想打印出密钥“secret”的加密值
到目前为止,我已经做了如下尝试
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"go.mozilla.org/sops"
)
type secretValue struct {
Value string `json:"secret"`
}
func main() {
file, _ := ioutil.ReadFile("secret.json")
getSecretValue := secretValue{}
_ = json.Unmarshal([]byte(file), &getSecretValue)
encryptedValue, err := sops.Tree.Encrypt([]byte(getSecretValue.Value), file)
if err != nil {
panic(err)
}
fmt.Println(encryptedValue)
}
你可能已经猜到了,我是个新手,上面的代码不起作用
如何改进代码以打印加密值
请注意,我编写这样的代码只是为了了解SOP如何使用Go工作。我不会在生产中打印出这样的秘密值
编辑:
我认为问题在于加密函数的参数。根据文档,它应该包含[]字节键和密码参数,但我不知道是否正确设置[]字节键,或者密码来自何处。它是来自加密/密码软件包吗
编辑2:
谢谢@HolaYang给出的好答案。
我试图让您的答案与外部JSON文件一起工作,如下所示,但它给了我一条错误消息,告诉我,不能使用fileContent(type secretValue)作为(&“go.mozilla.org/sops/stores/JSON.Store literal).LoadPlainFile
的参数中的类型[]字节
package main
import (
hey "encoding/json"
"fmt"
"io/ioutil"
"go.mozilla.org/sops"
"go.mozilla.org/sops/aes"
"go.mozilla.org/sops/stores/json"
)
type secretValue struct {
Value string `json:"secret"`
}
func main() {
// fileContent := []byte(`{
// "secret": "strongPassword"
// }`)
file, _ := ioutil.ReadFile("secret.json")
fileContent := secretValue{}
//_ = json.Unmarshal([]byte(file), &fileContent)
_ = hey.Unmarshal([]byte(file), &fileContent)
encryptKey := []byte("0123456789012345") // length 16
branches, _ := (&json.Store{}).LoadPlainFile(fileContent)
tree := sops.Tree{Branches: branches}
r, err := tree.Encrypt(encryptKey, aes.NewCipher())
if err != nil {
panic(err)
}
fmt.Println(r)
}
让我们看看
sops.Tree.Encrypt
(代码中的一个输入错误)的函数声明。
根据代码,我们应该在这些步骤中执行
sops.Tree
实例密码进行加密
package main
import (
"fmt"
"go.mozilla.org/sops"
"go.mozilla.org/sops/aes"
"go.mozilla.org/sops/stores/json"
)
func main() {
/*
fileContent := []byte(`{
"secret": "strongPassword"
}`)
*/
fileContent, _ := ioutil.ReadFile("xxx.json")
encryptKey := []byte("0123456789012345") // length 16
branches, _ := (&json.Store{}).LoadPlainFile(fileContent)
tree := sops.Tree{Branches: branches}
r, err := tree.Encrypt(encryptKey, aes.NewCipher())
if err != nil {
panic(err)
}
fmt.Println(r)
}
谢谢你指出错误,我编辑了我的文章。如果您能根据我发布的代码提供一个完整的代码示例,我将不胜感激。谢谢,您打印了一个加密值。你的回答肯定很有帮助。最后一个问题是,我试图调整您的代码,以便打印从外部json文件导入的加密值。但我还是不能让它工作。我编辑了我的帖子,这样你就可以看到我想做什么。
key
是Cipher.Encrypt的参数,Cipher是一个接口,也许你可以在下面看到我的代码