This repository was archived by the owner on Sep 6, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +72
-14
lines changed Expand file tree Collapse file tree 3 files changed +72
-14
lines changed Original file line number Diff line number Diff line change 4
4
package crypto
5
5
6
6
import (
7
- "bytes"
8
7
"crypto/elliptic"
9
8
"crypto/hmac"
10
9
"crypto/rand"
@@ -345,7 +344,9 @@ func KeyEqual(k1, k2 Key) bool {
345
344
return true
346
345
}
347
346
348
- b1 , err1 := k1 .Bytes ()
349
- b2 , err2 := k2 .Bytes ()
350
- return bytes .Equal (b1 , b2 ) && err1 == err2
347
+ if k1 .Type () != k2 .Type () {
348
+ return false
349
+ }
350
+
351
+ return k1 .Equals (k2 )
351
352
}
Original file line number Diff line number Diff line change @@ -101,18 +101,19 @@ func testKeyEncoding(t *testing.T, sk PrivKey) {
101
101
}
102
102
103
103
func testKeyEquals (t * testing.T , k Key ) {
104
- kb , err := k .Bytes ()
105
- if err != nil {
106
- t .Fatal (err )
107
- }
104
+ // kb, err := k.Raw ()
105
+ // if err != nil {
106
+ // t.Fatal(err)
107
+ // }
108
108
109
109
if ! KeyEqual (k , k ) {
110
110
t .Fatal ("Key not equal to itself." )
111
111
}
112
112
113
- if ! KeyEqual (k , testkey (kb )) {
114
- t .Fatal ("Key not equal to key with same bytes." )
115
- }
113
+ // bad test, relies on deep internals..
114
+ // if !KeyEqual(k, testkey(kb)) {
115
+ // t.Fatal("Key not equal to key with same bytes.")
116
+ // }
116
117
117
118
sk , pk , err := test .RandTestKeyPair (RSA , 512 )
118
119
if err != nil {
@@ -143,5 +144,18 @@ func (pk testkey) Raw() ([]byte, error) {
143
144
}
144
145
145
146
func (pk testkey ) Equals (k Key ) bool {
146
- return KeyEqual (pk , k )
147
+ if pk .Type () != k .Type () {
148
+ return false
149
+ }
150
+ a , err := pk .Raw ()
151
+ if err != nil {
152
+ return false
153
+ }
154
+
155
+ b , err := k .Raw ()
156
+ if err != nil {
157
+ return false
158
+ }
159
+
160
+ return bytes .Equal (a , b )
147
161
}
Original file line number Diff line number Diff line change 3
3
package crypto
4
4
5
5
import (
6
+ "bytes"
6
7
"crypto"
7
8
"crypto/rand"
8
9
"crypto/rsa"
@@ -63,7 +64,21 @@ func (pk *RsaPublicKey) Raw() ([]byte, error) {
63
64
64
65
// Equals checks whether this key is equal to another
65
66
func (pk * RsaPublicKey ) Equals (k Key ) bool {
66
- return KeyEqual (pk , k )
67
+ // make sure this is an rsa public key
68
+ other , ok := (k ).(* RsaPublicKey )
69
+ if ! ok {
70
+ a , err := pk .Raw ()
71
+ if err != nil {
72
+ return false
73
+ }
74
+ b , err := k .Raw ()
75
+ if err != nil {
76
+ return false
77
+ }
78
+ return bytes .Equal (a , b )
79
+ }
80
+
81
+ return pk .k .N .Cmp (other .k .N ) == 0 && pk .k .E == other .k .E
67
82
}
68
83
69
84
// Sign returns a signature of the input data
@@ -93,7 +108,35 @@ func (sk *RsaPrivateKey) Raw() ([]byte, error) {
93
108
94
109
// Equals checks whether this key is equal to another
95
110
func (sk * RsaPrivateKey ) Equals (k Key ) bool {
96
- return KeyEqual (sk , k )
111
+ // make sure this is an rsa public key
112
+ other , ok := (k ).(* RsaPrivateKey )
113
+ if ! ok {
114
+ a , err := sk .Raw ()
115
+ if err != nil {
116
+ return false
117
+ }
118
+ b , err := k .Raw ()
119
+ if err != nil {
120
+ return false
121
+ }
122
+ return bytes .Equal (a , b )
123
+ }
124
+
125
+ a := sk .sk
126
+ b := other .sk
127
+
128
+ if a .PublicKey .N .Cmp (b .PublicKey .N ) != 0 {
129
+ return false
130
+ }
131
+ if a .PublicKey .E != b .PublicKey .E {
132
+ return false
133
+ }
134
+
135
+ if a .D .Cmp (b .D ) != 0 {
136
+ return false
137
+ }
138
+
139
+ return true
97
140
}
98
141
99
142
// UnmarshalRsaPrivateKey returns a private key from the input x509 bytes
You can’t perform that action at this time.
0 commit comments