]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-hash/main.go
Remove huge usage headers, -warranty exists anyway
[nncp.git] / src / cmd / nncp-hash / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Calculate MTH hash of the file
19 package main
20
21 import (
22         "bufio"
23         "encoding/hex"
24         "flag"
25         "fmt"
26         "io"
27         "log"
28         "os"
29         "sync"
30
31         "go.cypherpunks.ru/nncp/v8"
32 )
33
34 func usage() {
35         fmt.Fprint(os.Stderr, "nncp-hash -- calculate MTH hash of the file\n\n")
36         fmt.Fprintf(os.Stderr, "Usage: %s [-file ...] [-seek X] [-debug] [-progress] [options]\nOptions:\n", os.Args[0])
37         flag.PrintDefaults()
38 }
39
40 func main() {
41         var (
42                 fn        = flag.String("file", "", "Read the file instead of stdin")
43                 seek      = flag.Uint64("seek", 0, "Seek the file, hash, rewind, hash remaining")
44                 forceFat  = flag.Bool("force-fat", false, "Force MTHFat implementation usage")
45                 showPrgrs = flag.Bool("progress", false, "Progress showing")
46                 debug     = flag.Bool("debug", false, "Print MTH steps calculations")
47                 version   = flag.Bool("version", false, "Print version information")
48                 warranty  = flag.Bool("warranty", false, "Print warranty information")
49         )
50         log.SetFlags(log.Lshortfile)
51         flag.Usage = usage
52         flag.Parse()
53         if *warranty {
54                 fmt.Println(nncp.Warranty)
55                 return
56         }
57         if *version {
58                 fmt.Println(nncp.VersionGet())
59                 return
60         }
61
62         fd := os.Stdin
63         var err error
64         var size int64
65         if *fn == "" {
66                 *showPrgrs = false
67         } else {
68                 fd, err = os.Open(*fn)
69                 if err != nil {
70                         log.Fatalln(err)
71                 }
72                 fi, err := fd.Stat()
73                 if err != nil {
74                         log.Fatalln(err)
75                 }
76                 size = fi.Size()
77         }
78
79         if *debug {
80                 fmt.Println("Leaf BLAKE3 key:", hex.EncodeToString(nncp.MTHLeafKey[:]))
81                 fmt.Println("Node BLAKE3 key:", hex.EncodeToString(nncp.MTHNodeKey[:]))
82         }
83
84         var debugger sync.WaitGroup
85         startDebug := func(events chan nncp.MTHEvent) {
86                 debugger.Add(1)
87                 go func() {
88                         for e := range events {
89                                 fmt.Println(e.String())
90                         }
91                         debugger.Done()
92                 }()
93         }
94         copier := func(w io.Writer) error {
95                 _, err := nncp.CopyProgressed(
96                         w, bufio.NewReaderSize(fd, nncp.MTHBlockSize), "hash",
97                         nncp.LEs{{K: "Pkt", V: *fn}, {K: "FullSize", V: size - int64(*seek)}},
98                         *showPrgrs,
99                 )
100                 return err
101         }
102
103         var sum []byte
104         if *forceFat {
105                 mth := nncp.MTHFatNew()
106                 if *debug {
107                         startDebug(mth.Events())
108
109                 }
110                 if err = copier(mth); err != nil {
111                         log.Fatalln(err)
112                 }
113                 sum = mth.Sum(nil)
114         } else {
115                 mth := nncp.MTHSeqNew(size, int64(*seek))
116                 if *debug {
117                         startDebug(mth.Events())
118                 }
119                 if *seek != 0 {
120                         if *fn == "" {
121                                 log.Fatalln("-file is required with -seek")
122                         }
123                         if _, err = fd.Seek(int64(*seek), io.SeekStart); err != nil {
124                                 log.Fatalln(err)
125                         }
126                 }
127                 if err = copier(mth); err != nil {
128                         log.Fatalln(err)
129                 }
130                 if *seek != 0 {
131                         if _, err = fd.Seek(0, io.SeekStart); err != nil {
132                                 log.Fatalln(err)
133                         }
134                         if _, err = mth.PreaddFrom(
135                                 bufio.NewReaderSize(fd, nncp.MTHBlockSize),
136                                 *fn, *showPrgrs,
137                         ); err != nil {
138                                 log.Fatalln(err)
139                         }
140                 }
141                 sum = mth.Sum(nil)
142         }
143         debugger.Wait()
144         fmt.Println(hex.EncodeToString(sum))
145 }