@cryptotaxi247 / kubo / commits / c236f8cb4

refactor(identify) extract hello

Brian Tiger Chow committed Sep 13, 2014 at 01:29 UTC c236f8cb4fe7df6760bcd2d82382382c715ecda5
1 file changed +21 -8
identify/identify.go
+21 -8
@@ -31,22 +31,20 @@ var SupportedHashes = "SHA256,SHA512,SHA1"
31 // ErrUnsupportedKeyType is returned when a private key cast/type switch fails.
32 var ErrUnsupportedKeyType = errors.New("unsupported key type")
33
34 -// Performs initial communication with this peer to share node ID's and
35 -// initiate communication. (secureIn, secureOut, error)
36 -func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-chan []byte, chan<- []byte, error) {
34 +func genRandHello(self *peer.Peer) (*Hello, error) {
35 + hello := new(Hello)
36 +
37 // Generate and send Hello packet.
38 // Hello = (rand, PublicKey, Supported)
39 nonce := make([]byte, 16)
40 _, err := rand.Read(nonce)
41 if err != nil {
42 - return nil, nil, err
42 + return nil, err
43 }
44
45 - hello := new(Hello)
46 -
45 myPubKey, err := self.PubKey.Bytes()
46 if err != nil {
49 - return nil, nil, err
47 + return nil, err
48 }
49
50 hello.Rand = nonce
@@ -54,8 +52,18 @@ func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-
52 hello.Exchanges = &SupportedExchanges
53 hello.Ciphers = &SupportedCiphers
54 hello.Hashes = &SupportedHashes
55 + return hello, nil
56 +}
57
58 - encoded, err := proto.Marshal(hello)
58 +// Performs initial communication with this peer to share node ID's and
59 +// initiate communication. (secureIn, secureOut, error)
60 +func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-chan []byte, chan<- []byte, error) {
61 + h, err := genRandHello(self)
62 + if err != nil {
63 + return nil, nil, err
64 + }
65 +
66 + encoded, err := proto.Marshal(h)
67 if err != nil {
68 return nil, nil, err
69 }
@@ -160,6 +168,11 @@ func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-
168 return nil, nil, err
169 }
170
171 + myPubKey, err := self.PubKey.Bytes()
172 + if err != nil {
173 + return nil, nil, err
174 + }
175 +
176 cmp := bytes.Compare(myPubKey, helloResp.GetPubkey())
177 mIV, tIV, mCKey, tCKey, mMKey, tMKey := ci.KeyStretcher(cmp, cipherType, hashType, secret)
178