Skip to content

Commit

Permalink
feat: add sha512sum
Browse files Browse the repository at this point in the history
Signed-off-by: Meng Zhuo <[email protected]>
  • Loading branch information
mengzhuo authored and rminnich committed Aug 21, 2024
1 parent 6795f99 commit 1a80242
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
14 changes: 9 additions & 5 deletions cmds/core/shasum/shasum.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bufio"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"flag"
"fmt"
"hash"
Expand All @@ -20,18 +21,21 @@ var algorithm int

var usage = "Usage:\nshasum -a <algorithm> <File Name>"

// shaPrinter prints sha1/sha256 of given data. The
// shaPrinter prints sha1/sha256/sha512 of given data. The
// value of algorithm is expected to be 1 for SHA1
// and 256 for SHA256
// 256 for SHA256
// and 512 for SHA512
func shaGenerator(w io.Writer, r io.Reader, algo int) ([]byte, error) {
var h hash.Hash
switch algo {
case 1:
h = sha1.New()
case 256:
h = sha256.New()
case 512:
h = sha512.New()
default:
return nil, fmt.Errorf("invalid algorithm, only 1 or 256 are valid")
return nil, fmt.Errorf("invalid algorithm, only 1, 256 or 512 are valid:%w", os.ErrInvalid)
}
if _, err := io.Copy(h, r); err != nil {
return nil, err
Expand Down Expand Up @@ -65,8 +69,8 @@ func shasum(w io.Writer, r io.Reader, args ...string) error {
}

func main() {
flag.IntVar(&algorithm, "algorithm", 1, "SHA algorithm, valid args are 1 and 256")
flag.IntVar(&algorithm, "a", 1, "SHA algorithm, valid args are 1 and 256 (shorthand)")
flag.IntVar(&algorithm, "algorithm", 1, "SHA algorithm, valid args are 1, 256 and 512")
flag.IntVar(&algorithm, "a", 1, "SHA algorithm, valid args are 1, 256 and 512")

flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), usage+"\n")
Expand Down
39 changes: 34 additions & 5 deletions cmds/core/shasum/shasum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -37,6 +38,7 @@ func TestSHASum(t *testing.T) {
args []string
algorithm int
want string
err error
}{
{
name: "bufIn as input with sha1 sum",
Expand All @@ -50,20 +52,26 @@ func TestSHASum(t *testing.T) {
algorithm: 256,
want: "ae0666f161fed1a5dde998bbd0e140550d2da0db27db1d0e31e370f2bd366a57 -\n",
},
{
name: "bufIn as input with sha512 sum",
args: []string{},
algorithm: 512,
want: "624eb88c6f2be3e77b1306f976bf1fb7b48855701d3ed2198a15f38bb12d76d26e8eefe6457bc036a3f93f28dd05512f5a399a319d48a58c38c590e182fe8159 -\n",
},
{
name: "wrong path file",
args: []string{"testfile"},
want: "open testfile: no such file or directory",
err: os.ErrNotExist,
},
{
name: "file1 as input with invalid algorithm",
args: []string{file1.Name()},
want: "invalid algorithm, only 1 or 256 are valid",
err: os.ErrInvalid,
},
{
name: "stdin as input with invalid algorithm",
args: []string{},
want: "invalid algorithm, only 1 or 256 are valid",
err: os.ErrInvalid,
},
{
name: "file1 as input with sha1 sum",
Expand Down Expand Up @@ -96,6 +104,26 @@ func TestSHASum(t *testing.T) {
want: fmt.Sprintf("%s %s\n%s %s\n", "ae0666f161fed1a5dde998bbd0e140550d2da0db27db1d0e31e370f2bd366a57", file1.Name(),
"db296dd0bcb796df9b327f44104029da142c8fff313a25bd1ac7c3b7562caea9", file2.Name()),
},
{
name: "file1 as input with sha512 sum",
args: []string{file1.Name()},
algorithm: 512,
want: fmt.Sprintf("%s %s\n", "624eb88c6f2be3e77b1306f976bf1fb7b48855701d3ed2198a15f38bb12d76d26e8eefe6457bc036a3f93f28dd05512f5a399a319d48a58c38c590e182fe8159", file1.Name()),
},
{
name: "file2 as input with sha512 sum",
args: []string{file2.Name()},
algorithm: 512,
want: fmt.Sprintf("%s %s\n", "53eb6dc4fc160a443941c53b40cc1d08b212b140c8a5030bb3c035e184c74898155ab811aafde46f8f4c0989fe49ac6fd72fb13bafe21b1ea32a452bf3a01c6d", file2.Name()),
},
{
name: "file1 and file 2 as input with sha512 sum",
args: []string{file1.Name(), file2.Name()},
algorithm: 512,
want: fmt.Sprintf("%s %s\n%s %s\n",
"624eb88c6f2be3e77b1306f976bf1fb7b48855701d3ed2198a15f38bb12d76d26e8eefe6457bc036a3f93f28dd05512f5a399a319d48a58c38c590e182fe8159", file1.Name(),
"53eb6dc4fc160a443941c53b40cc1d08b212b140c8a5030bb3c035e184c74898155ab811aafde46f8f4c0989fe49ac6fd72fb13bafe21b1ea32a452bf3a01c6d", file2.Name()),
},
} {
t.Run(tt.name, func(t *testing.T) {
// Setting flags
Expand All @@ -106,9 +134,10 @@ func TestSHASum(t *testing.T) {
}
bufOut := &bytes.Buffer{}
if got := shasum(bufOut, bufIn, tt.args...); got != nil {
if got.Error() != tt.want {
t.Errorf("shasum() = %q, want: %q", got.Error(), tt.want)
if tt.err != nil && errors.Is(got, tt.err) {
return
}
t.Errorf("shasum() = %q, want: %q", got, tt.err)
} else {
if bufOut.String() != tt.want {
t.Errorf("shasum() = %q, want: %q", bufOut.String(), tt.want)
Expand Down

0 comments on commit 1a80242

Please sign in to comment.