From 3d3453e9b51644821f573f2ec204275881767e59 Mon Sep 17 00:00:00 2001 From: AAVision <75311052+AAVision@users.noreply.github.com> Date: Wed, 19 Jul 2023 16:43:04 +0300 Subject: [PATCH] Adding checksums --- .gitignore | 1 + README.md | 48 ++++++++++++++++++++++++++++++++++++++++- checker.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 11 ++++++++++ go.sum | 18 ++++++++++++++++ main.go | 32 +++++++++++++++++++++++++++ 6 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 checker.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore index 3b735ec..37f006b 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out +*.txt # Dependency directories (remove the comment below to include it) # vendor/ diff --git a/README.md b/README.md index 0896f70..eeb961e 100644 --- a/README.md +++ b/README.md @@ -1 +1,47 @@ -# go-checksums \ No newline at end of file +# go-checksums + +It's a small utlity used for getting checksum for any file! + +## Usage + +```bash + go-checksums.exe -f, --file FILE +``` + +## Demo + +```bash + go run . --file test.txt + Processing... + + MD5: + 04ff2868ab8b0fd8c5a7075e935b451c + + Sha1: + 319c41f3bc8e4c2df19d60ff04e5bac255e36ffe + + Sha224: + 60428e84740e0eafad716e063ac086cc65593bb88dece289600b5cd1 + + Sha256: + b9bbd2bbe3fd9b9c2121ddfdfda6c068b87a0403672067baa0d4527d836552e7 + + Sum384: + e06e912ca5b3c3546fd4df8142134817868abe4ab4e16c2f77c88a8da055720973975344622e0507bea48b5004cf46ec + + Sha512: + f1f3a99cde58c1f59b56f707592f761e3f79f529be1e2d4b0c138f413482e83721e37ccf6760bb2661b45963b17b6d557f92fd1d3481c3351c34c7740c701559 + + SHA512_224: + 5af1c3f73f18b9849a3493677e39fedd4b98e380863d9ddeb07e5914 + + SHA512_256: + 49ecd0363616a4db7c3e36eb4a31e740cc0aad594076e0fac81a3739e1bc601b + + Finished in: 7.5266ms +``` + +## License + +This project is licensed under the [MIT License](https://github.com/go-nerds/go-checksums/blob/main/LICENSE). See the [LICENSE](https://github.com/go-nerds/go-checksums/blob/main/LICENSE) file for details. + diff --git a/checker.go b/checker.go new file mode 100644 index 0000000..47d2150 --- /dev/null +++ b/checker.go @@ -0,0 +1,63 @@ +package main + +import ( + "bufio" + "crypto/md5" + "crypto/sha1" + "crypto/sha256" + "crypto/sha512" + "errors" + "fmt" + "io" + "os" + + "github.com/gookit/color" +) + +func generateHashes(filePath string) error { + + file, err := os.Open(filePath) + if err != nil { + return errors.New("error in reading the file") + } + + defer file.Close() + + stat, err := file.Stat() + if err != nil { + return errors.New("error returning the File Info") + } + + bs := make([]byte, stat.Size()) + _, err = bufio.NewReader(file).Read(bs) + + if err != nil && err != io.EOF { + return errors.New("error in parsing the file") + } + + color.Cyan.Println("MD5:") + fmt.Printf("%x\n\n", md5.Sum(bs)) + + color.Cyan.Println("Sha1:") + fmt.Printf("%x\n\n", sha1.Sum(bs)) + + color.Cyan.Println("Sha224:") + fmt.Printf("%x\n\n", sha256.Sum224(bs)) + + color.Cyan.Println("Sha256:") + fmt.Printf("%x\n\n", sha256.Sum256(bs)) + + color.Cyan.Println("Sum384:") + fmt.Printf("%x\n\n", sha512.Sum384(bs)) + + color.Cyan.Println("Sha512:") + fmt.Printf("%x\n\n", sha512.Sum512(bs)) + + color.Cyan.Println("SHA512_224:") + fmt.Printf("%x\n\n", sha512.Sum512_224(bs)) + + color.Cyan.Println("SHA512_256:") + fmt.Printf("%x\n\n", sha512.Sum512_256(bs)) + + return nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..417ea2b --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/go-nerds/go-checksums + +go 1.20 + +require ( + github.com/alexflint/go-arg v1.4.3 // indirect + github.com/alexflint/go-scalar v1.1.0 // indirect + github.com/gookit/color v1.5.3 // indirect + github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect + golang.org/x/sys v0.6.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b9c8aed --- /dev/null +++ b/go.sum @@ -0,0 +1,18 @@ +github.com/alexflint/go-arg v1.4.3 h1:9rwwEBpMXfKQKceuZfYcwuc/7YY7tWJbFsgG5cAU/uo= +github.com/alexflint/go-arg v1.4.3/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA= +github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM= +github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gookit/color v1.5.3 h1:twfIhZs4QLCtimkP7MOxlF3A0U/5cDPseRT9M/+2SCE= +github.com/gookit/color v1.5.3/go.mod h1:NUzwzeehUfl7GIb36pqId+UGmRfQcU/WiiyTTeNjHtE= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..45e5367 --- /dev/null +++ b/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "time" + + "github.com/alexflint/go-arg" + "github.com/gookit/color" +) + +type UserInput struct { + File string `arg:"-f,required"` +} + +func main() { + + userInput := UserInput{} + arg.MustParse(&userInput) + + color.Yellowln("Processing...") + start := time.Now() + + err := generateHashes(userInput.File) + + if err != nil { + fmt.Println(err) + } + + duration := time.Since(start) + color.Green.Println("Finished in:", duration) + +}