Skip to content

Commit a84b84b

Browse files
committed
to md5 fingerprint
1 parent a9e2ee5 commit a84b84b

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

core/src/main/java/com/flowci/core/common/helper/CipherHelper.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,21 @@
2323
import com.jcraft.jsch.JSch;
2424
import com.jcraft.jsch.JSchException;
2525
import com.jcraft.jsch.KeyPair;
26+
import sun.security.util.DerInputStream;
27+
import sun.security.util.DerValue;
28+
29+
import javax.crypto.Cipher;
30+
import javax.crypto.spec.SecretKeySpec;
2631
import java.io.ByteArrayOutputStream;
2732
import java.io.IOException;
2833
import java.math.BigInteger;
2934
import java.nio.ByteBuffer;
3035
import java.nio.charset.StandardCharsets;
31-
import java.security.KeyFactory;
32-
import java.security.NoSuchAlgorithmException;
33-
import java.security.PrivateKey;
34-
import java.security.PublicKey;
36+
import java.security.*;
3537
import java.security.spec.InvalidKeySpecException;
3638
import java.security.spec.RSAPrivateCrtKeySpec;
3739
import java.security.spec.RSAPublicKeySpec;
3840
import java.util.Base64;
39-
import javax.crypto.Cipher;
40-
import javax.crypto.spec.SecretKeySpec;
41-
42-
import org.apache.commons.lang.NotImplementedException;
43-
import sun.security.util.DerInputStream;
44-
import sun.security.util.DerValue;
4541

4642
public abstract class CipherHelper {
4743

@@ -102,12 +98,29 @@ public static String decrypt(String encrypted, String privateKey) {
10298
}
10399
}
104100

105-
public static String fingerprintMd5(String publicKey) {
106-
throw new NotImplementedException();
101+
public static String fingerprintMd5(String publicKey) throws NoSuchAlgorithmException {
102+
String derFormat = publicKey.split(" ")[1].trim();
103+
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
104+
byte[] digest = messageDigest.digest(Base64.getDecoder().decode(derFormat));
105+
final StringBuilder toRet = new StringBuilder();
106+
for (int i = 0; i < digest.length; i++) {
107+
if (i != 0) {
108+
toRet.append(":");
109+
}
110+
111+
int b = digest[i] & 0xff;
112+
String hex = Integer.toHexString(b);
113+
114+
if (hex.length() == 1) {
115+
toRet.append("0");
116+
}
117+
toRet.append(hex);
118+
}
119+
return toRet.toString();
107120
}
108121

109122
private static PrivateKey toPrivateKey(String key)
110-
throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
123+
throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
111124
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
112125

113126
String content = key.replaceAll("\\n", "").replace(RsaPrivateKeyStart, "").replace(RsaPrivateKeyEnd, "");
@@ -127,7 +140,7 @@ private static PrivateKey toPrivateKey(String key)
127140
BigInteger crtCoef = seq[8].getBigInteger();
128141

129142
RSAPrivateCrtKeySpec keySpec =
130-
new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef);
143+
new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef);
131144

132145
return keyFactory.generatePrivate(keySpec);
133146
}
@@ -136,7 +149,7 @@ private static PrivateKey toPrivateKey(String key)
136149
* from <type><space><base64data><space><comment> to public key
137150
*/
138151
private static PublicKey toPublicKey(String sshPublicKey)
139-
throws NoSuchAlgorithmException, InvalidKeySpecException {
152+
throws NoSuchAlgorithmException, InvalidKeySpecException {
140153
String[] line = sshPublicKey.trim().split(" ", 3);
141154
String type = line[0];
142155
String content = line[1];

core/src/test/java/com/flowci/core/test/common/CipherHelperTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.io.IOException;
2929
import java.io.InputStream;
30+
import java.security.NoSuchAlgorithmException;
3031

3132
import static com.flowci.core.common.helper.CipherHelper.RSA.fingerprintMd5;
3233

@@ -61,7 +62,7 @@ public void should_encrypt_decrypt_by_rsa() {
6162
}
6263

6364
@Test
64-
public void should_create_public_key_fingerprint() throws IOException {
65+
public void should_create_public_key_fingerprint() throws IOException, NoSuchAlgorithmException {
6566
InputStream in = CipherHelper.class.getClassLoader().getResourceAsStream("pk_fingerprint");
6667
String publicKey = StringHelper.toString(in);
6768
Assert.assertEquals("09:e6:ce:d3:ba:a3:ee:75:9e:96:7b:55:12:85:c6:4e", fingerprintMd5(publicKey));

0 commit comments

Comments
 (0)