1
0
mirror of https://github.com/schollz/croc.git synced 2024-11-23 23:54:17 +03:00

add flag for skipping encryption

This commit is contained in:
Zack Scholl 2017-10-17 22:23:31 -06:00
parent adb3f59a51
commit 86b12a3770
2 changed files with 27 additions and 19 deletions

View File

@ -1,9 +1,12 @@
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"fmt"
mathrand "math/rand"
"strings"
@ -26,27 +29,31 @@ func GetRandomName() string {
}
func Encrypt(plaintext []byte, passphrase string) ([]byte, string, string) {
return plaintext, "salt", "iv"
// key, salt := deriveKey(passphrase, nil)
// iv := make([]byte, 12)
// // http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
// // Section 8.2
// rand.Read(iv)
// b, _ := aes.NewCipher(key)
// aesgcm, _ := cipher.NewGCM(b)
// data := aesgcm.Seal(nil, iv, plaintext, nil)
// return data, hex.EncodeToString(salt), hex.EncodeToString(iv)
if dontEncrypt {
return plaintext, "salt", "iv"
}
key, salt := deriveKey(passphrase, nil)
iv := make([]byte, 12)
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
// Section 8.2
rand.Read(iv)
b, _ := aes.NewCipher(key)
aesgcm, _ := cipher.NewGCM(b)
data := aesgcm.Seal(nil, iv, plaintext, nil)
return data, hex.EncodeToString(salt), hex.EncodeToString(iv)
}
func Decrypt(data []byte, passphrase string, salt string, iv string) (plaintext []byte, err error) {
return data, nil
// saltBytes, _ := hex.DecodeString(salt)
// ivBytes, _ := hex.DecodeString(iv)
// key, _ := deriveKey(passphrase, saltBytes)
// b, _ := aes.NewCipher(key)
// aesgcm, _ := cipher.NewGCM(b)
// plaintext, err = aesgcm.Open(nil, ivBytes, data, nil)
// return
if dontEncrypt {
return data, nil
}
saltBytes, _ := hex.DecodeString(salt)
ivBytes, _ := hex.DecodeString(iv)
key, _ := deriveKey(passphrase, saltBytes)
b, _ := aes.NewCipher(key)
aesgcm, _ := cipher.NewGCM(b)
plaintext, err = aesgcm.Open(nil, ivBytes, data, nil)
return
}
func deriveKey(passphrase string, salt []byte) ([]byte, []byte) {

View File

@ -19,7 +19,7 @@ var server, file string
// Global varaibles
var serverAddress, fileName, codePhraseFlag, connectionTypeFlag string
var runAsRelay, debugFlag bool
var runAsRelay, debugFlag, dontEncrypt bool
var fileSalt, fileIV, fileHash string
var fileBytes []byte
@ -29,6 +29,7 @@ func main() {
flag.StringVar(&serverAddress, "server", "cowyo.com", "address of relay server")
flag.StringVar(&fileName, "send", "", "file to send")
flag.StringVar(&codePhraseFlag, "code", "", "use your own code phrase")
flag.BoolVar(&dontEncrypt, "no-encrypt", false, "turn off encryption")
flag.Parse()
// Check build flags too, which take precedent
if server != "" {