Source file src/crypto/rsa/equal_test.go

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rsa_test
     6  
     7  import (
     8  	"crypto"
     9  	"crypto/rsa"
    10  	"crypto/x509"
    11  	"testing"
    12  )
    13  
    14  func TestEqual(t *testing.T) {
    15  	t.Setenv("GODEBUG", "rsa1024min=0")
    16  
    17  	private := test512Key
    18  	public := &private.PublicKey
    19  
    20  	if !public.Equal(public) {
    21  		t.Errorf("public key is not equal to itself: %v", public)
    22  	}
    23  	if !public.Equal(crypto.Signer(private).Public().(*rsa.PublicKey)) {
    24  		t.Errorf("private.Public() is not Equal to public: %q", public)
    25  	}
    26  	if !private.Equal(private) {
    27  		t.Errorf("private key is not equal to itself: %v", private)
    28  	}
    29  
    30  	enc, err := x509.MarshalPKCS8PrivateKey(private)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	decoded, err := x509.ParsePKCS8PrivateKey(enc)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	if !public.Equal(decoded.(crypto.Signer).Public()) {
    39  		t.Errorf("public key is not equal to itself after decoding: %v", public)
    40  	}
    41  	if !private.Equal(decoded) {
    42  		t.Errorf("private key is not equal to itself after decoding: %v", private)
    43  	}
    44  
    45  	other := test512KeyTwo
    46  	if public.Equal(other.Public()) {
    47  		t.Errorf("different public keys are Equal")
    48  	}
    49  	if private.Equal(other) {
    50  		t.Errorf("different private keys are Equal")
    51  	}
    52  }
    53  

View as plain text