Foto User
Microservice with Golang implementation using SHA2

Muhammad Panji

Sosial Media


0 orang menyukai ini
Suka

Summary

Here is an example implementation of using the Go language for building a microservice for cyber security. The data being used is student data encrypted using SHA-256 in Go

Description

Simple Server Implementation in Cybersecurity by choosing a server:

VPS 1

110.239.68.10

pass: Bisaai123

 

The first step is to do when entering the server with the command on cmd as follows:

then an output will appear asking for a password then enter the password according to what has been determined, if the password is successful then the display will appear as follows:

 

If you have entered the server, then create a private folder used for the needs of the microservice itself. With the following command:

If you have written the command, check in our server folder whether it has been created, using the following command

If you have created a folder(caption 1)then change directory into that folder with the following command:

If you have entered the folder directory that was created, then create a file called cyber.go on server 1 to create the first microservice, in the following way:

Then the GNU notepad display will appear and write the golang code and set the port according to what you want (here the server port 1 is set7000):

cyber.go:

package play

import (

            "encoding/json"

            "fmt"

            "logs"

            "net/http"

            "strconv"

            "crypto/sha256"

            "encoding/hex"

)

 

// Student

type Student struct {

            ID int `json:"id"`

            NIM int `json:"nim"`

            Name string `json:"name"`

            Hash string `json:"hash"`

}

 

// PostStudent

func PostStudent(w http.ResponseWriter, r *http.Request) {

            w.Header().Set("Content-Type", "application/json")

            var Student Student

            if r. Method == "POST" {

                        if r.Header.Get("Content-Type") == "application/json" {

                                    // parse from json

                                    decodeJSON := json.NewDecoder(r.Body)

                                    if err := decodeJSON.Decode(&Mhs); err != nil {

                                                log. Fatal(err)

                                    }

                        } else {

                                    // parse from form

                                    getID := r.PostFormValue("id")

                                    id, _ := strconv. Atoi(getID)

                                    getNim := r. PostFormValue("nim")

                                    nim, _ := strconv. Atoi(getNim)

                                    name := r. PostFormValue("name")

                                    

                                    Student = Student{

                                                id:id,

                                                NIM: nim,

                                                Name: name,

                                    }

                                    

                        }

                        

                        // Convert struct to byte slice

                        data_slice := []byte(fmt. Sprintf("%v", Mhs))

 

                        // Compute SHA256 hashes

                        hash := sha256.Sum256(data_slice)

 

                        // Convert hash to hexadecimal string

                        hashString := hex.EncodeToString(hash[:])

                        

                        Mhs. Hash = hashString

                        

                        dataMahasiswa, _ := json.Marshal(Mhs) // to byte

                        w.Write(Studentdata) // print in browser

                        returns

            }

 

            http.Error(w, "what are you going to do", http.StatusNotFound)

            returns

}

 

func main() {

            http.HandleFunc("/post_mahasiswa", PostMahasiswa)

            fmt. Println("server running...")

            if err := http.ListenAndServe(":7000", nil); err != nil {

                        log. Fatal(err)

            }

}

 

Then when you have finished writing the Golang code and set the port, please save using ctrl + x first, then check the file name if it matches the name, namely cyber.go, press enter. If so, the next step is to run the microservice on server 1 with the command:

And the following display will appear if server 1 microservice is successfully running:

If the microservice is already running, it's time to try whether the golang is running properly by using the postman application, as follows:

 

In the Postman application, we need to set requests with methods according to the golang source code, namely using the POST method. Then write down the IP server 1IP 110.239.68.10alongthe ports:7000 and the route that has been created ie/post_student (description 1).

If you have selected Body and raw then the text set is JSON. And write the code according to the parameters received by the source code, namelyid, nim name and data hashcomes from post data, with the following code (remark 2):

{

"id":2021,

"nim":200,

"name":"Muhammad's Banner"

}

If so, then we try to try throwing data using the post method using post, is it successful in creating a blockchain on server 1. If successful, postman will produce a json-valued result as below:

{

"id":2021,

"nim":200,

"name":"Muhammad's Banner",

"hash":"f440fd8cfdc74ef6adb655bbd1113393bedf263b5bef16451dd001c89b12a838"

}

By implementing sha-256 cryptography on thrown data so that data is more secure and microservices can be run on the desired cloud/server.

Thank You

 

 

 

 

 

 

Informasi Course Terkait
  Kategori: Cyber Security
  Course: Persiapan Sertifikasi Internasional CyberSAFE (Cyber Security Awareness For Everyone)