0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

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

View file

@ -67,6 +67,7 @@ type Options struct {
IP string IP string
Overwrite bool Overwrite bool
Curve string Curve string
HashAlgorithm string
} }
// Client holds the state of the croc transfer // Client holds the state of the croc transfer
@ -149,6 +150,7 @@ type SenderInfo struct {
Ask bool Ask bool
SendingText bool SendingText bool
NoCompress bool NoCompress bool
HashAlgorithm string
} }
// New establishes a new connection for transferring files between two instances. // 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]) log.Debugf("%+v", c.FilesToTransfer[i])
} }
c.FilesToTransfer[i].Hash, err = utils.HashFile(fullPath, c.Options.HashAlgorithm)
c.FilesToTransfer[i].Hash, err = utils.HashFile(fullPath)
totalFilesSize += fstats.Size() totalFilesSize += fstats.Size()
if err != nil { if err != nil {
return return
@ -769,6 +770,11 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error
} }
c.Options.SendingText = senderInfo.SendingText c.Options.SendingText = senderInfo.SendingText
c.Options.NoCompress = senderInfo.NoCompress 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 { if c.Options.NoCompress {
log.Debug("disabling compression") log.Debug("disabling compression")
} }
@ -1241,7 +1247,7 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) {
var fileHash []byte var fileHash []byte
if errRecipientFile == nil && recipientFileInfo.Size() == fileInfo.Size { if errRecipientFile == nil && recipientFileInfo.Size() == fileInfo.Size {
// the file exists, but is same size, so hash it // 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 != "" { if fileInfo.Size == 0 || fileInfo.Symlink != "" {
err = c.createEmptyFileAndFinish(fileInfo, i) 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 // HashFile returns the hash of a file or, in case of a symlink, the
// SHA256 hash of its target // SHA256 hash of its target. Takes an argument to specify the algorithm to use.
// HashFile returns the hash of a file func HashFile(fname string, algorithm string) (hash256 []byte, err error) {
func HashFile(fname string) (hash256 []byte, err error) {
var fstats os.FileInfo var fstats os.FileInfo
fstats, err = os.Lstat(fname) fstats, err = os.Lstat(fname)
if err != nil { if err != nil {
@ -59,7 +58,16 @@ func HashFile(fname string) (hash256 []byte, err error) {
if err != nil { if err != nil {
return nil, err 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 // MD5HashFile returns MD5 hash