Exemple #1
0
// DecryptPacket decrypts the given packet.
func (i *Ident) DecryptPacket(p *Packet) (transport.Packet, error) {
	var tPacket transport.Packet

	switch p.Type {
	case PacketIDGetNodes:
		tPacket = &GetNodesPacket{}
	case PacketIDSendNodes:
		tPacket = &SendNodesPacket{}
	case PacketIDPingRequest:
		tPacket = &PingRequestPacket{}
	case PacketIDPingResponse:
		tPacket = &PingResponsePacket{}
	default:
		return nil, fmt.Errorf("unknown packet type: %d", p.Type)
	}

	decryptedData, err := i.DecryptBlob(p.Payload, p.SenderPublicKey, p.Nonce)
	if err != nil {
		return nil, err
	}

	err = tPacket.UnmarshalBinary(decryptedData)
	if err != nil {
		return nil, err
	}

	return tPacket, nil
}
Exemple #2
0
// EncryptPacket encrypts the given packet.
func (i *Ident) EncryptPacket(packet transport.Packet, publicKey *[crypto.PublicKeySize]byte) (*Packet, error) {
	base := Packet{}
	base.Type = packet.ID()
	base.SenderPublicKey = i.PublicKey

	payload, err := packet.MarshalBinary()
	if err != nil {
		return nil, err
	}

	encryptedPayload, nonce, err := i.EncryptBlob(payload, publicKey)
	if err != nil {
		return nil, err
	}

	base.Nonce = nonce
	base.Payload = encryptedPayload

	return &base, nil
}