mirror of https://github.com/schollz/croc.git
encryption faster with simpler marshaling
This commit is contained in:
parent
8bfbe36dd9
commit
7147980eac
|
@ -5,6 +5,9 @@ import (
|
|||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/pbkdf2"
|
||||
)
|
||||
|
@ -16,6 +19,29 @@ type Encryption struct {
|
|||
IV []byte `json:"i"`
|
||||
}
|
||||
|
||||
func (e Encryption) Bytes() []byte {
|
||||
return []byte(base64.StdEncoding.EncodeToString(e.Encrypted) + "-" + base64.StdEncoding.EncodeToString(e.Salt) + "-" + base64.StdEncoding.EncodeToString(e.IV))
|
||||
}
|
||||
|
||||
func FromBytes(b []byte) (enc Encryption, err error) {
|
||||
enc = Encryption{}
|
||||
items := strings.Split(string(b), "-")
|
||||
if len(items) != 3 {
|
||||
err = errors.New("not valid")
|
||||
return
|
||||
}
|
||||
enc.Encrypted, err = base64.StdEncoding.DecodeString(items[0])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
enc.Salt, err = base64.StdEncoding.DecodeString(items[1])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
enc.IV, err = base64.StdEncoding.DecodeString(items[2])
|
||||
return
|
||||
}
|
||||
|
||||
// Encrypt will generate an encryption
|
||||
func Encrypt(plaintext []byte, passphrase []byte, dontencrypt ...bool) Encryption {
|
||||
if len(dontencrypt) > 0 && dontencrypt[0] {
|
||||
|
|
|
@ -133,8 +133,7 @@ func receive(forceSend int, serverAddress, serverTCP string, isLocal bool, c *we
|
|||
// unmarshal the file info
|
||||
log.Debugf("[%d] recieve file info", step)
|
||||
// do decryption on the file stats
|
||||
var enc crypt.Encryption
|
||||
err = json.Unmarshal(message, &enc)
|
||||
enc, err := crypt.FromBytes(message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -193,12 +193,8 @@ func send(forceSend int, serverAddress, serverTCP string, isLocal bool, c *webso
|
|||
}
|
||||
// encrypt the file meta data
|
||||
enc := crypt.Encrypt(fstatsBytes, sessionKey)
|
||||
encBytes, err := json.Marshal(enc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// send the file meta data
|
||||
c.WriteMessage(websocket.BinaryMessage, encBytes)
|
||||
c.WriteMessage(websocket.BinaryMessage, enc.Bytes())
|
||||
case 4:
|
||||
spin.Stop()
|
||||
|
||||
|
|
Loading…
Reference in New Issue