2323import com .jcraft .jsch .JSch ;
2424import com .jcraft .jsch .JSchException ;
2525import 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 ;
2631import java .io .ByteArrayOutputStream ;
2732import java .io .IOException ;
2833import java .math .BigInteger ;
2934import java .nio .ByteBuffer ;
3035import 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 .*;
3537import java .security .spec .InvalidKeySpecException ;
3638import java .security .spec .RSAPrivateCrtKeySpec ;
3739import java .security .spec .RSAPublicKeySpec ;
3840import 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
4642public 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 ];
0 commit comments