prepair to using go concurrent
This commit is contained in:
1 parent
ce0508294c
commit
47816debd3
10 files changed
+222
-139
No files matched your search
+65
-61
@@ -1,79 +1,83 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/skip2/go-qrcode"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func EncryptAES(data []byte, key []byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
//Mail
|
||||
|
||||
type MyCustomClaims struct {
|
||||
Email string `json:"email"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func buildTokenLink(token string, w http.ResponseWriter) (string, string) {
|
||||
tokenCustomTrim := strings.ReplaceAll(token, ".", "&")
|
||||
emailVerifyLink := "http://www.totoday.com/?p=" + tokenCustomTrim
|
||||
fmt.Println(emailVerifyLink)
|
||||
png, err := qrcode.Encode(emailVerifyLink, qrcode.Low, 200)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
fmt.Println("Internal log: error create qr ")
|
||||
_ = WriteJSONInternalError(w, "error create QR code")
|
||||
}
|
||||
base64Image := base64.StdEncoding.EncodeToString(png)
|
||||
dataURL := "data:image/png;base64," + base64Image
|
||||
|
||||
// Generate a random IV (Initialization Vector)
|
||||
iv := make([]byte, aes.BlockSize)
|
||||
if _, err := rand.Read(iv); err != nil {
|
||||
fmt.Println("error generating IV:", err)
|
||||
}
|
||||
|
||||
// Pad the data to a multiple of the block size
|
||||
data = pkcs7Pad(data, aes.BlockSize)
|
||||
|
||||
// Create a CBC mode cipher block
|
||||
mode := cipher.NewCBCEncrypter(block, iv)
|
||||
|
||||
// Encrypt the data
|
||||
ciphertext := make([]byte, len(data))
|
||||
mode.CryptBlocks(ciphertext, data)
|
||||
|
||||
// Prepend the IV to the ciphertext
|
||||
ciphertext = append(iv, ciphertext...)
|
||||
|
||||
return ciphertext, nil
|
||||
return emailVerifyLink, dataURL
|
||||
}
|
||||
|
||||
func DecryptAES(base64Ciphertext string, key []byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
func EncryptAESMailLink(data string, key string, w http.ResponseWriter) (string, string) {
|
||||
claims := MyCustomClaims{
|
||||
data,
|
||||
jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
|
||||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||
NotBefore: jwt.NewNumericDate(time.Now()),
|
||||
Issuer: "totodayShop",
|
||||
},
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
token.Header["purpose"] = "Email_Verify"
|
||||
|
||||
tokenString, err := token.SignedString([]byte(key))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
fmt.Println("Internal Log: Error signed token fail", err.Error())
|
||||
_ = WriteJSONInternalError(w, "Error signing token fail")
|
||||
}
|
||||
|
||||
// Decode base64
|
||||
ciphertext, err := base64.StdEncoding.DecodeString(base64Ciphertext)
|
||||
if err != nil {
|
||||
fmt.Println("error decoding base64:", err)
|
||||
}
|
||||
|
||||
// Extract the IV from the ciphertext
|
||||
iv := ciphertext[:aes.BlockSize]
|
||||
ciphertext = ciphertext[aes.BlockSize:]
|
||||
|
||||
// Create a CBC mode cipher block
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
|
||||
// Decrypt the data
|
||||
mode.CryptBlocks(ciphertext, ciphertext)
|
||||
|
||||
// Remove padding
|
||||
ciphertext = pkcs7Unpad(ciphertext)
|
||||
|
||||
return ciphertext, nil
|
||||
return buildTokenLink(tokenString, w)
|
||||
}
|
||||
|
||||
// pkcs7Pad pads the input to a multiple of blockSize using PKCS#7 padding
|
||||
func pkcs7Pad(data []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(data)%blockSize
|
||||
padText := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(data, padText...)
|
||||
}
|
||||
//
|
||||
//func DecryptAESMailLink(data string, key string) string {
|
||||
//}
|
||||
|
||||
// pkcs7Unpad removes PKCS#7 padding from the input
|
||||
func pkcs7Unpad(data []byte) []byte {
|
||||
padding := int(data[len(data)-1])
|
||||
return data[:len(data)-padding]
|
||||
}
|
||||
//OTP
|
||||
//Write preuser to mongo db
|
||||
|
||||
//func WriteToMongo(registerInfo *models.PreusersMongo) {
|
||||
//
|
||||
//
|
||||
//}
|
||||
|
||||
//Write otp to redis
|
||||
|
||||
//func CheckAndWriteRedis(email string, username string, hashOTP string) {
|
||||
// //checking does it valid or have in redis or not
|
||||
// //var count int
|
||||
// exists, err := Redis.Exists(context.Background(), "otp:nhocdl.poro1@gmail.com").Result()
|
||||
// if err != nil {
|
||||
// fmt.Println("something went wrong")
|
||||
// }
|
||||
// if exists == 0 {
|
||||
// //count = 0
|
||||
//
|
||||
// }
|
||||
//}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
)
|
||||
|
||||
var MongoDB *mongo.Client
|
||||
var PreUserData *mongo.Collection
|
||||
|
||||
func InitMongoDriver(mongoUri string) *mongo.Client {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
@@ -30,6 +31,7 @@ func InitMongoDriver(mongoUri string) *mongo.Client {
|
||||
}
|
||||
|
||||
MongoDB = client
|
||||
PreUserData = MongoDB.Database("Totoday-shop").Collection("preusers")
|
||||
|
||||
return client
|
||||
}
|
||||
+15
-1
@@ -9,7 +9,21 @@ import (
|
||||
func WriteJSON(w http.ResponseWriter, status int, v any) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
return json.NewEncoder(w).Encode(v)
|
||||
err := json.NewEncoder(w).Encode(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteJSONInternalError(w http.ResponseWriter, v any) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
err := json.NewEncoder(w).Encode(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type ApiError struct {
|
||||
|
||||
Reference in new issue
Block a user