Why key.PublicKey.Equal(key.PublicKey) return false ?

115 views
Skip to first unread message

christoph...@gmail.com

unread,
Oct 27, 2020, 11:21:05 AM10/27/20
to golang-nuts
I have the following test that fails reporting a key  mismatch.

func TestKeyEqual(t *testing.T) {
     key, err := rsa.GenerateKey(rand.Reader, 2048)
     if err != nil {
         t.Fatal("failed generating private key: ", err)
     }
     if !key.PublicKey.Equal(key.PublicKey) {
         t.Fatal("key mismatch")
     }
}

Why is that ? Shouldn’t it return true ?

Initially I compared key after save and reload read, and since it failed, I just tested the same key for equality to verify that Equal was working as expected. Apparently it does not or I’m doing something wrong.

christoph...@gmail.com

unread,
Oct 27, 2020, 12:01:53 PM10/27/20
to golang-nuts
My formulation was not clear. The test fails, and it reports a key mismatch.

Jason Phillips

unread,
Oct 27, 2020, 12:16:36 PM10/27/20
to golang-nuts
The implementation of Equal() expects the PublicKey argument to be a pointer to an rsa.PublicKey. If you do the following it works:

if !key.PublicKey.Equal(&key.PublicKey) {
    t.Fatal("key mismatch")
}

It should probably be documented as such.
Reply all
Reply to author
Forward
0 new messages