X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcmd%2Fnncp-hash%2Fmain.go;h=a350134c0486f9c46a1d3ab44526b6ea8c725aa2;hb=b47dbfe6687569650fa544a4ecf3e4ea388390cb;hp=ba0723221bc856e2f2a8822e46aa484ebb8c9a74;hpb=0fad171c0d79ad583c0faf5427e22d1d62a0a52d;p=nncp.git diff --git a/src/cmd/nncp-hash/main.go b/src/cmd/nncp-hash/main.go index ba07232..a350134 100644 --- a/src/cmd/nncp-hash/main.go +++ b/src/cmd/nncp-hash/main.go @@ -28,7 +28,7 @@ import ( "os" "sync" - "go.cypherpunks.ru/nncp/v7" + "go.cypherpunks.ru/nncp/v8" ) func usage() { @@ -42,6 +42,7 @@ func main() { var ( fn = flag.String("file", "", "Read the file instead of stdin") seek = flag.Uint64("seek", 0, "Seek the file, hash, rewind, hash remaining") + forceFat = flag.Bool("force-fat", false, "Force MTHFat implementation usage") showPrgrs = flag.Bool("progress", false, "Progress showing") debug = flag.Bool("debug", false, "Print MTH steps calculations") version = flag.Bool("version", false, "Print version information") @@ -75,59 +76,71 @@ func main() { } size = fi.Size() } - mth := nncp.MTHNew(size, int64(*seek)) - var debugger sync.WaitGroup + if *debug { fmt.Println("Leaf BLAKE3 key:", hex.EncodeToString(nncp.MTHLeafKey[:])) fmt.Println("Node BLAKE3 key:", hex.EncodeToString(nncp.MTHNodeKey[:])) - mth.Events = make(chan nncp.MTHEvent) + } + + var debugger sync.WaitGroup + startDebug := func(events chan nncp.MTHEvent) { debugger.Add(1) go func() { - for e := range mth.Events { - var t string - switch e.Type { - case nncp.MTHEventAppend: - t = "Add" - case nncp.MTHEventPrepend: - t = "Pre" - case nncp.MTHEventFold: - t = "Fold" - } - fmt.Printf( - "%s\t%03d\t%06d\t%s\n", - t, e.Level, e.Ctr, hex.EncodeToString(e.Hsh), - ) + for e := range events { + fmt.Println(e.String()) } debugger.Done() }() } - if *seek != 0 { - if *fn == "" { - log.Fatalln("-file is required with -seek") + copier := func(w io.Writer) error { + _, err := nncp.CopyProgressed( + w, bufio.NewReaderSize(fd, nncp.MTHBlockSize), "hash", + nncp.LEs{{K: "Pkt", V: *fn}, {K: "FullSize", V: size - int64(*seek)}}, + *showPrgrs, + ) + return err + } + + var sum []byte + if *forceFat { + mth := nncp.MTHFatNew() + if *debug { + startDebug(mth.Events()) + } - if _, err = fd.Seek(int64(*seek), io.SeekStart); err != nil { + if err = copier(mth); err != nil { log.Fatalln(err) } - } - if _, err = nncp.CopyProgressed( - mth, bufio.NewReaderSize(fd, nncp.MTHBlockSize), - "hash", nncp.LEs{{K: "Pkt", V: *fn}, {K: "FullSize", V: size - int64(*seek)}}, - *showPrgrs, - ); err != nil { - log.Fatalln(err) - } - if *seek != 0 { - if _, err = fd.Seek(0, io.SeekStart); err != nil { - log.Fatalln(err) + sum = mth.Sum(nil) + } else { + mth := nncp.MTHSeqNew(size, int64(*seek)) + if *debug { + startDebug(mth.Events()) } - if *showPrgrs { - mth.PktName = *fn + if *seek != 0 { + if *fn == "" { + log.Fatalln("-file is required with -seek") + } + if _, err = fd.Seek(int64(*seek), io.SeekStart); err != nil { + log.Fatalln(err) + } } - if _, err = mth.PrependFrom(bufio.NewReaderSize(fd, nncp.MTHBlockSize)); err != nil { + if err = copier(mth); err != nil { log.Fatalln(err) } + if *seek != 0 { + if _, err = fd.Seek(0, io.SeekStart); err != nil { + log.Fatalln(err) + } + if _, err = mth.PreaddFrom( + bufio.NewReaderSize(fd, nncp.MTHBlockSize), + *fn, *showPrgrs, + ); err != nil { + log.Fatalln(err) + } + } + sum = mth.Sum(nil) } - sum := mth.Sum(nil) debugger.Wait() fmt.Println(hex.EncodeToString(sum)) }