mirror of
https://github.com/rkonfj/peerguard.git
synced 2024-08-11 11:00:25 +08:00
p2p: add SharedKey func
This commit is contained in:
parent
99e2fba7e2
commit
571871e35a
@ -240,6 +240,14 @@ func (c *PeerPacketConn) runControlEventLoop(wsConn *disco.WSConn, udpConn *disc
|
||||
}
|
||||
}
|
||||
|
||||
// SharedKey get the key shared with the peer
|
||||
func (c *PeerPacketConn) SharedKey(peerID peer.ID) ([]byte, error) {
|
||||
if c.cfg.SymmAlgo == nil {
|
||||
return nil, errors.New("get shared key from plain conn")
|
||||
}
|
||||
return c.cfg.SymmAlgo.SecretKey()(peerID.String())
|
||||
}
|
||||
|
||||
// ListenPacket listen the p2p network for read/write packets
|
||||
func ListenPacket(peermap *peermap.Peermap, opts ...Option) (*PeerPacketConn, error) {
|
||||
id := make([]byte, 16)
|
||||
|
@ -124,6 +124,10 @@ func (s *AESCBC) Decrypt(b []byte, pubKey string) ([]byte, error) {
|
||||
return PKCS7UnPadding(plainBytes)
|
||||
}
|
||||
|
||||
func (s *AESCBC) SecretKey() secure.ProvideSecretKey {
|
||||
return s.provideSecretKey
|
||||
}
|
||||
|
||||
func (s *AESCBC) ensureChiperBlock(pubKey string) (cipher.Block, error) {
|
||||
s.mut.RLock()
|
||||
block, ok := s.cipher.Get(pubKey)
|
||||
|
@ -62,6 +62,10 @@ func (s *Chacha20Poly1305) Decrypt(data []byte, pubKey string) ([]byte, error) {
|
||||
return plain, nil
|
||||
}
|
||||
|
||||
func (s *Chacha20Poly1305) SecretKey() secure.ProvideSecretKey {
|
||||
return s.provideSecretKey
|
||||
}
|
||||
|
||||
func (s *Chacha20Poly1305) ensureChiperAEAD(pubKey string) (cipher.AEAD, error) {
|
||||
s.mut.RLock()
|
||||
aead, ok := s.cipher.Get(pubKey)
|
||||
|
@ -5,4 +5,5 @@ type ProvideSecretKey func(pubKey string) ([]byte, error)
|
||||
type SymmAlgo interface {
|
||||
Encrypt(data []byte, pubKey string) ([]byte, error)
|
||||
Decrypt(data []byte, pubKey string) ([]byte, error)
|
||||
SecretKey() ProvideSecretKey
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user