-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
63 lines (53 loc) · 1.15 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package easycodefgo
import (
"bufio"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"os"
"strings"
)
// 모든 공백 제거
func TrimAll(str string) string {
str = strings.ReplaceAll(str, " ", "")
return str
}
// 파일 정보를 base64 문자열로 인코딩
func EncodeToFileString(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
fInfo, err := file.Stat()
if err != nil {
return "", err
}
var fSize int64 = fInfo.Size()
buf := make([]byte, fSize)
reader := bufio.NewReader(file)
_, err = reader.Read(buf)
if err != nil {
return "", err
}
base64str := base64.StdEncoding.EncodeToString(buf)
return base64str, nil
}
// RSA 암호화
func EncryptRSA(text, publicKey string) (string, error) {
pub, err := base64.StdEncoding.DecodeString(publicKey)
if err != nil {
return "", err
}
pubI, err := x509.ParsePKIXPublicKey(pub)
if err != nil {
return "", err
}
key := pubI.(*rsa.PublicKey)
data, err := rsa.EncryptPKCS1v15(rand.Reader, key, []byte(text))
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(data), nil
}