func TestSignature(t *testing.T) {
	pub, priv, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		t.Error(err)
	}
	t.Logf("priv, pub - %x, %x", *priv, *pub)

	testData := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}

	sig := ed25519.Sign(priv, testData)

	ok := ed25519.Verify(pub, testData, sig)

	if ok == false {
		t.Error("Signature could not be verified")
	}

	pub2, err := primitives.PrivateKeyToPublicKey(priv[:])
	if err != nil {
		t.Error(err)
	}

	t.Logf("pub1 - %x", pub)
	t.Logf("pub2 - %x", pub2)

	if primitives.AreBytesEqual(pub[:], pub2[:]) == false {
		t.Error("Public keys are not equal")
	}
}
Beispiel #2
0
func VerifySignature(data, publicKey, signature []byte) error {
	pub := [32]byte{}
	sig := [64]byte{}

	if len(publicKey) == 32 {
		copy(pub[:], publicKey[:])
	} else {
		return fmt.Errorf("Invalid public key length")
	}

	if len(signature) == 64 {
		copy(sig[:], signature[:])
	} else {
		return fmt.Errorf("Invalid signature length")
	}

	valid := ed25519.Verify(&pub, data, &sig)
	if valid == false {
		return fmt.Errorf("Invalid signature")
	}
	return nil
}