public static void main(String[] args)
{
File file = new File("D:\\zhangming\\id_rsa.pub");
FileReader fileReader = null;
String test = "";
byte[] bs =null;
//=========按Byte读取,读取字节文件
try
{
FileInputStream fileInputStream = new FileInputStream(file);
bs= new byte[2048];
while (fileInputStream.read() != -1)
{
fileInputStream.read(bs);
}
}
catch (FileNotFoundException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("。。。。。。。" + bs.length);
test = new String(bs);
System.out.println("===========================================");
System.out.println(test);
System.out.println("===========================================");
//==========读取字符文件=========
try
{
fileReader = new FileReader(file);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(fileReader);
StringBuffer buffer = new StringBuffer();
String temp = null;
try
{
while ((temp = reader.readLine()) != null)
{
buffer.append(temp);
}
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println(buffer.toString());
System.out.println("MD5 fingerprint:\n");
try
{
System.out.println(md5Digest(bs));
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
System.out.println("sha1 fingerprint:\n");
try
{
System.out.println(sha_1Digest(bs));
}
catch (NoSuchAlgorithmException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String md5Digest(byte[] messageByte)
throws NoSuchAlgorithmException
{
MessageDigest digestmds = MessageDigest.getInstance("md5");
digestmds.update(messageByte);
byte[] digesta = digestmds.digest();
return byte2hex(digesta);
}
/**
* @param messageByte
* @return
* @throws NoSuchAlgorithmException
*/
public static String sha_1Digest(byte[] messageByte)
throws NoSuchAlgorithmException
{
MessageDigest digestmds = MessageDigest.getInstance("SHA-1");
digestmds.update(messageByte);
byte[] digesta = digestmds.digest();
return byte2hex(digesta);
}
/**
* 二行制转字符串
*
* @param b
* @return String
*/
public static String byte2hex(byte[] b) // 二行制转字符串
{
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++)
{
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
if (n < b.length - 1)
hs = hs + ":";
}
return hs.toLowerCase(Locale.getDefault());
}