mirror of
https://github.com/gravitl/netmaker.git
synced 2025-02-28 18:13:00 +08:00
revert
This commit is contained in:
parent
5cc6507a0c
commit
20adc73e2a
3 changed files with 4 additions and 54 deletions
|
@ -26,7 +26,7 @@ func decryptMsg(node *models.Node, msg []byte) ([]byte, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return ncutils.DestructMessage(string(msg), nodePubTKey, serverPrivTKey)
|
||||
return ncutils.BoxDecrypt(msg, nodePubTKey, serverPrivTKey)
|
||||
}
|
||||
|
||||
func encryptMsg(node *models.Node, msg []byte) ([]byte, error) {
|
||||
|
@ -46,8 +46,7 @@ func encryptMsg(node *models.Node, msg []byte) ([]byte, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
var encrypted, encErr = ncutils.BuildMessage(msg, nodePubKey, serverPrivKey)
|
||||
return []byte(encrypted), encErr
|
||||
return ncutils.BoxEncrypt(msg, nodePubKey, serverPrivKey)
|
||||
}
|
||||
|
||||
func publish(node *models.Node, dest string, msg []byte) error {
|
||||
|
|
|
@ -552,7 +552,7 @@ func publish(cfg *config.ClientConfig, dest string, msg []byte) error {
|
|||
|
||||
client := SetupMQTT(cfg, true)
|
||||
defer client.Disconnect(250)
|
||||
encrypted, err := ncutils.BuildMessage(msg, serverPubKey, trafficPrivKey)
|
||||
encrypted, err := ncutils.BoxEncrypt(msg, serverPubKey, trafficPrivKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -583,7 +583,7 @@ func decryptMsg(cfg *config.ClientConfig, msg []byte) ([]byte, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return ncutils.DestructMessage(string(msg), serverPubKey, diskKey)
|
||||
return ncutils.BoxDecrypt(msg, serverPubKey, diskKey)
|
||||
}
|
||||
|
||||
func pingServer(cfg *config.ClientConfig) error {
|
||||
|
|
|
@ -2,7 +2,6 @@ package ncutils
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -23,51 +22,3 @@ func BackOff(isExponential bool, maxTime int, f interface{}) (interface{}, error
|
|||
}
|
||||
return nil, fmt.Errorf("could not find result")
|
||||
}
|
||||
|
||||
// DestructMessage - reconstruct original message through chunks
|
||||
func DestructMessage(builtMsg string, senderPublicKey *[32]byte, recipientPrivateKey *[32]byte) ([]byte, error) {
|
||||
var chunks = strings.Split(builtMsg, splitKey)
|
||||
var totalMessage = make([]byte, len(builtMsg))
|
||||
for _, chunk := range chunks {
|
||||
var bytes, decErr = BoxDecrypt([]byte(chunk), senderPublicKey, recipientPrivateKey)
|
||||
if decErr != nil || bytes == nil {
|
||||
return nil, decErr
|
||||
}
|
||||
totalMessage = append(totalMessage, bytes...)
|
||||
}
|
||||
return totalMessage, nil
|
||||
}
|
||||
|
||||
// BuildMessage Build a message for publishing
|
||||
func BuildMessage(originalMessage []byte, recipientPubKey *[32]byte, senderPrivateKey *[32]byte) (string, error) {
|
||||
chunks := getSliceChunks(originalMessage, 16128)
|
||||
var sb strings.Builder
|
||||
for i := 0; i < len(chunks); i++ {
|
||||
var encryptedText, encryptErr = BoxEncrypt(chunks[i], recipientPubKey, senderPrivateKey)
|
||||
if encryptErr != nil {
|
||||
return "", encryptErr
|
||||
}
|
||||
sb.Write(encryptedText)
|
||||
if i < len(chunks)-1 {
|
||||
sb.WriteString(splitKey)
|
||||
}
|
||||
}
|
||||
return sb.String(), nil
|
||||
}
|
||||
|
||||
var splitKey = "<|#|>"
|
||||
|
||||
func getSliceChunks(slice []byte, chunkSize int) [][]byte {
|
||||
var chunks [][]byte
|
||||
for i := 0; i < len(slice); i += chunkSize {
|
||||
lastByte := i + chunkSize
|
||||
|
||||
if lastByte > len(slice) {
|
||||
lastByte = len(slice)
|
||||
}
|
||||
|
||||
chunks = append(chunks, slice[i:lastByte])
|
||||
}
|
||||
|
||||
return chunks
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue