From c236f8cb4fe7df6760bcd2d82382382c715ecda5 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Sat, 13 Sep 2014 01:29:19 -0700 Subject: [PATCH] refactor(identify) extract hello --- identify/identify.go | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/identify/identify.go b/identify/identify.go index b6b896780..bb11686e2 100644 --- a/identify/identify.go +++ b/identify/identify.go @@ -31,22 +31,20 @@ var SupportedHashes = "SHA256,SHA512,SHA1" // ErrUnsupportedKeyType is returned when a private key cast/type switch fails. var ErrUnsupportedKeyType = errors.New("unsupported key type") -// Performs initial communication with this peer to share node ID's and -// initiate communication. (secureIn, secureOut, error) -func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-chan []byte, chan<- []byte, error) { +func genRandHello(self *peer.Peer) (*Hello, error) { + hello := new(Hello) + // Generate and send Hello packet. // Hello = (rand, PublicKey, Supported) nonce := make([]byte, 16) _, err := rand.Read(nonce) if err != nil { - return nil, nil, err + return nil, err } - hello := new(Hello) - myPubKey, err := self.PubKey.Bytes() if err != nil { - return nil, nil, err + return nil, err } hello.Rand = nonce @@ -54,8 +52,18 @@ func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<- hello.Exchanges = &SupportedExchanges hello.Ciphers = &SupportedCiphers hello.Hashes = &SupportedHashes + return hello, nil +} - encoded, err := proto.Marshal(hello) +// Performs initial communication with this peer to share node ID's and +// initiate communication. (secureIn, secureOut, error) +func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-chan []byte, chan<- []byte, error) { + h, err := genRandHello(self) + if err != nil { + return nil, nil, err + } + + encoded, err := proto.Marshal(h) if err != nil { return nil, nil, err } @@ -160,6 +168,11 @@ func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<- return nil, nil, err } + myPubKey, err := self.PubKey.Bytes() + if err != nil { + return nil, nil, err + } + cmp := bytes.Compare(myPubKey, helloResp.GetPubkey()) mIV, tIV, mCKey, tCKey, mMKey, tMKey := ci.KeyStretcher(cmp, cipherType, hashType, secret)