aboutsummaryrefslogtreecommitdiffstats
path: root/modules/util/legacy.go
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2022-04-01 16:47:50 +0800
committerGitHub <noreply@github.com>2022-04-01 16:47:50 +0800
commit65f17bfc31f0f2659978d4d1d5ff825146c53a4d (patch)
treec039c5b332c7a4cb72ee7f92bed10c97df1fe5dd /modules/util/legacy.go
parent5b7466053d993685939da8623fb78e94e4ee0797 (diff)
downloadgitea-65f17bfc31f0f2659978d4d1d5ff825146c53a4d.tar.gz
gitea-65f17bfc31f0f2659978d4d1d5ff825146c53a4d.zip
Refactor legacy `unknwon/com` package, improve golangci lint (#19284)
The main purpose is to refactor the legacy `unknwon/com` package. 1. Remove most imports of `unknwon/com`, only `util/legacy.go` imports the legacy `unknwon/com` 2. Use golangci's depguard to process denied packages 3. Fix some incorrect values in golangci.yml, eg, the version should be quoted string `"1.18"` 4. Use correctly escaped content for `go-import` and `go-source` meta tags 5. Refactor `com.Expand` to our stable (and the same fast) `vars.Expand`, our `vars.Expand` can still return partially rendered content even if the template is not good (eg: key mistach).
Diffstat (limited to 'modules/util/legacy.go')
-rw-r--r--modules/util/legacy.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/modules/util/legacy.go b/modules/util/legacy.go
new file mode 100644
index 0000000000..c7da541534
--- /dev/null
+++ b/modules/util/legacy.go
@@ -0,0 +1,84 @@
+// Copyright 2022 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package util
+
+import (
+ "crypto/aes"
+ "crypto/cipher"
+ "crypto/rand"
+ "errors"
+
+ "github.com/unknwon/com" //nolint:depguard
+)
+
+// CopyFile copies file from source to target path.
+func CopyFile(src, dest string) error {
+ return com.Copy(src, dest)
+}
+
+// CopyDir copy files recursively from source to target directory.
+// It returns error when error occurs in underlying functions.
+func CopyDir(srcPath, destPath string) error {
+ return com.CopyDir(srcPath, destPath)
+}
+
+// ToStr converts any interface to string. should be replaced.
+func ToStr(value interface{}, args ...int) string {
+ return com.ToStr(value, args...)
+}
+
+// ToSnakeCase converts a string to snake_case. should be replaced.
+func ToSnakeCase(str string) string {
+ return com.ToSnakeCase(str)
+}
+
+// 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
+}