Merge pull request '[CLEANUP] Remove deadcode' (#1804) from Gusted/forgejo:forgejo-remove-aes into forgejo-development

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/1804
This commit is contained in:
Gusted 2023-11-25 10:12:36 +00:00
commit 48dfa935bc
3 changed files with 0 additions and 77 deletions

View file

@ -317,10 +317,6 @@ package "code.gitea.io/gitea/modules/translation"
func (MockLocale).TrN func (MockLocale).TrN
func (MockLocale).PrettyNumber func (MockLocale).PrettyNumber
package "code.gitea.io/gitea/modules/util"
func AESGCMEncrypt
func AESGCMDecrypt
package "code.gitea.io/gitea/modules/util/filebuffer" package "code.gitea.io/gitea/modules/util/filebuffer"
func CreateFromReader func CreateFromReader

View file

@ -4,10 +4,6 @@
package util package util
import ( import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"errors"
"io" "io"
"os" "os"
) )
@ -40,52 +36,3 @@ func CopyFile(src, dest string) error {
} }
return os.Chmod(dest, si.Mode()) return os.Chmod(dest, si.Mode())
} }
// AESGCMEncrypt (from legacy package): encrypts plaintext with the given key using AES in GCM mode. should be replaced.
func AESGCMEncrypt(key, plaintext []byte) ([]byte, error) {
block, err := aes.NewCipher(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 := rand.Read(nonce); err != nil {
return nil, err
}
ciphertext := gcm.Seal(nil, nonce, plaintext, nil)
return append(nonce, ciphertext...), nil
}
// AESGCMDecrypt (from legacy package): decrypts ciphertext with the given key using AES in GCM mode. should be replaced.
func AESGCMDecrypt(key, ciphertext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
size := gcm.NonceSize()
if len(ciphertext)-size <= 0 {
return nil, errors.New("ciphertext is empty")
}
nonce := ciphertext[:size]
ciphertext = ciphertext[size:]
plainText, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plainText, nil
}

View file

@ -4,8 +4,6 @@
package util package util
import ( import (
"crypto/aes"
"crypto/rand"
"fmt" "fmt"
"os" "os"
"testing" "testing"
@ -37,21 +35,3 @@ func TestCopyFile(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, testContent, dstContent) assert.Equal(t, testContent, dstContent)
} }
func TestAESGCM(t *testing.T) {
t.Parallel()
key := make([]byte, aes.BlockSize)
_, err := rand.Read(key)
assert.NoError(t, err)
plaintext := []byte("this will be encrypted")
ciphertext, err := AESGCMEncrypt(key, plaintext)
assert.NoError(t, err)
decrypted, err := AESGCMDecrypt(key, ciphertext)
assert.NoError(t, err)
assert.Equal(t, plaintext, decrypted)
}