Skip to content

Commit c64d040

Browse files
authored
[JENKINS-76219] Update SSH key fingerprints to use SHA-256 (#631)
Replace MD5 fingerprint format with SHA-256 Base64 encoding to match OpenSSH 6.8+ behavior. Existing stored keys automatically use the new format without migration.
1 parent b796a5b commit c64d040

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/main/java/hudson/plugins/sshslaves/verifiers/HostKey.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525

2626
import com.trilead.ssh2.KnownHosts;
2727
import java.io.Serializable;
28+
import java.security.MessageDigest;
29+
import java.security.NoSuchAlgorithmException;
2830
import java.util.Arrays;
31+
import java.util.Base64;
2932

3033
/**
3134
* A representation of the SSH key provided by a remote host to verify itself
@@ -63,7 +66,14 @@ public byte[] getKey() {
6366
}
6467

6568
public String getFingerprint() {
66-
return KnownHosts.createHexFingerprint(getAlgorithm(), getKey());
69+
try {
70+
MessageDigest md = MessageDigest.getInstance("SHA-256");
71+
byte[] digest = md.digest(getKey());
72+
return "SHA256:" + Base64.getEncoder().encodeToString(digest);
73+
} catch (NoSuchAlgorithmException e) {
74+
// SHA-256 should always be available, but fallback to MD5 if not
75+
return KnownHosts.createHexFingerprint(getAlgorithm(), getKey());
76+
}
6777
}
6878

6979
@Override
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package hudson.plugins.sshslaves.verifiers;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.Base64;
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* @author Steven Scheffler
10+
*/
11+
class HostKeyTest {
12+
13+
@Test
14+
void testFingerprintUsesSHA256() {
15+
// Example RSA key bytes (this is just test data)
16+
byte[] keyBytes = "test-key-data".getBytes();
17+
HostKey hostKey = new HostKey("ssh-rsa", keyBytes);
18+
19+
String fingerprint = hostKey.getFingerprint();
20+
21+
// Verify it starts with SHA256: prefix
22+
assertTrue(fingerprint.startsWith("SHA256:"), "Fingerprint should use SHA256 format");
23+
24+
// Verify it's Base64 encoded after the prefix
25+
String base64Part = fingerprint.substring(7); // Remove "SHA256:"
26+
assertDoesNotThrow(
27+
() -> Base64.getDecoder().decode(base64Part),
28+
"Fingerprint should be valid Base64 after SHA256: prefix");
29+
}
30+
31+
@Test
32+
void testFingerprintFormat() {
33+
byte[] keyBytes = "test-key-data".getBytes();
34+
HostKey hostKey = new HostKey("ssh-rsa", keyBytes);
35+
36+
String fingerprint = hostKey.getFingerprint();
37+
38+
// Should match pattern: SHA256:[Base64]
39+
assertTrue(fingerprint.matches("SHA256:[A-Za-z0-9+/=]+"), "Fingerprint should match SHA256:Base64 format");
40+
}
41+
}

0 commit comments

Comments
 (0)