Skip to content

Commit

Permalink
support for json output, refactor var naming
Browse files Browse the repository at this point in the history
  • Loading branch information
JocularMarrow committed May 20, 2024
1 parent 70f2e04 commit b778fa3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 29 deletions.
31 changes: 27 additions & 4 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/json"
"fmt"
"os"

Expand All @@ -20,6 +21,8 @@ var rootCmd = &cobra.Command{
func hashRun(cmd *cobra.Command, args []string) {
filePath, _ := cmd.Flags().GetString("file")
hashType, _ := cmd.Flags().GetString("type")
jsonOutput, _ := cmd.Flags().GetBool("json")

var isFile bool
if filePath != "" {
isFile = true
Expand All @@ -44,8 +47,16 @@ func hashRun(cmd *cobra.Command, args []string) {
return
}

cmd.Println(hash.HexDigest)

if jsonOutput {
j, err := json.MarshalIndent(hash, "", " ")
if err != nil {
cmd.PrintErr(err)
return
}
cmd.Println(string(j))
} else {
cmd.Println(hash.HexDigest)
}
return
}

Expand All @@ -61,8 +72,19 @@ func hashRun(cmd *cobra.Command, args []string) {
return
}

for _, h := range hashes.Array() {
cmd.Printf("%s: %s\n", h.Type, h.Hash)
if jsonOutput {
j, err := json.MarshalIndent(hashes, "", " ")
if err != nil {
cmd.PrintErr(err)
return
}

cmd.Println(string(j))
return
} else {
for _, h := range hashes.Array() {
cmd.Printf("%s: %s\n", h.Type, h.Hash)
}
}

}
Expand All @@ -77,4 +99,5 @@ func Execute() {
func init() {
rootCmd.Flags().StringP("file", "f", "", "File to hash")
rootCmd.Flags().StringP("type", "t", "", "Type of hash function to use")
rootCmd.Flags().BoolP("json", "j", false, "Output as JSON")
}
37 changes: 21 additions & 16 deletions pkg/hash/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,36 @@ const (

// GenericHash represents a hash of data.
type GenericHash struct {
Input []byte
HashBytes []byte
HexDigest string
Duration time.Duration
Input []byte `json:"input"`
HashBytes []byte `json:"hashBytes"`
HexDigest string `json:"hexDigest"`
Duration int64 `json:"duration"`
DurationStr string `json:"durationStr"`
}

// Hash returns a hash of the data using the specified hash type.
func Hash(data []byte, hash hash.Hash) *GenericHash {
start := time.Now()
timeStart := time.Now()

sha := &GenericHash{
gh := &GenericHash{
Input: data,
}

hash.Write(data)
sha.HashBytes = hash.Sum(nil)
sha.HexDigest = fmt.Sprintf("%x", sha.HashBytes)
sha.Duration = time.Since(start)
gh.HashBytes = hash.Sum(nil)
gh.HexDigest = fmt.Sprintf("%x", gh.HashBytes)
timeSince := time.Since(timeStart)
gh.Duration = timeSince.Milliseconds()
gh.DurationStr = timeSince.String()

return sha
return gh
}

// HashFile returns a hash of the file using the specified hash type.
func HashFile(path string, hash hash.Hash) (*GenericHash, error) {
start := time.Now()
timeStart := time.Now()

sha := &GenericHash{
gh := &GenericHash{
Input: []byte(path),
}

Expand All @@ -75,11 +78,13 @@ func HashFile(path string, hash hash.Hash) (*GenericHash, error) {
hash.Write(buf[:n])
}

sha.HashBytes = hash.Sum(nil)
sha.HexDigest = fmt.Sprintf("%x", sha.HashBytes)
sha.Duration = time.Since(start)
gh.HashBytes = hash.Sum(nil)
gh.HexDigest = fmt.Sprintf("%x", gh.HashBytes)
timeSince := time.Since(timeStart)
gh.Duration = timeSince.Milliseconds()
gh.DurationStr = timeSince.String()

return sha, nil
return gh, nil
}

// ComputeHash returns a hash of the data using the specified hash type.
Expand Down
29 changes: 20 additions & 9 deletions pkg/hash/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"hash/fnv"
"io"
"os"
"time"

"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/blake2s"
Expand All @@ -21,15 +22,17 @@ import (
)

type Hashes struct {
Adler32 string `json:"adler32"`
MD4 string `json:"md4"`
MD5 string `json:"md5"`
SHA1 string `json:"sha1"`
SHA2 SHA2 `json:"sha2"`
SHA3 SHA3 `json:"sha3"`
FNV FNV `json:"fnv"`
CRC CRC `json:"crc"`
Blake Blake `json:"blake"`
Adler32 string `json:"adler32"`
MD4 string `json:"md4"`
MD5 string `json:"md5"`
SHA1 string `json:"sha1"`
SHA2 SHA2 `json:"sha2"`
SHA3 SHA3 `json:"sha3"`
FNV FNV `json:"fnv"`
CRC CRC `json:"crc"`
Blake Blake `json:"blake"`
Duration int64 `json:"duration"`
DurationStr string `json:"durationStr"`
}

type SHA2 struct {
Expand Down Expand Up @@ -177,6 +180,7 @@ func setHashes(hashes *Hashes, hashers []hash.Hash) {
}

func HasherMulti(b []byte) (Hashes, error) {
timeStart := time.Now()
hashers, hashes := initializeHashers()
multiWriter := io.MultiWriter(convertToWriters(hashers)...)

Expand All @@ -186,10 +190,14 @@ func HasherMulti(b []byte) (Hashes, error) {
}

setHashes(hashes, hashers)
timeSince := time.Since(timeStart)
hashes.Duration = timeSince.Milliseconds()
hashes.DurationStr = timeSince.String()
return *hashes, nil
}

func HasherMultiFile(path string) (Hashes, error) {
timeStart := time.Now()
hashers, hashes := initializeHashers()
multiWriter := io.MultiWriter(convertToWriters(hashers)...)

Expand All @@ -205,6 +213,9 @@ func HasherMultiFile(path string) (Hashes, error) {
}

setHashes(hashes, hashers)
timeSince := time.Since(timeStart)
hashes.Duration = timeSince.Milliseconds()
hashes.DurationStr = timeSince.String()
return *hashes, nil
}

Expand Down

0 comments on commit b778fa3

Please sign in to comment.