Closed
Description
Since golang.org/cl/225460 (#21704), the following code compiles but prints an unexpected result:
package main
import (
"crypto/rand"
"crypto/rsa"
)
func main() {
pk, _ := rsa.GenerateKey(rand.Reader, 512)
println(pk.Equal(pk)) // prints false
}
This is due to PrivateKey embedding PublicKey
without having its own Equal
method to mask PublicKey
’s.
This causes difficult to debug issues with tools like go-cmp, as it looks for an Equal
method on each type recursively and finds one in this case for the concrete private key types. The diff printed by the following code will be non-empty, but the values are identical:
package main
import (
"crypto/rand"
"crypto/rsa"
"fmt"
"math/big"
"github.com/google/go-cmp/cmp"
)
func main() {
pk, _ := rsa.GenerateKey(rand.Reader, 512)
bigIntCmp := cmp.Comparer(func(x, y *big.Int) bool {
return x.Cmp(y) == 0
})
fmt.Printf("Diff: '%s'\n", cmp.Diff(pk, pk, bigIntCmp))
}