/
cmd_hash.go
69 lines (55 loc) · 1.49 KB
/
cmd_hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"encoding/hex"
"errors"
"fmt"
"os"
"github.com/urfave/cli/v2"
)
// cmdMakeBucket create a new Bucket
func cmdCalHash() *cli.Command {
return &cli.Command{
Name: "get-hash",
Action: computeHashRoot,
Usage: "compute the integrity hash of file",
ArgsUsage: "filePath",
Description: `
Compute the integrity hash value of the file which use same algorithm of greenfield
Examples:
$ gnfd-cmd object get-hash /home/test.text `,
}
}
func computeHashRoot(ctx *cli.Context) error {
// read the local file payload to be uploaded
filePath := ctx.Args().Get(0)
exists, objectSize, err := pathExists(filePath)
if err != nil {
return toCmdErr(err)
}
if !exists {
return errors.New("upload file not exists")
} else if objectSize > int64(5*1024*1024*1024) {
return errors.New("file size should less than 5G")
}
gnfdClient, err := NewClient(ctx, ClientOptions{IsQueryCmd: false})
if err != nil {
return toCmdErr(err)
}
// Open the referenced file.
fReader, err := os.Open(filePath)
if err != nil {
return toCmdErr(err)
}
defer fReader.Close()
hashes, size, _, err := gnfdClient.ComputeHashRoots(fReader, false)
if err != nil {
fmt.Println("compute hash root fail:", err.Error())
return toCmdErr(err)
}
fmt.Printf("the primary sp hash root: \n%s\n%s\n", hex.EncodeToString(hashes[0]), "the secondary sp hash list:")
for _, hash := range hashes[1:] {
fmt.Println(hex.EncodeToString(hash))
}
fmt.Println("file size:", size)
return nil
}