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