Skip to content

Commit 7376a8a

Browse files
committed
fine tune
1 parent 655506d commit 7376a8a

File tree

5 files changed

+127
-84
lines changed

5 files changed

+127
-84
lines changed

modules/util/copy.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

modules/util/io.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// ReadAtMost reads at most len(buf) bytes from r into buf.
12-
// It returns the number of bytes copied. n is only less then len(buf) if r provides fewer bytes.
12+
// It returns the number of bytes copied. n is only less than len(buf) if r provides fewer bytes.
1313
// If EOF occurs while reading, err will be nil.
1414
func ReadAtMost(r io.Reader, buf []byte) (n int, err error) {
1515
n, err = io.ReadFull(r, buf)

modules/util/legacy.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package util
6+
7+
import (
8+
"crypto/aes"
9+
"crypto/cipher"
10+
"crypto/rand"
11+
"errors"
12+
13+
"github.com/unknwon/com" //nolint:depguard
14+
)
15+
16+
// CopyFile copies file from source to target path.
17+
func CopyFile(src, dest string) error {
18+
return com.Copy(src, dest)
19+
}
20+
21+
// CopyDir copy files recursively from source to target directory.
22+
// It returns error when error occurs in underlying functions.
23+
func CopyDir(srcPath, destPath string) error {
24+
return com.CopyDir(srcPath, destPath)
25+
}
26+
27+
// ToStr converts any interface to string. should be replaced.
28+
func ToStr(value interface{}, args ...int) string {
29+
return com.ToStr(value, args...)
30+
}
31+
32+
// ToSnakeCase converts a string to snake_case. should be replaced.
33+
func ToSnakeCase(str string) string {
34+
return com.ToSnakeCase(str)
35+
}
36+
37+
// AESGCMEncrypt (from legacy package): encrypts plaintext with the given key using AES in GCM mode. should be replaced.
38+
func AESGCMEncrypt(key, plaintext []byte) ([]byte, error) {
39+
block, err := aes.NewCipher(key)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
gcm, err := cipher.NewGCM(block)
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
nonce := make([]byte, gcm.NonceSize())
50+
if _, err := rand.Read(nonce); err != nil {
51+
return nil, err
52+
}
53+
54+
ciphertext := gcm.Seal(nil, nonce, plaintext, nil)
55+
return append(nonce, ciphertext...), nil
56+
}
57+
58+
// AESGCMDecrypt (from legacy package): decrypts ciphertext with the given key using AES in GCM mode. should be replaced.
59+
func AESGCMDecrypt(key, ciphertext []byte) ([]byte, error) {
60+
block, err := aes.NewCipher(key)
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
gcm, err := cipher.NewGCM(block)
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
size := gcm.NonceSize()
71+
if len(ciphertext)-size <= 0 {
72+
return nil, errors.New("ciphertext is empty")
73+
}
74+
75+
nonce := ciphertext[:size]
76+
ciphertext = ciphertext[size:]
77+
78+
plainText, err := gcm.Open(nil, nonce, ciphertext, nil)
79+
if err != nil {
80+
return nil, err
81+
}
82+
83+
return plainText, nil
84+
}

modules/util/legacy_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package util
6+
7+
import (
8+
"crypto/aes"
9+
"crypto/rand"
10+
"github.com/stretchr/testify/assert"
11+
"testing"
12+
)
13+
14+
func TestAESGCMEncrypt(t *testing.T) {
15+
t.Parallel()
16+
17+
key := make([]byte, aes.BlockSize)
18+
_, err := rand.Read(key)
19+
assert.NoError(t, err)
20+
21+
plaintext := []byte("this will be encrypted")
22+
_, err = AESGCMEncrypt(key, plaintext)
23+
assert.NoError(t, err)
24+
}
25+
26+
func TestAESGCMDecrypt(t *testing.T) {
27+
t.Parallel()
28+
29+
key := make([]byte, aes.BlockSize)
30+
_, err := rand.Read(key)
31+
assert.NoError(t, err)
32+
33+
plaintext := []byte("this will be encrypted")
34+
35+
ciphertext, err := AESGCMEncrypt(key, plaintext)
36+
assert.NoError(t, err)
37+
38+
decrypted, err := AESGCMDecrypt(key, ciphertext)
39+
assert.NoError(t, err)
40+
41+
assert.Equal(t, plaintext, decrypted)
42+
}

modules/util/util.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ package util
66

77
import (
88
"bytes"
9-
"crypto/aes"
10-
"crypto/cipher"
119
"crypto/rand"
1210
"errors"
1311
"math/big"
1412
"strconv"
1513
"strings"
16-
17-
"github.com/unknwon/com" //nolint:depguard
1814
)
1915

2016
// OptionalBool a boolean that can be "null"
@@ -185,62 +181,3 @@ func ToUpperASCII(s string) string {
185181
}
186182
return string(b)
187183
}
188-
189-
// ToStr (from unknwon/com): should be replaced.
190-
func ToStr(value interface{}, args ...int) string {
191-
return com.ToStr(value, args...)
192-
}
193-
194-
// ToSnakeCase (from unknwon/com): should be replaced.
195-
func ToSnakeCase(str string) string {
196-
return com.ToSnakeCase(str)
197-
}
198-
199-
// AESGCMEncrypt (from unknwon/com): encrypts plaintext with the given key using AES in GCM mode. should be replaced.
200-
func AESGCMEncrypt(key, plaintext []byte) ([]byte, error) {
201-
block, err := aes.NewCipher(key)
202-
if err != nil {
203-
return nil, err
204-
}
205-
206-
gcm, err := cipher.NewGCM(block)
207-
if err != nil {
208-
return nil, err
209-
}
210-
211-
nonce := make([]byte, gcm.NonceSize())
212-
if _, err := rand.Read(nonce); err != nil {
213-
return nil, err
214-
}
215-
216-
ciphertext := gcm.Seal(nil, nonce, plaintext, nil)
217-
return append(nonce, ciphertext...), nil
218-
}
219-
220-
// AESGCMDecrypt (from unknwon/com): decrypts ciphertext with the given key using AES in GCM mode. should be replaced.
221-
func AESGCMDecrypt(key, ciphertext []byte) ([]byte, error) {
222-
block, err := aes.NewCipher(key)
223-
if err != nil {
224-
return nil, err
225-
}
226-
227-
gcm, err := cipher.NewGCM(block)
228-
if err != nil {
229-
return nil, err
230-
}
231-
232-
size := gcm.NonceSize()
233-
if len(ciphertext)-size <= 0 {
234-
return nil, errors.New("ciphertext is empty")
235-
}
236-
237-
nonce := ciphertext[:size]
238-
ciphertext = ciphertext[size:]
239-
240-
plainText, err := gcm.Open(nil, nonce, ciphertext, nil)
241-
if err != nil {
242-
return nil, err
243-
}
244-
245-
return plainText, nil
246-
}

0 commit comments

Comments
 (0)