GoWind CMS shares the encryption toolkit of GoWind Admin, providing AES-GCM encryption, password hashing, API key management, and more. This tutorial explains the usage scenarios and implementation of encryption utilities in the CMS.
| Scenario | Encryption Method | Description |
|---|
| User passwords | bcrypt | Irreversible hashing |
| Sensitive configuration | AES-256-GCM | Reversible encryption |
| API keys | AES-256-GCM | Third-party API keys |
| Database connection passwords | AES-256-GCM | Configuration file encryption |
| Webhook signatures | HMAC-SHA256 | Message integrity |
| Session tokens | JWT + RSA | Identity verification |
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"errors"
"io"
)
type AESGCM struct {
key []byte
}
func NewAESGCM(key string) (*AESGCM, error) {
if len(key) != 32 {
return nil, errors.New("key must be 32 bytes")
}
return &AESGCM{key: []byte(key)}, nil
}
func (a *AESGCM) Encrypt(plaintext []byte) ([]byte, error) {
block, err := aes.NewCipher(a.key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
return gcm.Seal(nonce, nonce, plaintext, nil), nil
}
func (a *AESGCM) Decrypt(ciphertext []byte) ([]byte, error) {
block, err := aes.NewCipher(a.key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize {
return nil, errors.New("ciphertext too short")
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
return gcm.Open(nil, nonce, ciphertext, nil)
}
func EncryptConfig(rawConfig string, encryptionKey string) (string, error) {
aes, err := crypto.NewAESGCM(encryptionKey)
if err != nil {
return "", err
}
encrypted, err := aes.Encrypt([]byte(rawConfig))
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(encrypted), nil
}
apiKey := "sk-xxxxxxxxxxxxxxxxxxxx"
encrypted, _ := EncryptConfig(apiKey, config.EncryptionKey)
decrypted, _ := aes.Decrypt(base64Decode(encrypted))
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 12)
return string(bytes), err
}
func VerifyPassword(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
func GenerateAPIKey() (string, string) {
apiKey := make([]byte, 32)
rand.Read(apiKey)
key := hex.EncodeToString(apiKey)
apiSecret := make([]byte, 64)
rand.Read(apiSecret)
secret := hex.EncodeToString(apiSecret)
return key, secret
}
func SignRequest(secret string, payload []byte) string {
h := hmac.New(sha256.New, []byte(secret))
h.Write(payload)
return hex.EncodeToString(h.Sum(nil))
}
func VerifySignature(secret string, payload []byte, signature string) bool {
expected := SignRequest(secret, payload)
return hmac.Equal([]byte(expected), []byte(signature))
}
| Item | Description |
|---|
| AES-GCM | Encryption of sensitive data |
| bcrypt | Password hashing |
| API keys | Key generation + HMAC signatures |
| Key management | Secure key storage |