- Fix remaining 187 pkg/ files referencing core/cli → core/go - Move SDK library code from internal/cmd/sdk/ → pkg/sdk/ (new package) - Create pkg/rag/helpers.go with convenience functions from internal/cmd/rag/ - Fix pkg/mcp/tools_rag.go to use pkg/rag instead of internal/cmd/rag - Fix pkg/build/buildcmd/cmd_sdk.go and pkg/release/sdk.go to use pkg/sdk - Remove all non-library content: main.go, internal/, cmd/, docker/, scripts/, tasks/, tools/, .core/, .forgejo/, .woodpecker/, Taskfile.yml - Run go mod tidy to trim unused dependencies core/go is now a pure Go package suite (library only). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Claude <developers@lethean.io> Reviewed-on: #3
100 lines
3 KiB
Go
100 lines
3 KiB
Go
package crypt
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/rand"
|
|
|
|
core "forge.lthn.ai/core/go/pkg/framework/core"
|
|
"golang.org/x/crypto/chacha20poly1305"
|
|
)
|
|
|
|
// ChaCha20Encrypt encrypts plaintext using ChaCha20-Poly1305.
|
|
// The key must be 32 bytes. The nonce is randomly generated and prepended
|
|
// to the ciphertext.
|
|
func ChaCha20Encrypt(plaintext, key []byte) ([]byte, error) {
|
|
aead, err := chacha20poly1305.NewX(key)
|
|
if err != nil {
|
|
return nil, core.E("crypt.ChaCha20Encrypt", "failed to create cipher", err)
|
|
}
|
|
|
|
nonce := make([]byte, aead.NonceSize())
|
|
if _, err := rand.Read(nonce); err != nil {
|
|
return nil, core.E("crypt.ChaCha20Encrypt", "failed to generate nonce", err)
|
|
}
|
|
|
|
ciphertext := aead.Seal(nonce, nonce, plaintext, nil)
|
|
return ciphertext, nil
|
|
}
|
|
|
|
// ChaCha20Decrypt decrypts ciphertext encrypted with ChaCha20Encrypt.
|
|
// The key must be 32 bytes. Expects the nonce prepended to the ciphertext.
|
|
func ChaCha20Decrypt(ciphertext, key []byte) ([]byte, error) {
|
|
aead, err := chacha20poly1305.NewX(key)
|
|
if err != nil {
|
|
return nil, core.E("crypt.ChaCha20Decrypt", "failed to create cipher", err)
|
|
}
|
|
|
|
nonceSize := aead.NonceSize()
|
|
if len(ciphertext) < nonceSize {
|
|
return nil, core.E("crypt.ChaCha20Decrypt", "ciphertext too short", nil)
|
|
}
|
|
|
|
nonce, encrypted := ciphertext[:nonceSize], ciphertext[nonceSize:]
|
|
plaintext, err := aead.Open(nil, nonce, encrypted, nil)
|
|
if err != nil {
|
|
return nil, core.E("crypt.ChaCha20Decrypt", "failed to decrypt", err)
|
|
}
|
|
|
|
return plaintext, nil
|
|
}
|
|
|
|
// AESGCMEncrypt encrypts plaintext using AES-256-GCM.
|
|
// The key must be 32 bytes. The nonce is randomly generated and prepended
|
|
// to the ciphertext.
|
|
func AESGCMEncrypt(plaintext, key []byte) ([]byte, error) {
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, core.E("crypt.AESGCMEncrypt", "failed to create cipher", err)
|
|
}
|
|
|
|
aead, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return nil, core.E("crypt.AESGCMEncrypt", "failed to create GCM", err)
|
|
}
|
|
|
|
nonce := make([]byte, aead.NonceSize())
|
|
if _, err := rand.Read(nonce); err != nil {
|
|
return nil, core.E("crypt.AESGCMEncrypt", "failed to generate nonce", err)
|
|
}
|
|
|
|
ciphertext := aead.Seal(nonce, nonce, plaintext, nil)
|
|
return ciphertext, nil
|
|
}
|
|
|
|
// AESGCMDecrypt decrypts ciphertext encrypted with AESGCMEncrypt.
|
|
// The key must be 32 bytes. Expects the nonce prepended to the ciphertext.
|
|
func AESGCMDecrypt(ciphertext, key []byte) ([]byte, error) {
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, core.E("crypt.AESGCMDecrypt", "failed to create cipher", err)
|
|
}
|
|
|
|
aead, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return nil, core.E("crypt.AESGCMDecrypt", "failed to create GCM", err)
|
|
}
|
|
|
|
nonceSize := aead.NonceSize()
|
|
if len(ciphertext) < nonceSize {
|
|
return nil, core.E("crypt.AESGCMDecrypt", "ciphertext too short", nil)
|
|
}
|
|
|
|
nonce, encrypted := ciphertext[:nonceSize], ciphertext[nonceSize:]
|
|
plaintext, err := aead.Open(nil, nonce, encrypted, nil)
|
|
if err != nil {
|
|
return nil, core.E("crypt.AESGCMDecrypt", "failed to decrypt", err)
|
|
}
|
|
|
|
return plaintext, nil
|
|
}
|