Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion encryption/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
40 changes: 35 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down