Compare commits

..

No commits in common. "4498a2622276d8fd812107c76f993be0f213e73f" and "257287954c5e553c8c1495921f75c5d02c9e83e3" have entirely different histories.

3 changed files with 18 additions and 27 deletions

View File

@ -140,7 +140,7 @@ GO_DIRS := cmd tests models modules routers build services tools
WEB_DIRS := web_src/js web_src/css WEB_DIRS := web_src/js web_src/css
GO_SOURCES := $(wildcard *.go) GO_SOURCES := $(wildcard *.go)
GO_SOURCES += $(shell find $(GO_DIRS) -type f -name "*.go" ! -path modules/options/bindata.go ! -path modules/public/bindata.go ! -path modules/templates/bindata.go) GO_SOURCES += $(shell find $(GO_DIRS) -type f -name "*.go" -not -path modules/options/bindata.go -not -path modules/public/bindata.go -not -path modules/templates/bindata.go)
GO_SOURCES += $(GENERATED_GO_DEST) GO_SOURCES += $(GENERATED_GO_DEST)
GO_SOURCES_NO_BINDATA := $(GO_SOURCES) GO_SOURCES_NO_BINDATA := $(GO_SOURCES)

View File

@ -11,7 +11,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt"
"io" "io"
) )
@ -19,13 +18,13 @@ import (
func AesEncrypt(key, text []byte) ([]byte, error) { func AesEncrypt(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key) block, err := aes.NewCipher(key)
if err != nil { if err != nil {
return nil, fmt.Errorf("AesEncrypt invalid key: %v", err) return nil, err
} }
b := base64.StdEncoding.EncodeToString(text) b := base64.StdEncoding.EncodeToString(text)
ciphertext := make([]byte, aes.BlockSize+len(b)) ciphertext := make([]byte, aes.BlockSize+len(b))
iv := ciphertext[:aes.BlockSize] iv := ciphertext[:aes.BlockSize]
if _, err = io.ReadFull(rand.Reader, iv); err != nil { if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, fmt.Errorf("AesEncrypt unable to read IV: %w", err) return nil, err
} }
cfb := cipher.NewCFBEncrypter(block, iv) cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b)) cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))
@ -39,7 +38,7 @@ func AesDecrypt(key, text []byte) ([]byte, error) {
return nil, err return nil, err
} }
if len(text) < aes.BlockSize { if len(text) < aes.BlockSize {
return nil, errors.New("AesDecrypt ciphertext too short") return nil, errors.New("ciphertext too short")
} }
iv := text[:aes.BlockSize] iv := text[:aes.BlockSize]
text = text[aes.BlockSize:] text = text[aes.BlockSize:]
@ -47,7 +46,7 @@ func AesDecrypt(key, text []byte) ([]byte, error) {
cfb.XORKeyStream(text, text) cfb.XORKeyStream(text, text)
data, err := base64.StdEncoding.DecodeString(string(text)) data, err := base64.StdEncoding.DecodeString(string(text))
if err != nil { if err != nil {
return nil, fmt.Errorf("AesDecrypt invalid decrypted base64 string: %w", err) return nil, err
} }
return data, nil return data, nil
} }
@ -58,21 +57,21 @@ func EncryptSecret(key, str string) (string, error) {
plaintext := []byte(str) plaintext := []byte(str)
ciphertext, err := AesEncrypt(keyHash[:], plaintext) ciphertext, err := AesEncrypt(keyHash[:], plaintext)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to encrypt by secret: %w", err) return "", err
} }
return hex.EncodeToString(ciphertext), nil return hex.EncodeToString(ciphertext), nil
} }
// DecryptSecret decrypts a previously encrypted hex string // DecryptSecret decrypts a previously encrypted hex string
func DecryptSecret(key, cipherHex string) (string, error) { func DecryptSecret(key, cipherhex string) (string, error) {
keyHash := sha256.Sum256([]byte(key)) keyHash := sha256.Sum256([]byte(key))
ciphertext, err := hex.DecodeString(cipherHex) ciphertext, err := hex.DecodeString(cipherhex)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to decrypt by secret, invalid hex string: %w", err) return "", err
} }
plaintext, err := AesDecrypt(keyHash[:], ciphertext) plaintext, err := AesDecrypt(keyHash[:], ciphertext)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to decrypt by secret, the key (maybe SECRET_KEY?) might be incorrect: %w", err) return "", err
} }
return string(plaintext), nil return string(plaintext), nil
} }

View File

@ -10,22 +10,14 @@ import (
) )
func TestEncryptDecrypt(t *testing.T) { func TestEncryptDecrypt(t *testing.T) {
hex, err := EncryptSecret("foo", "baz") var hex string
assert.NoError(t, err) var str string
str, _ := DecryptSecret("foo", hex)
assert.Equal(t, "baz", str)
hex, err = EncryptSecret("bar", "baz") hex, _ = EncryptSecret("foo", "baz")
assert.NoError(t, err)
str, _ = DecryptSecret("foo", hex) str, _ = DecryptSecret("foo", hex)
assert.NotEqual(t, "baz", str) assert.Equal(t, str, "baz")
_, err = DecryptSecret("a", "b") hex, _ = EncryptSecret("bar", "baz")
assert.ErrorContains(t, err, "invalid hex string") str, _ = DecryptSecret("foo", hex)
assert.NotEqual(t, str, "baz")
_, err = DecryptSecret("a", "bb")
assert.ErrorContains(t, err, "the key (maybe SECRET_KEY?) might be incorrect: AesDecrypt ciphertext too short")
_, err = DecryptSecret("a", "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
assert.ErrorContains(t, err, "the key (maybe SECRET_KEY?) might be incorrect: AesDecrypt invalid decrypted base64 string")
} }