1
0
mirror of https://github.com/schollz/croc.git synced 2024-12-27 15:55:41 +03:00

Send sha256sum and check it Fixes #2

This commit is contained in:
Zack Scholl 2017-10-17 21:28:32 -06:00
parent 0cf680fd66
commit 3ab0f92bd8
3 changed files with 42 additions and 31 deletions

View File

@ -30,7 +30,6 @@ func runClient(connectionType string, codePhrase string) {
uiprogress.Start()
bars = make([]*uiprogress.Bar, numberConnections)
var iv, salt, fileNameToReceive string
for id := 0; id < numberConnections; id++ {
go func(id int) {
defer wg.Done()
@ -61,7 +60,7 @@ func runClient(connectionType string, codePhrase string) {
} else { // this is a receiver
// receive file
logger.Debug("receive file")
fileNameToReceive, iv, salt = receiveFile(id, connection, codePhrase)
fileName, fileIV, fileSalt, fileHash = receiveFile(id, connection, codePhrase)
}
}(id)
@ -69,26 +68,31 @@ func runClient(connectionType string, codePhrase string) {
wg.Wait()
if connectionType == "r" {
catFile(fileNameToReceive)
encrypted, err := ioutil.ReadFile(fileNameToReceive + ".encrypted")
catFile(fileName)
encrypted, err := ioutil.ReadFile(fileName + ".encrypted")
if err != nil {
log.Error(err)
return
}
fmt.Println("\n\ndecrypting...")
decrypted, err := Decrypt(encrypted, codePhrase, salt, iv)
decrypted, err := Decrypt(encrypted, codePhrase, fileSalt, fileIV)
if err != nil {
log.Error(err)
return
}
ioutil.WriteFile(fileNameToReceive, decrypted, 0644)
os.Remove(fileNameToReceive + ".encrypted")
fmt.Println("\nDownloaded " + fileNameToReceive + "!")
ioutil.WriteFile(fileName, decrypted, 0644)
os.Remove(fileName + ".encrypted")
log.Debugf("\n\n\ndownloaded hash: %s", HashBytes(decrypted))
log.Debugf("\n\n\nrelayed hash: %s", fileHash)
if fileHash != HashBytes(decrypted) {
fmt.Printf("\nUh oh! %s is corrupted! Sorry, try again.\n", fileName)
} else {
fmt.Printf("\nDownloaded %s!", fileName)
}
} else {
log.Info("cleaning up")
os.Remove(fileName + ".encrypted")
os.Remove(fileName + ".iv")
os.Remove(fileName + ".salt")
}
}
@ -116,7 +120,7 @@ func catFile(fileNameToReceive string) {
}
func receiveFile(id int, connection net.Conn, codePhrase string) (fileNameToReceive string, iv string, salt string) {
func receiveFile(id int, connection net.Conn, codePhrase string) (fileNameToReceive string, iv string, salt string, hashOfFile string) {
logger := log.WithFields(log.Fields{
"function": "receiveFile #" + strconv.Itoa(id),
})
@ -136,12 +140,17 @@ func receiveFile(id int, connection net.Conn, codePhrase string) (fileNameToRece
ivHex := make([]byte, BUFFERSIZE)
connection.Read(ivHex)
iv = strings.Trim(string(ivHex), ":")
logger.Debugf("iv: %v", iv)
logger.Debugf("iv: %s", iv)
saltHex := make([]byte, BUFFERSIZE)
connection.Read(saltHex)
salt = strings.Trim(string(saltHex), ":")
logger.Debugf("salt: %v", salt)
logger.Debugf("salt: %s", salt)
hashOfFileBytes := make([]byte, BUFFERSIZE)
connection.Read(hashOfFileBytes)
hashOfFile = strings.Trim(string(hashOfFileBytes), ":")
logger.Debugf("hashOfFile: %s", hashOfFile)
os.Remove(fileNameToReceive + "." + strconv.Itoa(id))
newFile, err := os.Create(fileNameToReceive + "." + strconv.Itoa(id))
@ -212,28 +221,25 @@ func sendFile(id int, connection net.Conn, codePhrase string) {
logger.Debugf("fileNameToSend: %v", path.Base(fileName))
}
// send file size
logger.Debugf("sending fileSize: %s", fileSize)
connection.Write([]byte(fileSize))
// send fileName
logger.Debugf("sending fileNameToSend: %s", fileNameToSend)
connection.Write([]byte(fileNameToSend))
// send iv
iv, err := ioutil.ReadFile(fileName + ".iv")
if err != nil {
log.Error(err)
return
}
logger.Debugf("sending iv: %s", iv)
connection.Write([]byte(fillString(string(iv), BUFFERSIZE)))
logger.Debugf("sending iv: %s", fileIV)
connection.Write([]byte(fillString(fileIV, BUFFERSIZE)))
// send salt
salt, err := ioutil.ReadFile(fileName + ".salt")
if err != nil {
log.Error(err)
return
}
logger.Debugf("sending salt: %s", salt)
connection.Write([]byte(fillString(string(salt), BUFFERSIZE)))
logger.Debugf("sending salt: %s", fileSalt)
connection.Write([]byte(fillString(fileSalt, BUFFERSIZE)))
// send sha256sum of file
logger.Debugf("sending sha256sum: %s", fileHash)
connection.Write([]byte(fillString(fileHash, BUFFERSIZE)))
sendBuffer := make([]byte, BUFFERSIZE)

View File

@ -61,6 +61,10 @@ func deriveKey(passphrase string, salt []byte) ([]byte, []byte) {
}
func Hash(data string) string {
sum := sha256.Sum256([]byte(data))
return HashBytes([]byte(data))
}
func HashBytes(data []byte) string {
sum := sha256.Sum256(data)
return fmt.Sprintf("%x", sum)
}

View File

@ -20,6 +20,7 @@ var server, file string
// Global varaibles
var serverAddress, fileName, codePhraseFlag, connectionTypeFlag string
var runAsRelay, debugFlag bool
var fileSalt, fileIV, fileHash string
func main() {
flag.BoolVar(&runAsRelay, "relay", false, "run as relay")
@ -62,14 +63,14 @@ func main() {
log.Fatal(err)
return
}
encrypted, salt, iv := Encrypt(fdata, codePhraseFlag)
var encrypted []byte
encrypted, fileSalt, fileIV = Encrypt(fdata, codePhraseFlag)
if err != nil {
log.Fatal(err)
return
}
ioutil.WriteFile(fileName+".encrypted", encrypted, 0644)
ioutil.WriteFile(fileName+".salt", []byte(salt), 0644)
ioutil.WriteFile(fileName+".iv", []byte(iv), 0644)
fileHash = HashBytes(fdata)
}
log.SetFormatter(&log.TextFormatter{})