-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (104 loc) · 2.85 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"crypto/aes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"github.com/gin-gonic/gin"
)
type RequestToDecrypt struct {
ToDecrypt string `json:"toDecrypt" binding:"required"`
Key string `json:"key" binding:"required"`
}
func main() {
router := gin.Default()
router.POST("/encryptAES", func(c *gin.Context) {
var request map[string]interface{}
jsonData, err := io.ReadAll(c.Request.Body)
if err != nil {
fmt.Println("error getting json data: %w", err)
return
}
if err := json.Unmarshal(jsonData, &request); err != nil {
fmt.Println("error unmarshalling json data: %w", err)
return
}
marshalledJson, err := json.Marshal(request)
if err != nil {
fmt.Println("error marshalling json data: %w", err)
return
}
key, err := ReadAESKeyFromPemFile("aes.pem")
if err != nil {
log.Println("Error reading AES key: ", err)
c.JSON(500, gin.H{"error": "Internal server error"})
return
}
aesCipher, err := aes.NewCipher(key)
if err != nil {
log.Println("Error creating AES cipher: ", err)
return
}
ciphertext, err := AESEncryptWithGCM(marshalledJson, aesCipher)
if err != nil {
log.Println("Error encrypting: ", err)
}
base64CipherText := base64.StdEncoding.EncodeToString(ciphertext)
c.JSON(200, gin.H{"encrypted": base64CipherText})
})
router.POST("/decryptAES", func(c *gin.Context) {
var request RequestToDecrypt
err := c.BindJSON(&request)
if err != nil {
log.Println("Error parsing request: ", err)
c.JSON(400, gin.H{"error": "Invalid request"})
return
}
key, err := base64.StdEncoding.DecodeString(request.Key)
if err != nil {
log.Println("Error decoding base64 on key: ", err)
c.JSON(400, gin.H{"error": "Invalid base64"})
return
}
aesCipher, err := aes.NewCipher(key)
if err != nil {
log.Println("Error creating AES cipher: ", err)
return
}
decodedCipherText, err := base64.StdEncoding.DecodeString(request.ToDecrypt)
if err != nil {
log.Println("Error decoding base64: ", err)
c.JSON(400, gin.H{"error": "Invalid base64"})
return
}
decryptedDataInBytes, err := AESDecryptWithGCM(decodedCipherText, aesCipher)
if err != nil {
log.Println("Error decrypting: ", err)
c.JSON(500, gin.H{
"message": err.Error(),
})
return
}
var decryptedBody map[string]interface{}
if err := json.Unmarshal(decryptedDataInBytes, &decryptedBody); err != nil {
c.JSON(500, gin.H{
"messageJson": err.Error(),
})
return
}
c.Set("decryptedPayloadAsMap", decryptedBody)
c.Set("decryptedPayloadAsBytes", decryptedDataInBytes)
decryptedPayloadAsMap, exists := c.Get("decryptedPayloadAsMap")
if !exists {
fmt.Println("did not get decrypted payload")
return
}
c.AbortWithStatusJSON(200, decryptedPayloadAsMap)
})
err := router.Run(":8080")
if err != nil {
log.Println("Error starting server: ", err)
}
}