Upstream sync
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -7,6 +7,7 @@
|
|||||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
*.out
|
*.out
|
||||||
|
|
||||||
|
backend
|
||||||
debug
|
debug
|
||||||
.vscode
|
.vscode
|
||||||
.vs/
|
.vs/
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ func Load() error {
|
|||||||
// 9000 DEFAULT FOR DEV ENVIRONMENT
|
// 9000 DEFAULT FOR DEV ENVIRONMENT
|
||||||
Port: getEnv("NOVATECH_SERVICE_PORT", "9000"),
|
Port: getEnv("NOVATECH_SERVICE_PORT", "9000"),
|
||||||
Environment: getEnv("NOVATECH_SERVICE_ENVIRONMENT", "DEV"),
|
Environment: getEnv("NOVATECH_SERVICE_ENVIRONMENT", "DEV"),
|
||||||
|
BlockchainSecret: getEnv("NOVATECH_SERVICE_BLOCKCHAIN_SECRET", "novatech_service_blockchain_secret"),
|
||||||
},
|
},
|
||||||
AdminService: Service{
|
AdminService: Service{
|
||||||
// 8080 DEFAULT FOR DEV ENVIRONMENT
|
// 8080 DEFAULT FOR DEV ENVIRONMENT
|
||||||
@@ -37,9 +38,7 @@ func Load() error {
|
|||||||
DatabaseName: getEnv("NOVATECH_DATABASE_NAME", "postgres"),
|
DatabaseName: getEnv("NOVATECH_DATABASE_NAME", "postgres"),
|
||||||
HostName: getEnv("NOVATECH_DATABASE_ADDRESS", "localhost"),
|
HostName: getEnv("NOVATECH_DATABASE_ADDRESS", "localhost"),
|
||||||
Port: getEnv("NOVATECH_DATABASE_PORT", " "),
|
Port: getEnv("NOVATECH_DATABASE_PORT", " "),
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ type Config struct {
|
|||||||
Database Database
|
Database Database
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Service contains configuration for service
|
// Service contains configuration for service
|
||||||
type Service struct {
|
type Service struct {
|
||||||
Port string
|
Port string
|
||||||
Environment string
|
Environment string
|
||||||
WebPageURL string
|
WebPageURL string
|
||||||
|
BlockchainSecret string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Database configuration
|
// Database configuration
|
||||||
@@ -23,4 +23,3 @@ type Database struct {
|
|||||||
HostName string
|
HostName string
|
||||||
Port string
|
Port string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import (
|
|||||||
|
|
||||||
type AuthController struct{}
|
type AuthController struct{}
|
||||||
|
|
||||||
|
|
||||||
func (AuthController) HandleLogin(c *gin.Context) {
|
func (AuthController) HandleLogin(c *gin.Context) {
|
||||||
userId := "123"
|
userId := "123"
|
||||||
username := "Beast"
|
username := "Beast"
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ func SaveDeviceInfofunc(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
deviceInfo.RawJSON = string(rawData)
|
deviceInfo.RawJSON = string(rawData)
|
||||||
|
|
||||||
// Attempt to find the device by IMEI; if not found, create a new device
|
// Attempt to find the device by IMEI; if not found, create a new device
|
||||||
@@ -52,7 +51,6 @@ func SaveDeviceInfofunc(c *gin.Context) {
|
|||||||
deviceInfo.DeviceID = device.ID
|
deviceInfo.DeviceID = device.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Save deviceInfo to your database
|
// Save deviceInfo to your database
|
||||||
if err := shared.GetDb().Create(&deviceInfo).Error; err != nil {
|
if err := shared.GetDb().Create(&deviceInfo).Error; err != nil {
|
||||||
log.Printf("SaveDeviceInfo CREATE -DeviceInfo DB Error: %v", err)
|
log.Printf("SaveDeviceInfo CREATE -DeviceInfo DB Error: %v", err)
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ func TestSaveDeviceInfofunc(t *testing.T) {
|
|||||||
t.Fatalf("Expected no error, got %v", err)
|
t.Fatalf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
assert.Equal(t, 400, resp.StatusCode)
|
assert.Equal(t, 400, resp.StatusCode)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -7,3 +7,4 @@ NOVATECH_DATABASE_PASSWORD=password
|
|||||||
NOVATECH_DATABASE_NAME=dbname
|
NOVATECH_DATABASE_NAME=dbname
|
||||||
NOVATECH_DATABASE_ADDRESS=localhost
|
NOVATECH_DATABASE_ADDRESS=localhost
|
||||||
NOVATECH_DATABASE_PORT=5432
|
NOVATECH_DATABASE_PORT=5432
|
||||||
|
NOVATECH_SERVICE_BLOCKCHAIN_SECRET="abc&1*~#^2^#s0^=)^^7%b34"
|
||||||
3
go.mod
3
go.mod
@@ -1,6 +1,6 @@
|
|||||||
module gitlab.com/pactual1/backend
|
module gitlab.com/pactual1/backend
|
||||||
|
|
||||||
go 1.21.0
|
go 1.21
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||||
@@ -59,6 +59,7 @@ require (
|
|||||||
golang.org/x/net v0.10.0 // indirect
|
golang.org/x/net v0.10.0 // indirect
|
||||||
golang.org/x/sys v0.8.0 // indirect
|
golang.org/x/sys v0.8.0 // indirect
|
||||||
golang.org/x/text v0.9.0 // indirect
|
golang.org/x/text v0.9.0 // indirect
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
google.golang.org/protobuf v1.30.0 // indirect
|
google.golang.org/protobuf v1.30.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
3
go.sum
3
go.sum
@@ -206,8 +206,9 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
|
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
|
||||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
|
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
|
||||||
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||||
|
|||||||
12
main.go
12
main.go
@@ -8,7 +8,7 @@ import (
|
|||||||
"gitlab.com/pactual1/backend/config"
|
"gitlab.com/pactual1/backend/config"
|
||||||
"gitlab.com/pactual1/backend/models"
|
"gitlab.com/pactual1/backend/models"
|
||||||
"gitlab.com/pactual1/backend/routes"
|
"gitlab.com/pactual1/backend/routes"
|
||||||
"gitlab.com/pactual1/backend/services/contact"
|
"gitlab.com/pactual1/backend/services/contract"
|
||||||
"gitlab.com/pactual1/backend/services/erp"
|
"gitlab.com/pactual1/backend/services/erp"
|
||||||
"gitlab.com/pactual1/backend/services/messaging"
|
"gitlab.com/pactual1/backend/services/messaging"
|
||||||
"gitlab.com/pactual1/backend/shared"
|
"gitlab.com/pactual1/backend/shared"
|
||||||
@@ -22,7 +22,6 @@ var DB *gorm.DB
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
|
||||||
// LOAD APPLICATION CONFIGURATION
|
// LOAD APPLICATION CONFIGURATION
|
||||||
err := config.Load()
|
err := config.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -63,17 +62,20 @@ func main() {
|
|||||||
// Initialize channels
|
// Initialize channels
|
||||||
messagingChannel := make(chan string)
|
messagingChannel := make(chan string)
|
||||||
erpChannel := make(chan string)
|
erpChannel := make(chan string)
|
||||||
contactChannel := make(chan string)
|
contractChannel := make(chan string)
|
||||||
|
|
||||||
// Start services and pass the respective channels
|
// Start services and pass the respective channels
|
||||||
go messaging.MessagingService(messagingChannel)
|
go messaging.MessagingService(messagingChannel)
|
||||||
go erp.ERPService(erpChannel)
|
go erp.ERPService(erpChannel)
|
||||||
go contact.ContactService(contactChannel)
|
|
||||||
|
encryptionClient := shared.NewEncryptionClient(config.AppConfig.Service.BlockchainSecret)
|
||||||
|
contractService := contract.NewService(contractChannel, shared.GetDb(), encryptionClient)
|
||||||
|
go contractService.ContractService()
|
||||||
|
|
||||||
// Sending messages via channels
|
// Sending messages via channels
|
||||||
messagingChannel <- "Send an email"
|
messagingChannel <- "Send an email"
|
||||||
erpChannel <- "Update ERP record"
|
erpChannel <- "Update ERP record"
|
||||||
contactChannel <- "Update contact info"
|
contractChannel <- "Update contract info"
|
||||||
|
|
||||||
// Initialize Gin
|
// Initialize Gin
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
package middlewares
|
package middlewares
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -102,4 +102,3 @@ func ValidateToken(tokenString string, key string) (*jwt.Token, error) {
|
|||||||
|
|
||||||
return token, err
|
return token, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ type Company struct {
|
|||||||
Devices []Device
|
Devices []Device
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// func FetchCompanies(companies *[]Company) (err error) {
|
// func FetchCompanies(companies *[]Company) (err error) {
|
||||||
// db := gorm.GetDb()
|
// db := gorm.GetDb()
|
||||||
|
|
||||||
|
|||||||
23
models/contract.go
Normal file
23
models/contract.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/jinzhu/gorm"
|
||||||
|
|
||||||
|
type Contract struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string
|
||||||
|
DeviceID []uint
|
||||||
|
BuyerID uint
|
||||||
|
Status string
|
||||||
|
BlockchainID string
|
||||||
|
ContractInfos []ContractInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (Contract) Update() (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
func (Contract) Create() (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
func (Contract) Delete() (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
18
models/contract_info.go
Normal file
18
models/contract_info.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/jinzhu/gorm"
|
||||||
|
|
||||||
|
type ContractInfo struct {
|
||||||
|
gorm.Model
|
||||||
|
RawJSON string `json:"raw_json" gorm:"type:json"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ContractInfo) Update() (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
func (ContractInfo) Create() (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
func (ContractInfo) Delete() (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
@@ -4,5 +4,4 @@ type SimpleCRUD interface {
|
|||||||
Create() (bool, error)
|
Create() (bool, error)
|
||||||
Update() (bool, error)
|
Update() (bool, error)
|
||||||
Delete() (bool, error)
|
Delete() (bool, error)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ type SensorData struct {
|
|||||||
Temperature float64 `json:"temperature"`
|
Temperature float64 `json:"temperature"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type DeviceInfo struct {
|
type DeviceInfo struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
RawJSON string `json:"raw_json" gorm:"type:json"`
|
RawJSON string `json:"raw_json" gorm:"type:json"`
|
||||||
|
|||||||
@@ -11,4 +11,3 @@ func RegisterPublicRoutes(r *gin.Engine){
|
|||||||
r.GET("/publicmessage", controllers.GetPublicText)
|
r.GET("/publicmessage", controllers.GetPublicText)
|
||||||
r.POST("/device_info", controllers.SaveDeviceInfofunc)
|
r.POST("/device_info", controllers.SaveDeviceInfofunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,5 +22,5 @@ func InitRouter(engine *gin.Engine) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func InitMiddleware(engine *gin.Engine) {
|
func InitMiddleware(engine *gin.Engine) {
|
||||||
engine.Use(middlewares.CORSMiddleware());
|
engine.Use(middlewares.CORSMiddleware())
|
||||||
}
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package contact
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ContactService(ch chan string) {
|
|
||||||
for msg := range ch {
|
|
||||||
fmt.Println("Contact Service: ", msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
28
services/contract/contract_service.go
Normal file
28
services/contract/contract_service.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package contract
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
"gitlab.com/pactual1/backend/shared"
|
||||||
|
)
|
||||||
|
|
||||||
|
type service struct {
|
||||||
|
ch chan string
|
||||||
|
db *gorm.DB
|
||||||
|
encryptionClient shared.EncryptionClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewService(ch chan string, db *gorm.DB, encryptionClient shared.EncryptionClient) service {
|
||||||
|
return service{
|
||||||
|
ch: ch,
|
||||||
|
db: db,
|
||||||
|
encryptionClient: encryptionClient,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s service) ContractService() {
|
||||||
|
for msg := range s.ch {
|
||||||
|
fmt.Println("Contract Service: ", msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,10 +11,7 @@ import (
|
|||||||
_ "github.com/jinzhu/gorm/dialects/postgres"
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
var db *gorm.DB
|
var db *gorm.DB
|
||||||
var err error
|
|
||||||
|
|
||||||
|
|
||||||
func Init() error {
|
func Init() error {
|
||||||
host := config.AppConfig.Database.HostName
|
host := config.AppConfig.Database.HostName
|
||||||
@@ -34,13 +31,12 @@ func Init() error{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
//TODO AUTOMIGRATE models once we have them
|
//TODO AUTOMIGRATE models once we have them
|
||||||
db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{}, &models.DeviceInfo{})
|
db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{}, &models.DeviceInfo{}, &models.Contract{}, &models.ContractInfo{})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func GetDb() *gorm.DB {
|
func GetDb() *gorm.DB {
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|||||||
88
shared/encryption.go
Normal file
88
shared/encryption.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package shared
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/flate"
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EncryptionClient interface {
|
||||||
|
Encrypt(string) (string, error)
|
||||||
|
Decrypt(string) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type encryptionClient struct {
|
||||||
|
Secret string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEncryptionClient(secret string) EncryptionClient {
|
||||||
|
return &encryptionClient{Secret: secret}
|
||||||
|
}
|
||||||
|
|
||||||
|
func compress(text string) ([]byte, error) {
|
||||||
|
var compressedBytes bytes.Buffer
|
||||||
|
w, err := flate.NewWriter(&compressedBytes, flate.BestCompression)
|
||||||
|
w.Write([]byte(text))
|
||||||
|
w.Close()
|
||||||
|
return compressedBytes.Bytes(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func decompress(compressedBytes []byte) (string, error) {
|
||||||
|
reader := flate.NewReader(bytes.NewReader(compressedBytes))
|
||||||
|
defer reader.Close()
|
||||||
|
textBytes := new(bytes.Buffer)
|
||||||
|
_, err := textBytes.ReadFrom(reader)
|
||||||
|
return textBytes.String(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encrypt method is to encrypt or hide any classified text
|
||||||
|
func (e encryptionClient) Encrypt(text string) (string, error) {
|
||||||
|
block, err := aes.NewCipher([]byte(e.Secret))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
compressedText, err := compress(text)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
gcm, err := cipher.NewGCM(block)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
bytes := make([]byte, gcm.NonceSize())
|
||||||
|
if _, err = io.ReadFull(rand.Reader, bytes); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
encodedText := gcm.Seal(bytes, bytes, compressedText, nil)
|
||||||
|
return base64.StdEncoding.EncodeToString(encodedText), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt method is to extract back the encrypted text
|
||||||
|
func (e encryptionClient) Decrypt(text string) (string, error) {
|
||||||
|
block, err := aes.NewCipher([]byte(e.Secret))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
cipherText, err := base64.StdEncoding.DecodeString(text)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
gcm, err := cipher.NewGCM(block)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if len(cipherText) < gcm.NonceSize() {
|
||||||
|
return "", fmt.Errorf("text is too small")
|
||||||
|
}
|
||||||
|
bytes, cipherText := cipherText[:gcm.NonceSize()], cipherText[gcm.NonceSize():]
|
||||||
|
compressedTextBytes, err := gcm.Open(nil, bytes, cipherText, nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return decompress(compressedTextBytes)
|
||||||
|
}
|
||||||
22
shared/encryption_test.go
Normal file
22
shared/encryption_test.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package shared
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEncryptionClient(t *testing.T) {
|
||||||
|
|
||||||
|
client := encryptionClient{Secret: `abc&1*~#^2^#s0^=)^^7%b34`}
|
||||||
|
|
||||||
|
t.Run("encrypt/ decyrpt works", func(t *testing.T) {
|
||||||
|
text := "sample text to test encryption"
|
||||||
|
encodedText, err := client.Encrypt(text)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEqual(t, text, encodedText)
|
||||||
|
decodedText, err := client.Decrypt(encodedText)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, text, decodedText)
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user