github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/yuubinsya/crypto/signer.go (about)

     1  package crypto
     2  
     3  import (
     4  	"crypto"
     5  	"crypto/ed25519"
     6  	"io"
     7  
     8  	"github.com/Asutorufa/yuhaiin/pkg/net/proxy/yuubinsya/types"
     9  	"golang.org/x/crypto/hkdf"
    10  )
    11  
    12  type Ed25519 struct {
    13  	privatekey ed25519.PrivateKey
    14  }
    15  
    16  func NewEd25519(hash types.Hash, key []byte) types.Signer {
    17  	r := hkdf.New(hash.New, key, make([]byte, hash.Size()), []byte("ed25519-signature"))
    18  	seed := make([]byte, ed25519.SeedSize)
    19  	_, _ = io.ReadFull(r, seed)
    20  
    21  	return &Ed25519{ed25519.NewKeyFromSeed(seed)}
    22  }
    23  
    24  func (Ed25519) SignatureSize() int { return ed25519.SignatureSize }
    25  func (e Ed25519) Verify(message, sig []byte) bool {
    26  	return ed25519.Verify(e.privatekey.Public().(ed25519.PublicKey), message, sig)
    27  }
    28  func (e Ed25519) Sign(rand io.Reader, digest []byte) (signature []byte, err error) {
    29  	return e.privatekey.Sign(rand, digest, crypto.Hash(0))
    30  }