X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcheck.go;h=a4e0d06d31ea7cc51048acb4ec8b0d7a5a0dbd9f;hb=d386e5b28dc603ac4ffac10337f322e15bc92d80;hp=a8a32e2c7899bcb68c4c0f6b23394f72312c2aac;hpb=26d25d4822c87a6c89aabf32f273e627b476ef8c;p=nncp.git diff --git a/src/check.go b/src/check.go index a8a32e2..a4e0d06 100644 --- a/src/check.go +++ b/src/check.go @@ -1,6 +1,6 @@ /* NNCP -- Node to Node copy, utilities for store-and-forward data exchange -Copyright (C) 2016-2021 Sergey Matveev +Copyright (C) 2016-2023 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,24 +23,28 @@ import ( "errors" "fmt" "io" - "log" "os" "path/filepath" - - "golang.org/x/crypto/blake2b" ) const NoCKSuffix = ".nock" -func Check(src io.Reader, checksum []byte, les LEs, showPrgrs bool) (bool, error) { - hsh, err := blake2b.New256(nil) - if err != nil { - log.Fatalln(err) - } - if _, err = CopyProgressed(hsh, bufio.NewReader(src), "check", les, showPrgrs); err != nil { +func Check( + src io.Reader, + size int64, + checksum []byte, + les LEs, + showPrgrs bool, +) (bool, error) { + hsh := MTHNew(size, 0) + if _, err := CopyProgressed( + hsh, + bufio.NewReaderSize(src, MTHBlockSize), + "check", les, showPrgrs, + ); err != nil { return false, err } - return bytes.Compare(hsh.Sum(nil), checksum) == 0, nil + return bytes.Equal(hsh.Sum(nil), checksum), nil } func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool { @@ -61,8 +65,8 @@ func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool { ctx.LogE("checking", les, err, logMsg) return true } - gut, err := Check(fd, job.HshValue[:], les, ctx.ShowPrgrs) - fd.Close() // #nosec G104 + gut, err := Check(fd, job.Size, job.HshValue[:], les, ctx.ShowPrgrs) + fd.Close() if err != nil { ctx.LogE("checking", les, err, logMsg) return true @@ -79,7 +83,7 @@ func (ctx *Ctx) Check(nodeId *NodeId) bool { return !(ctx.checkXxIsBad(nodeId, TRx) || ctx.checkXxIsBad(nodeId, TTx)) } -func (ctx *Ctx) CheckNoCK(nodeId *NodeId, hshValue *[32]byte) (int64, error) { +func (ctx *Ctx) CheckNoCK(nodeId *NodeId, hshValue *[MTHSize]byte, mth MTH) (int64, error) { dirToSync := filepath.Join(ctx.Spool, nodeId.String(), string(TRx)) pktName := Base32Codec.EncodeToString(hshValue[:]) pktPath := filepath.Join(dirToSync, pktName) @@ -92,7 +96,6 @@ func (ctx *Ctx) CheckNoCK(nodeId *NodeId, hshValue *[32]byte) (int64, error) { if err != nil { return 0, err } - defer fd.Close() size := fi.Size() les := LEs{ {"XX", string(TRx)}, @@ -100,7 +103,20 @@ func (ctx *Ctx) CheckNoCK(nodeId *NodeId, hshValue *[32]byte) (int64, error) { {"Pkt", pktName}, {"FullSize", size}, } - gut, err := Check(fd, hshValue[:], les, ctx.ShowPrgrs) + var gut bool + if mth == nil { + gut, err = Check(fd, size, hshValue[:], les, ctx.ShowPrgrs) + } else { + if _, err = mth.PreaddFrom( + bufio.NewReaderSize(fd, MTHBlockSize), + pktName, ctx.ShowPrgrs, + ); err != nil { + return 0, err + } + if bytes.Equal(mth.Sum(nil), hshValue[:]) { + gut = true + } + } if err != nil || !gut { return 0, errors.New("checksum mismatch") }