为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 SOPS(Secrets OPerationS)和 Go 加密从 JSON 文件导入的值?

如何使用 SOPS(Secrets OPerationS)和 Go 加密从 JSON 文件导入的值?

Go
紫衣仙女 2023-06-19 15:21:01
我有一个 JSON 文件,如下所示。秘密.json:{    "secret": "strongPassword"}我想打印出密钥“secret”的加密值。到目前为止,我已经尝试过如下。package mainimport (    "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)}您可能已经猜到了,我是 Go 的新手,上面的代码不起作用。如何改进代码以打印出加密值?请注意,我编写这样的代码只是为了了解 SOPS 如何使用 Go 工作。我不会在生产中打印出这样的秘密值。
查看完整描述

1 回答

?
叮当猫咪

TA贡献1776条经验 获得超12个赞

sops.Tree.Encrypt 让我们看看(a typo here in your code)的函数声明。通过代码,我们应该分这几步去做。

  1. sops.Tree使用 json 文件构造一个实例。

  2. 使用特定Cipher的加密。

请以这种方式尝试自己。

下面代码demo,用AES作为Cipher,sops只能用源码接口加密total 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)

}


查看完整回答
反对 回复 2023-06-19
  • 1 回答
  • 0 关注
  • 60 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信