]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-hash/main.go
Raise copyright years
[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-2022 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.Fprintf(os.Stderr, nncp.UsageHeader())
36         fmt.Fprintf(os.Stderr, "nncp-hash -- calculate MTH hash of the file\n\n")
37         fmt.Fprintf(os.Stderr, "Usage: %s [-file ...] [-seek X] [-debug] [-progress] [options]\nOptions:\n", os.Args[0])
38         flag.PrintDefaults()
39 }
40
41 func main() {
42         var (
43                 fn        = flag.String("file", "", "Read the file instead of stdin")
44                 seek      = flag.Uint64("seek", 0, "Seek the file, hash, rewind, hash remaining")
45                 forceFat  = flag.Bool("force-fat", false, "Force MTHFat implementation usage")
46                 showPrgrs = flag.Bool("progress", false, "Progress showing")
47                 debug     = flag.Bool("debug", false, "Print MTH steps calculations")
48                 version   = flag.Bool("version", false, "Print version information")
49                 warranty  = flag.Bool("warranty", false, "Print warranty information")
50         )
51         log.SetFlags(log.Lshortfile)
52         flag.Usage = usage
53         flag.Parse()
54         if *warranty {
55                 fmt.Println(nncp.Warranty)
56                 return
57         }
58         if *version {
59                 fmt.Println(nncp.VersionGet())
60                 return
61         }
62
63         fd := os.Stdin
64         var err error
65         var size int64
66         if *fn == "" {
67                 *showPrgrs = false
68         } else {
69                 fd, err = os.Open(*fn)
70                 if err != nil {
71                         log.Fatalln(err)
72                 }
73                 fi, err := fd.Stat()
74                 if err != nil {
75                         log.Fatalln(err)
76                 }
77                 size = fi.Size()
78         }
79
80         if *debug {
81                 fmt.Println("Leaf BLAKE3 key:", hex.EncodeToString(nncp.MTHLeafKey[:]))
82                 fmt.Println("Node BLAKE3 key:", hex.EncodeToString(nncp.MTHNodeKey[:]))
83         }
84
85         var debugger sync.WaitGroup
86         startDebug := func(events chan nncp.MTHEvent) {
87                 debugger.Add(1)
88                 go func() {
89                         for e := range events {
90                                 fmt.Println(e.String())
91                         }
92                         debugger.Done()
93                 }()
94         }
95         copier := func(w io.Writer) error {
96                 _, err := nncp.CopyProgressed(
97                         w, bufio.NewReaderSize(fd, nncp.MTHBlockSize), "hash",
98                         nncp.LEs{{K: "Pkt", V: *fn}, {K: "FullSize", V: size - int64(*seek)}},
99                         *showPrgrs,
100                 )
101                 return err
102         }
103
104         var sum []byte
105         if *forceFat {
106                 mth := nncp.MTHFatNew()
107                 if *debug {
108                         startDebug(mth.Events())
109
110                 }
111                 if err = copier(mth); err != nil {
112                         log.Fatalln(err)
113                 }
114                 sum = mth.Sum(nil)
115         } else {
116                 mth := nncp.MTHSeqNew(size, int64(*seek))
117                 if *debug {
118                         startDebug(mth.Events())
119                 }
120                 if *seek != 0 {
121                         if *fn == "" {
122                                 log.Fatalln("-file is required with -seek")
123                         }
124                         if _, err = fd.Seek(int64(*seek), io.SeekStart); err != nil {
125                                 log.Fatalln(err)
126                         }
127                 }
128                 if err = copier(mth); err != nil {
129                         log.Fatalln(err)
130                 }
131                 if *seek != 0 {
132                         if _, err = fd.Seek(0, io.SeekStart); err != nil {
133                                 log.Fatalln(err)
134                         }
135                         if _, err = mth.PreaddFrom(
136                                 bufio.NewReaderSize(fd, nncp.MTHBlockSize),
137                                 *fn, *showPrgrs,
138                         ); err != nil {
139                                 log.Fatalln(err)
140                         }
141                 }
142                 sum = mth.Sum(nil)
143         }
144         debugger.Wait()
145         fmt.Println(hex.EncodeToString(sum))
146 }