Skip to content

Instantly share code, notes, and snippets.

@Grrrben
Created November 1, 2018 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grrrben/92138ee80ed66ae4bfd5a3b3956b7891 to your computer and use it in GitHub Desktop.
Save Grrrben/92138ee80ed66ae4bfd5a3b3956b7891 to your computer and use it in GitHub Desktop.
func TestSignVerify(t *testing.T) {
henk, _ := NewRsaIdentity()
// A public message from Henk.
// note that the message is a byte array, not just a string.
msg := []byte("Wilders doet tenminste iets tegen de politiek.")
// Henk signs the message with his private key. This will show the recipient
// proof that this message is indeed from Henk
sig, err := henk.Sign(msg)
// now, if the message msg is public, anyone can read it.
// the signature sig however, proves this message is from Henk.
ingrid, _ := NewRsaIdentity()
hans, _ := NewRsaIdentity()
err = ingrid.Verify(msg, sig, henk.public)
if err != nil {
t.Errorf("Unable to verify Henk's signature; %s", err)
}
err = hans.Verify(msg, sig, henk.public)
if err != nil {
t.Errorf("Unable to verify Henk's signature; %s", err)
}
// Let's see if we can break the signature verification:
// (1) changing the message
err = hans.Verify([]byte("Wilders is een opruier"), sig, henk.public)
if err == nil {
t.Error("Expected an error as we changed the message")
}
// (2) changing the signature
err = hans.Verify(msg, []byte("I am not the signature"), henk.public)
if err == nil {
t.Error("Expected an error as we changed the signature")
}
// (3) changing the public key
err = hans.Verify(msg, sig, ingrid.public)
if err == nil {
t.Error("Expected an error as we changed the public key")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment