登录
首页 >  Golang >  Go问答

保护存储库机密:Github Action Secrets API 的加密技巧

来源:stackoverflow

时间:2024-02-06 15:36:23 126浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《保护存储库机密:Github Action Secrets API 的加密技巧》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

要使用 github action secrets api 创建存储库密钥,我们必须使用钠库对密钥进行加密。 github 文档。未提供 go 的示例。有人可以帮助我编写一个函数来加密秘密吗?

我当前的实现如下:

func EncodeWithPublicKey(text string, publicKey string) (string, error) {
    // Decode the public key from base64
    publicKeyBytes, _ := base64.StdEncoding.DecodeString(publicKey)
    fmt.Printf("%x\n\n", publicKeyBytes)

    // Generate a key pair
    _, privateKey, _ := box.GenerateKey(rand.Reader)
    fmt.Printf("Private key: %x\n\n", *privateKey)

    // Convert publickey to bytes
    var publicKeyDecoded [32]byte
    copy(publicKeyDecoded[:], publicKeyBytes)

    // Encrypt the secret value
    nonce := new([24]byte)
    encrypted := box.Seal(nil, []byte(text), nonce, &publicKeyDecoded, privateKey)
    fmt.Printf("%x\n\n", encrypted)

    // Encode the encrypted value in base64
    encryptedBase64 := base64.StdEncoding.EncodeToString(encrypted)
    fmt.Printf("%x\n\n\n", encryptedBase64)

    return encryptedBase64, nil
}

我正在使用 golang.org/x/crypto/nacl/box

截至当前实施,操作密钥设置不正确,因为构建过程提示我错误“找不到 apitoken”。但是,如果我使用 github 的网站设置密码,部署就会起作用。


正确答案


我不得不将 seal 更改为 sealanonymous

func EncodeWithPublicKey(text string, publicKey string) (string, error) {
    // Decode the public key from base64
    publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKey)
    if err != nil {
        return "", err
    }

    // Decode the public key
    var publicKeyDecoded [32]byte
    copy(publicKeyDecoded[:], publicKeyBytes)

    // Encrypt the secret value
    encrypted, err := box.SealAnonymous(nil, []byte(text), (*[32]byte)(publicKeyBytes), rand.Reader)

    if err != nil {
        return "", err
    }
    // Encode the encrypted value in base64
    encryptedBase64 := base64.StdEncoding.EncodeToString(encrypted)

    return encryptedBase64, nil
}

终于介绍完啦!小伙伴们,这篇关于《保护存储库机密:Github Action Secrets API 的加密技巧》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>