diff --git a/encryption/encrypt.go b/encryption/encrypt.go index 3f6f840..c0c8c39 100644 --- a/encryption/encrypt.go +++ b/encryption/encrypt.go @@ -17,7 +17,7 @@ var ( ) func EncryptAES(key string, input []byte) (string, error) { - keyByte, err := hex.DecodeString(AESKey) + keyByte, err := hex.DecodeString(key) if err != nil { return "", err } diff --git a/main.go b/main.go index 4d38b81..b31876f 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,12 @@ package main import ( + "encoding/json" + "fmt" "log" "net/http" + "github.com/ariefmaulidy/security-workshop/encryption" "github.com/ariefmaulidy/security-workshop/messaging" "github.com/nsqio/go-nsq" ) @@ -53,7 +56,7 @@ func main() { // initiate consumer consumer, err := messaging.NewConsumer(messaging.ConsumerConfig{ - Topic: "test", // Change the topic + Topic: "andr", // Change the topic Channel: "test", // Change the channel LookupAddress: "172.18.59.254:4161", MaxAttempts: defaultConsumerMaxAttempts, @@ -67,18 +70,45 @@ func main() { http.HandleFunc("/publish_payment", handlePublish) go messaging.RunConsumer(consumer) - log.Fatal(http.ListenAndServe(":8090", nil)) + log.Fatal(http.ListenAndServe(":9889", nil)) } func handlePublish(w http.ResponseWriter, r *http.Request) { // Do Publish - topic := "" // TODO: update to given topic name - msg := "" // TODO: write your message here - producer.Publish(topic, msg) + topic := "andr" // TODO: update to given topic name + msg := Payment{ + TotalPayment: 10000, + } + + jsonMsg, err := json.Marshal(msg) + if err != nil { + fmt.Println("Marshal Error: ", err) + return + } + + cipherMsg, err := encryption.EncrpytRSA(encryption.ServiceA, jsonMsg) + if err != nil { + fmt.Println("Encrypt RSA Error: ", err) + return + } + + producer.Publish(topic, cipherMsg) } func handleMessage(message *nsq.Message) error { // Handle message NSQ here + cipheredMsg := string(message.Body) + + byteMsg, err := encryption.DecryptRSA(encryption.ServiceB, cipheredMsg) + if err != nil { + return err + } + + var paymentData Payment + err = json.Unmarshal(byteMsg, &paymentData) + if err != nil { + return err + } message.Finish() return nil