-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enveloper verifier
- Loading branch information
Showing
12 changed files
with
286 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package crypto | ||
|
||
type ( | ||
Crypto interface { | ||
Signer | ||
Hasher | ||
Verifier | ||
|
||
GenerateKey() (publicKey, privateKey []byte, err error) | ||
PublicKey(privateKey []byte) ([]byte, error) | ||
} | ||
|
||
Signer interface { | ||
Sign(privateKey, hash []byte) ([]byte, error) | ||
} | ||
|
||
Hasher interface { | ||
Hash([]byte) []byte | ||
} | ||
|
||
Verifier interface { | ||
Verify(publicKey, hash, signature []byte) error | ||
Hasher | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package crypto_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/hyperledger-labs/cckit/extensions/envelope/crypto" | ||
) | ||
|
||
const ( | ||
Ed25519PublicKeyLen = 32 | ||
Ed25519PrivateKeyLen = 64 | ||
Ed25519SignatureLen = 64 | ||
) | ||
|
||
func TestCrypto(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Crypto suite") | ||
} | ||
|
||
var _ = Describe(`Ed25519 crypto`, func() { | ||
|
||
ed25519 := crypto.NewEd25519() | ||
|
||
It("Allow to create keys", func() { | ||
publicKey, privateKey, err := ed25519.GenerateKey() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(len(publicKey)).To(Equal(Ed25519PublicKeyLen)) | ||
Expect(len(privateKey)).To(Equal(Ed25519PrivateKeyLen)) | ||
}) | ||
|
||
It("Allow to create signature", func() { | ||
_, privateKey, _ := ed25519.GenerateKey() | ||
sig, err := ed25519.Sign(privateKey, []byte(`anything`)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(len(sig)).To(Equal(Ed25519SignatureLen)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package crypto | ||
|
||
import ( | ||
"crypto/ed25519" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
func NewEd25519() *Ed25519 { | ||
return &Ed25519{} | ||
} | ||
|
||
type Ed25519 struct{} | ||
|
||
func (ed *Ed25519) GenerateKey() (publicKey, privateKey []byte, err error) { | ||
publicKey, privateKey, err = ed25519.GenerateKey(rand.Reader) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return publicKey, privateKey, nil | ||
} | ||
|
||
func (ed *Ed25519) Sign(privateKey, hash []byte) (signature []byte, err error) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("sign: %v", r) | ||
} | ||
}() | ||
return ed25519.Sign(privateKey, hash), nil | ||
} | ||
|
||
func (ed *Ed25519) Hash(msg []byte) []byte { | ||
h := sha256.Sum256(msg) | ||
return h[:] | ||
} | ||
|
||
func (ed *Ed25519) Verify(publicKey, hash, signature []byte) error { | ||
if !ed25519.Verify(publicKey, hash, signature) { | ||
return errors.New(`invalid signature`) | ||
} | ||
return nil | ||
} | ||
|
||
func (ed *Ed25519) PublicKey(privateKey []byte) ([]byte, error) { | ||
return ed25519.PrivateKey(privateKey).Public().(ed25519.PublicKey), nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ message Envelope { | |
string channel = 7; | ||
string chaincode = 8; | ||
string method = 9; | ||
string signature_alg = 10; | ||
} |
18 changes: 15 additions & 3 deletions
18
extensions/envelope/testdata/cc_envelope.go → extensions/envelope/envelope_cc_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.