mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 09:34:03 +08:00
refactor(identify) extract hello
This commit is contained in:
@ -31,22 +31,20 @@ var SupportedHashes = "SHA256,SHA512,SHA1"
|
|||||||
// ErrUnsupportedKeyType is returned when a private key cast/type switch fails.
|
// ErrUnsupportedKeyType is returned when a private key cast/type switch fails.
|
||||||
var ErrUnsupportedKeyType = errors.New("unsupported key type")
|
var ErrUnsupportedKeyType = errors.New("unsupported key type")
|
||||||
|
|
||||||
// Performs initial communication with this peer to share node ID's and
|
func genRandHello(self *peer.Peer) (*Hello, error) {
|
||||||
// initiate communication. (secureIn, secureOut, error)
|
hello := new(Hello)
|
||||||
func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-chan []byte, chan<- []byte, error) {
|
|
||||||
// Generate and send Hello packet.
|
// Generate and send Hello packet.
|
||||||
// Hello = (rand, PublicKey, Supported)
|
// Hello = (rand, PublicKey, Supported)
|
||||||
nonce := make([]byte, 16)
|
nonce := make([]byte, 16)
|
||||||
_, err := rand.Read(nonce)
|
_, err := rand.Read(nonce)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
hello := new(Hello)
|
|
||||||
|
|
||||||
myPubKey, err := self.PubKey.Bytes()
|
myPubKey, err := self.PubKey.Bytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
hello.Rand = nonce
|
hello.Rand = nonce
|
||||||
@ -54,8 +52,18 @@ func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-
|
|||||||
hello.Exchanges = &SupportedExchanges
|
hello.Exchanges = &SupportedExchanges
|
||||||
hello.Ciphers = &SupportedCiphers
|
hello.Ciphers = &SupportedCiphers
|
||||||
hello.Hashes = &SupportedHashes
|
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 {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@ -160,6 +168,11 @@ func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
myPubKey, err := self.PubKey.Bytes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
cmp := bytes.Compare(myPubKey, helloResp.GetPubkey())
|
cmp := bytes.Compare(myPubKey, helloResp.GetPubkey())
|
||||||
mIV, tIV, mCKey, tCKey, mMKey, tMKey := ci.KeyStretcher(cmp, cipherType, hashType, secret)
|
mIV, tIV, mCKey, tCKey, mMKey, tMKey := ci.KeyStretcher(cmp, cipherType, hashType, secret)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user