mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2025-02-21 08:20:07 +08:00
use buffer pool for envelope signatures
This commit is contained in:
parent
3724a31efe
commit
be36d83110
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
|
"github.com/libp2p/go-buffer-pool"
|
||||||
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
|
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -42,7 +43,10 @@ func MakeEnvelope(privateKey PrivKey, domain string, payloadType []byte, payload
|
|||||||
if len(domain) == 0 {
|
if len(domain) == 0 {
|
||||||
return nil, errEmptyDomain
|
return nil, errEmptyDomain
|
||||||
}
|
}
|
||||||
toSign := makeSigBuffer(domain, payloadType, payload)
|
toSign, err := makeSigBuffer(domain, payloadType, payload)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
sig, err := privateKey.Sign(toSign)
|
sig, err := privateKey.Sign(toSign)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -129,7 +133,10 @@ func (e *SignedEnvelope) Equals(other *SignedEnvelope) bool {
|
|||||||
// validate returns true if the envelope signature is valid for the given 'domain',
|
// validate returns true if the envelope signature is valid for the given 'domain',
|
||||||
// or false if it is invalid. May return an error if signature validation fails.
|
// or false if it is invalid. May return an error if signature validation fails.
|
||||||
func (e *SignedEnvelope) validate(domain string) error {
|
func (e *SignedEnvelope) validate(domain string) error {
|
||||||
toVerify := makeSigBuffer(domain, e.payloadType, e.payload)
|
toVerify, err := makeSigBuffer(domain, e.payloadType, e.payload)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
valid, err := e.publicKey.Verify(toVerify, e.signature)
|
valid, err := e.publicKey.Verify(toVerify, e.signature)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -141,16 +148,36 @@ func (e *SignedEnvelope) validate(domain string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// makeSigBuffer is a helper function that prepares a buffer to sign or verify.
|
// makeSigBuffer is a helper function that prepares a buffer to sign or verify.
|
||||||
func makeSigBuffer(domain string, typeHint []byte, content []byte) []byte {
|
func makeSigBuffer(domain string, payloadType []byte, payload []byte) ([]byte, error) {
|
||||||
b := bytes.Buffer{}
|
|
||||||
domainBytes := []byte(domain)
|
domainBytes := []byte(domain)
|
||||||
b.Write(encodedSize(domainBytes))
|
fields := [][]byte{domainBytes, payloadType, payload}
|
||||||
b.Write(domainBytes)
|
|
||||||
b.Write(encodedSize(typeHint))
|
const lengthPrefixSize = 8
|
||||||
b.Write(typeHint)
|
size := 0
|
||||||
b.Write(encodedSize(content))
|
for _, f := range fields {
|
||||||
b.Write(content)
|
size += len(f) + lengthPrefixSize
|
||||||
return b.Bytes()
|
}
|
||||||
|
|
||||||
|
b := pool.NewBuffer(nil)
|
||||||
|
b.Grow(size)
|
||||||
|
|
||||||
|
for _, f := range fields {
|
||||||
|
err := writeField(b, f)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeField(b *pool.Buffer, f []byte) error {
|
||||||
|
_, err := b.Write(encodedSize(f))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = b.Write(f)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodedSize(content []byte) []byte {
|
func encodedSize(content []byte) []byte {
|
||||||
|
1
go.mod
1
go.mod
@ -7,6 +7,7 @@ require (
|
|||||||
github.com/golang/protobuf v1.3.1
|
github.com/golang/protobuf v1.3.1
|
||||||
github.com/ipfs/go-cid v0.0.4
|
github.com/ipfs/go-cid v0.0.4
|
||||||
github.com/jbenet/goprocess v0.1.3
|
github.com/jbenet/goprocess v0.1.3
|
||||||
|
github.com/libp2p/go-buffer-pool v0.0.1
|
||||||
github.com/libp2p/go-flow-metrics v0.0.3
|
github.com/libp2p/go-flow-metrics v0.0.3
|
||||||
github.com/libp2p/go-openssl v0.0.4
|
github.com/libp2p/go-openssl v0.0.4
|
||||||
github.com/minio/sha256-simd v0.1.1
|
github.com/minio/sha256-simd v0.1.1
|
||||||
|
Loading…
Reference in New Issue
Block a user