Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: raw signer #23

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions principal/ed25519/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ func (s Ed25519Signer) Encode() []byte {
return s
}

func (s Ed25519Signer) Sign(msg []byte) signature.SignatureView {
func (s Ed25519Signer) Raw() []byte {
pk := make(ed25519.PrivateKey, ed25519.PrivateKeySize)
copy(pk[0:ed25519.PublicKeySize], s[privateTagSize:pubKeyOffset])
copy(pk[ed25519.PrivateKeySize-ed25519.PublicKeySize:ed25519.PrivateKeySize], s[pubKeyOffset+publicTagSize:pubKeyOffset+publicTagSize+keySize])
return signature.NewSignatureView(signature.NewSignature(signature.EdDSA, ed25519.Sign(pk, msg)))
return pk
}

func (s Ed25519Signer) Sign(msg []byte) signature.SignatureView {
return signature.NewSignatureView(signature.NewSignature(signature.EdDSA, ed25519.Sign(s.Raw(), msg)))
}
14 changes: 14 additions & 0 deletions principal/ed25519/signer/signer_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package signer

import (
"crypto/ed25519"
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func TestGenerateEncodeDecode(t *testing.T) {
Expand Down Expand Up @@ -66,3 +69,14 @@ func TestVerify(t *testing.T) {
t.Fatalf("verify failed")
}
}

func TestSignerRaw(t *testing.T) {
s, err := Generate()
require.NoError(t, err)

msg := []byte{1, 2, 3}
raw := s.Raw()
sig := ed25519.Sign(raw, msg)

require.Equal(t, s.Sign(msg).Raw(), sig)
}
2 changes: 2 additions & 0 deletions principal/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type Signer interface {
Code() uint64
Verifier() Verifier
Encode() []byte
// Raw encodes the bytes of the private key without multiformats tags.
Raw() []byte
}

// Verifier is the principal that issued a UCAN. In usually represents remote
Expand Down
5 changes: 5 additions & 0 deletions principal/rsa/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ func (s rsasigner) Encode() []byte {
return s.bytes
}

func (s rsasigner) Raw() []byte {
b, _ := multiformat.UntagWith(Code, s.bytes, 0)
return b
}

func (s rsasigner) Sign(msg []byte) signature.SignatureView {
hash := sha256.New()
hash.Write(msg)
Expand Down
4 changes: 4 additions & 0 deletions principal/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (w wrapsgn) Encode() []byte {
return w.key.Encode()
}

func (w wrapsgn) Raw() []byte {
return w.key.Raw()
}

func (w wrapsgn) Sign(msg []byte) signature.SignatureView {
return w.key.Sign(msg)
}
Expand Down
Loading