diff --git a/src/cli/cli.go b/src/cli/cli.go index 5b2d9a9..bf8a77f 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -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 = "" diff --git a/src/croc/croc.go b/src/croc/croc.go index 5a6e6ae..b95671d 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -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) diff --git a/src/utils/utils.go b/src/utils/utils.go index f7b2e24..f7a4166 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -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