choose hash algorithm

This commit is contained in:
Zack Scholl 2021-04-20 15:32:05 -07:00
parent e9949ce3d7
commit b655afb533
3 changed files with 23 additions and 7 deletions

View File

@ -198,6 +198,7 @@ func send(c *cli.Context) (err error) {
NoCompress: c.Bool("no-compress"),
Overwrite: c.Bool("overwrite"),
Curve: c.String("curve"),
HashAlgorithm: "xxhash",
}
if crocOptions.RelayAddress != models.DEFAULT_RELAY {
crocOptions.RelayAddress6 = ""
@ -395,6 +396,7 @@ func receive(c *cli.Context) (err error) {
IP: c.String("ip"),
Overwrite: c.Bool("overwrite"),
Curve: c.String("curve"),
HashAlgorithm: "xxhash",
}
if crocOptions.RelayAddress != models.DEFAULT_RELAY {
crocOptions.RelayAddress6 = ""

View File

@ -67,6 +67,7 @@ type Options struct {
IP string
Overwrite bool
Curve string
HashAlgorithm string
}
// Client holds the state of the croc transfer
@ -149,6 +150,7 @@ type SenderInfo struct {
Ask bool
SendingText bool
NoCompress bool
HashAlgorithm string
}
// New establishes a new connection for transferring files between two instances.
@ -222,8 +224,7 @@ func (c *Client) sendCollectFiles(options TransferOptions) (err error) {
}
log.Debugf("%+v", c.FilesToTransfer[i])
}
c.FilesToTransfer[i].Hash, err = utils.HashFile(fullPath)
c.FilesToTransfer[i].Hash, err = utils.HashFile(fullPath, c.Options.HashAlgorithm)
totalFilesSize += fstats.Size()
if err != nil {
return
@ -769,6 +770,11 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error
}
c.Options.SendingText = senderInfo.SendingText
c.Options.NoCompress = senderInfo.NoCompress
c.Options.HashAlgorithm = senderInfo.HashAlgorithm
if c.Options.HashAlgorithm == "" {
c.Options.HashAlgorithm = "imohash"
}
log.Debugf("using hash algorithm: %s", c.Options.HashAlgorithm)
if c.Options.NoCompress {
log.Debug("disabling compression")
}
@ -1241,7 +1247,7 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) {
var fileHash []byte
if errRecipientFile == nil && recipientFileInfo.Size() == fileInfo.Size {
// the file exists, but is same size, so hash it
fileHash, errHash = utils.HashFile(path.Join(fileInfo.FolderRemote, fileInfo.Name))
fileHash, errHash = utils.HashFile(path.Join(fileInfo.FolderRemote, fileInfo.Name), c.Options.HashAlgorithm)
}
if fileInfo.Size == 0 || fileInfo.Symlink != "" {
err = c.createEmptyFileAndFinish(fileInfo, i)

View File

@ -43,9 +43,8 @@ func GetInput(prompt string) string {
}
// HashFile returns the hash of a file or, in case of a symlink, the
// SHA256 hash of its target
// HashFile returns the hash of a file
func HashFile(fname string) (hash256 []byte, err error) {
// SHA256 hash of its target. Takes an argument to specify the algorithm to use.
func HashFile(fname string, algorithm string) (hash256 []byte, err error) {
var fstats os.FileInfo
fstats, err = os.Lstat(fname)
if err != nil {
@ -59,7 +58,16 @@ func HashFile(fname string) (hash256 []byte, err error) {
if err != nil {
return nil, err
}
return IMOHashFile(fname)
switch algorithm {
case "imohash":
return IMOHashFile(fname)
case "md5":
return MD5HashFile(fname)
case "xxhash":
return XXHashFile(fname)
}
err = fmt.Errorf("unspecified algorithm")
return
}
// MD5HashFile returns MD5 hash