]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-hash/main.go
Sequential MTH optimization
[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-2021 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/v7"
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         var mth nncp.MTH
80         if *forceFat {
81                 mth = nncp.MTHFatNew(size, int64(*seek))
82         } else {
83                 mth = nncp.MTHNew(size, int64(*seek))
84         }
85         var debugger sync.WaitGroup
86         if *debug {
87                 fmt.Println("Leaf BLAKE3 key:", hex.EncodeToString(nncp.MTHLeafKey[:]))
88                 fmt.Println("Node BLAKE3 key:", hex.EncodeToString(nncp.MTHNodeKey[:]))
89                 events := mth.Events()
90                 debugger.Add(1)
91                 go func() {
92                         for e := range events {
93                                 var t string
94                                 switch e.Type {
95                                 case nncp.MTHEventAppend:
96                                         t = "Add"
97                                 case nncp.MTHEventPrepend:
98                                         t = "Pre"
99                                 case nncp.MTHEventFold:
100                                         t = "Fold"
101                                 }
102                                 fmt.Printf(
103                                         "%s\t%03d\t%06d\t%s\n",
104                                         t, e.Level, e.Ctr, hex.EncodeToString(e.Hsh),
105                                 )
106                         }
107                         debugger.Done()
108                 }()
109         }
110         if *seek != 0 {
111                 if *fn == "" {
112                         log.Fatalln("-file is required with -seek")
113                 }
114                 if _, err = fd.Seek(int64(*seek), io.SeekStart); err != nil {
115                         log.Fatalln(err)
116                 }
117         }
118         if _, err = nncp.CopyProgressed(
119                 mth, bufio.NewReaderSize(fd, nncp.MTHBlockSize),
120                 "hash", nncp.LEs{{K: "Pkt", V: *fn}, {K: "FullSize", V: size - int64(*seek)}},
121                 *showPrgrs,
122         ); err != nil {
123                 log.Fatalln(err)
124         }
125         if *seek != 0 {
126                 if _, err = fd.Seek(0, io.SeekStart); err != nil {
127                         log.Fatalln(err)
128                 }
129                 if *showPrgrs {
130                         mth.SetPktName(*fn)
131                 }
132                 if _, err = mth.PrependFrom(bufio.NewReaderSize(fd, nncp.MTHBlockSize)); err != nil {
133                         log.Fatalln(err)
134                 }
135         }
136         sum := mth.Sum(nil)
137         debugger.Wait()
138         fmt.Println(hex.EncodeToString(sum))
139 }