mirror of https://github.com/schollz/croc.git
Send sha256sum and check it Fixes #2
This commit is contained in:
parent
0cf680fd66
commit
3ab0f92bd8
60
connect.go
60
connect.go
|
@ -30,7 +30,6 @@ func runClient(connectionType string, codePhrase string) {
|
||||||
|
|
||||||
uiprogress.Start()
|
uiprogress.Start()
|
||||||
bars = make([]*uiprogress.Bar, numberConnections)
|
bars = make([]*uiprogress.Bar, numberConnections)
|
||||||
var iv, salt, fileNameToReceive string
|
|
||||||
for id := 0; id < numberConnections; id++ {
|
for id := 0; id < numberConnections; id++ {
|
||||||
go func(id int) {
|
go func(id int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
@ -61,7 +60,7 @@ func runClient(connectionType string, codePhrase string) {
|
||||||
} else { // this is a receiver
|
} else { // this is a receiver
|
||||||
// receive file
|
// receive file
|
||||||
logger.Debug("receive file")
|
logger.Debug("receive file")
|
||||||
fileNameToReceive, iv, salt = receiveFile(id, connection, codePhrase)
|
fileName, fileIV, fileSalt, fileHash = receiveFile(id, connection, codePhrase)
|
||||||
}
|
}
|
||||||
|
|
||||||
}(id)
|
}(id)
|
||||||
|
@ -69,26 +68,31 @@ func runClient(connectionType string, codePhrase string) {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
if connectionType == "r" {
|
if connectionType == "r" {
|
||||||
catFile(fileNameToReceive)
|
catFile(fileName)
|
||||||
encrypted, err := ioutil.ReadFile(fileNameToReceive + ".encrypted")
|
encrypted, err := ioutil.ReadFile(fileName + ".encrypted")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("\n\ndecrypting...")
|
fmt.Println("\n\ndecrypting...")
|
||||||
decrypted, err := Decrypt(encrypted, codePhrase, salt, iv)
|
decrypted, err := Decrypt(encrypted, codePhrase, fileSalt, fileIV)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ioutil.WriteFile(fileNameToReceive, decrypted, 0644)
|
ioutil.WriteFile(fileName, decrypted, 0644)
|
||||||
os.Remove(fileNameToReceive + ".encrypted")
|
os.Remove(fileName + ".encrypted")
|
||||||
fmt.Println("\nDownloaded " + fileNameToReceive + "!")
|
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 {
|
} else {
|
||||||
log.Info("cleaning up")
|
log.Info("cleaning up")
|
||||||
os.Remove(fileName + ".encrypted")
|
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{
|
logger := log.WithFields(log.Fields{
|
||||||
"function": "receiveFile #" + strconv.Itoa(id),
|
"function": "receiveFile #" + strconv.Itoa(id),
|
||||||
})
|
})
|
||||||
|
@ -136,12 +140,17 @@ func receiveFile(id int, connection net.Conn, codePhrase string) (fileNameToRece
|
||||||
ivHex := make([]byte, BUFFERSIZE)
|
ivHex := make([]byte, BUFFERSIZE)
|
||||||
connection.Read(ivHex)
|
connection.Read(ivHex)
|
||||||
iv = strings.Trim(string(ivHex), ":")
|
iv = strings.Trim(string(ivHex), ":")
|
||||||
logger.Debugf("iv: %v", iv)
|
logger.Debugf("iv: %s", iv)
|
||||||
|
|
||||||
saltHex := make([]byte, BUFFERSIZE)
|
saltHex := make([]byte, BUFFERSIZE)
|
||||||
connection.Read(saltHex)
|
connection.Read(saltHex)
|
||||||
salt = strings.Trim(string(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))
|
os.Remove(fileNameToReceive + "." + strconv.Itoa(id))
|
||||||
newFile, err := os.Create(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))
|
logger.Debugf("fileNameToSend: %v", path.Base(fileName))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// send file size
|
||||||
logger.Debugf("sending fileSize: %s", fileSize)
|
logger.Debugf("sending fileSize: %s", fileSize)
|
||||||
connection.Write([]byte(fileSize))
|
connection.Write([]byte(fileSize))
|
||||||
|
|
||||||
|
// send fileName
|
||||||
logger.Debugf("sending fileNameToSend: %s", fileNameToSend)
|
logger.Debugf("sending fileNameToSend: %s", fileNameToSend)
|
||||||
connection.Write([]byte(fileNameToSend))
|
connection.Write([]byte(fileNameToSend))
|
||||||
|
|
||||||
// send iv
|
// send iv
|
||||||
iv, err := ioutil.ReadFile(fileName + ".iv")
|
logger.Debugf("sending iv: %s", fileIV)
|
||||||
if err != nil {
|
connection.Write([]byte(fillString(fileIV, BUFFERSIZE)))
|
||||||
log.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logger.Debugf("sending iv: %s", iv)
|
|
||||||
connection.Write([]byte(fillString(string(iv), BUFFERSIZE)))
|
|
||||||
|
|
||||||
// send salt
|
// send salt
|
||||||
salt, err := ioutil.ReadFile(fileName + ".salt")
|
logger.Debugf("sending salt: %s", fileSalt)
|
||||||
if err != nil {
|
connection.Write([]byte(fillString(fileSalt, BUFFERSIZE)))
|
||||||
log.Error(err)
|
|
||||||
return
|
// send sha256sum of file
|
||||||
}
|
logger.Debugf("sending sha256sum: %s", fileHash)
|
||||||
logger.Debugf("sending salt: %s", salt)
|
connection.Write([]byte(fillString(fileHash, BUFFERSIZE)))
|
||||||
connection.Write([]byte(fillString(string(salt), BUFFERSIZE)))
|
|
||||||
|
|
||||||
sendBuffer := make([]byte, BUFFERSIZE)
|
sendBuffer := make([]byte, BUFFERSIZE)
|
||||||
|
|
||||||
|
|
|
@ -61,6 +61,10 @@ func deriveKey(passphrase string, salt []byte) ([]byte, []byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Hash(data string) string {
|
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)
|
return fmt.Sprintf("%x", sum)
|
||||||
}
|
}
|
||||||
|
|
7
main.go
7
main.go
|
@ -20,6 +20,7 @@ var server, file string
|
||||||
// Global varaibles
|
// Global varaibles
|
||||||
var serverAddress, fileName, codePhraseFlag, connectionTypeFlag string
|
var serverAddress, fileName, codePhraseFlag, connectionTypeFlag string
|
||||||
var runAsRelay, debugFlag bool
|
var runAsRelay, debugFlag bool
|
||||||
|
var fileSalt, fileIV, fileHash string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.BoolVar(&runAsRelay, "relay", false, "run as relay")
|
flag.BoolVar(&runAsRelay, "relay", false, "run as relay")
|
||||||
|
@ -62,14 +63,14 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
encrypted, salt, iv := Encrypt(fdata, codePhraseFlag)
|
var encrypted []byte
|
||||||
|
encrypted, fileSalt, fileIV = Encrypt(fdata, codePhraseFlag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ioutil.WriteFile(fileName+".encrypted", encrypted, 0644)
|
ioutil.WriteFile(fileName+".encrypted", encrypted, 0644)
|
||||||
ioutil.WriteFile(fileName+".salt", []byte(salt), 0644)
|
fileHash = HashBytes(fdata)
|
||||||
ioutil.WriteFile(fileName+".iv", []byte(iv), 0644)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SetFormatter(&log.TextFormatter{})
|
log.SetFormatter(&log.TextFormatter{})
|
||||||
|
|
Loading…
Reference in New Issue