From 117d461c4944f189576f0c4967851de458ba6f32 Mon Sep 17 00:00:00 2001 From: Saumit Dinesan Date: Mon, 4 Sep 2023 16:19:32 +0530 Subject: Updating README + Creating idgen package and importing it --- README.md | 10 +++++++++- idgen/id_generator.go | 29 +++++++++++++++++++++++++++++ main.go | 15 +++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 idgen/id_generator.go diff --git a/README.md b/README.md index 4ab0c58..546c993 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Golang Backend API using Echo for FSI(File System Integrity) Application -Developing a simple Golang backend API using the [Echo framework](https://github.com/labstack/echo). This API stores IDs and their corresponding hashes in a SQL server and provides functionality to verify if a given hash matches the stored hash for a specific ID, thereby providing integrity service that aligns with the CIA (Confidentiality, Integrity, Availability) triad for data security. +Developing a simple Golang backend API using the [Echo framework](https://github.com/labstack/echo). This API stores IDs and their corresponding hashes in a SQL server and provides functionality to verify if a given hash matches the stored hash for a specific ID. All the communication between the Application and API will secured using TLS encryption(HTTPS). +Thereby providing both confidentiality and integrity service that aligns with the CIA (Confidentiality, Integrity, Availability) triad for data security. ## Getting Started @@ -40,6 +41,13 @@ Once the server is running, you can access the API endpoints to add file hashes - To add a file hash, make a POST request to `/add` with JSON data containing the ID and hash. - To verify a file hash, make a POST request to `/verify` with JSON data containing the ID and hash. +## To-Do-List +- [x] Perform short ID Generation (API/Application) +- [ ] Perform Hashing (API/Application) +- [ ] Connect with DB +- [ ] Add JSON data to DB +- [ ] Perform verification + ## License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. diff --git a/idgen/id_generator.go b/idgen/id_generator.go new file mode 100644 index 0000000..8c34f41 --- /dev/null +++ b/idgen/id_generator.go @@ -0,0 +1,29 @@ +// id_generator.go +package idgen + +import ( + "math/rand" + "time" +) + +const ( + // Define the character set for the generated IDs. + chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + + // Define the desired length of the generated IDs. + idLength = 8 +) + +// Initialize the random number generator. +func init() { + rand.Seed(time.Now().UnixNano()) +} + +// GenerateID generates a short and readable ID. +func GenerateID() string { + id := make([]byte, idLength) + for i := 0; i < idLength; i++ { + id[i] = chars[rand.Intn(len(chars))] + } + return string(id) +} diff --git a/main.go b/main.go index 34df0fa..fb9628e 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,9 @@ import ( "net/http" "github.com/labstack/echo/v4" + + "fmt" + "github.com/justsaumit/fis-golang/idgen" ) type HelloWorld struct { @@ -15,6 +18,7 @@ func main() { e.GET("/hello", Greetings) e.GET("/hello/:name", GreetingsWithParams) e.GET("/hello-queries", GreetingsWithQuery) + e.GET("/genid", GenerateIDHandler) e.Logger.Fatal(e.Start(":3000")) } @@ -37,3 +41,14 @@ func GreetingsWithQuery(c echo.Context) error { Message: "Hello World, I'm using queries and my name is " + query, }) } + + +func GenerateIDHandler(c echo.Context) error { + id := idgen.GenerateID() + //Print the generated ID to the console. + fmt.Println("Generated ID:", id) + return c.JSON(http.StatusOK, map[string]string{"message": "Generated ID: " + id}) + // return c.JSON(http.StatusOK, HelloWorld{ + // Message: "Generated ID: " + id, + //}) +} -- cgit v1.2.3