@@ -19,9 +19,12 @@ package libgit2
19
19
import (
20
20
"bufio"
21
21
"bytes"
22
+ "crypto/md5"
22
23
"crypto/sha1"
24
+ "crypto/sha256"
23
25
"crypto/x509"
24
26
"fmt"
27
+ "hash"
25
28
"net"
26
29
"net/url"
27
30
"strings"
@@ -157,7 +160,7 @@ func (s *PublicKeyAuth) Method(secret corev1.Secret) (*git.Auth, error) {
157
160
// is an entry for the hostname _and_ port.
158
161
host = knownhosts .Normalize (s .host )
159
162
for _ , k := range kk {
160
- if k .matches (host , cert .Hostkey . HashSHA1 [:] ) {
163
+ if k .matches (host , cert .Hostkey ) {
161
164
return git2go .ErrOk
162
165
}
163
166
}
@@ -195,13 +198,28 @@ func parseKnownHosts(s string) ([]knownKey, error) {
195
198
return knownHosts , nil
196
199
}
197
200
198
- func (k knownKey ) matches (host string , key [] byte ) bool {
201
+ func (k knownKey ) matches (host string , hostkey git2go. HostkeyCertificate ) bool {
199
202
if ! containsHost (k .hosts , host ) {
200
203
return false
201
204
}
202
205
203
- hash := sha1 .Sum (k .key .Marshal ())
204
- if bytes .Compare (hash [:], key ) != 0 {
206
+ var fingerprint []byte
207
+ var hasher hash.Hash
208
+ switch hostkey .Kind {
209
+ case git2go .HostkeyMD5 :
210
+ fingerprint = hostkey .HashMD5 [:]
211
+ hasher = md5 .New ()
212
+ case git2go .HostkeySHA1 :
213
+ fingerprint = hostkey .HashSHA1 [:]
214
+ hasher = sha1 .New ()
215
+ case git2go .HostkeySHA256 :
216
+ fingerprint = hostkey .HashSHA256 [:]
217
+ hasher = sha256 .New ()
218
+ default :
219
+ return false
220
+ }
221
+ hasher .Write (k .key .Marshal ())
222
+ if bytes .Compare (hasher .Sum (nil ), fingerprint ) != 0 {
205
223
return false
206
224
}
207
225
0 commit comments