?? shatools.java
字號:
package com.gmc.algorithms;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import com.gmc.crypto.MessageDigester;
/**
* SHA摘要算法的實現(xiàn)類
* @author Wanna
*
*/
public class SHATools implements MessageDigester
{
private static final String algorithm = "SHA"; // 數(shù)字摘要算法
private MessageDigest SHAmes;
public MessageDigest getSHAmes()
{
return SHAmes;
}
public void setSHAmes(MessageDigest ames)
{
SHAmes = ames;
}
/**
* 構(gòu)造方法,創(chuàng)建MessageDigest對象,供消息摘要使用
*
*/
public SHATools()
{
try
{
SHAmes = MessageDigest.getInstance(algorithm);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
}
/**
* 消息摘要,對字節(jié)數(shù)組操作,無需密鑰key,傳入null即可,
*/
public String encrypt(byte[] plainText, Key key) throws Exception
{
if (plainText == null || plainText.length == 0)
{
return null;
}
if (key != null)
{
return null; // 消息摘要不需要傳入密鑰
}
return this.byte2hex(this.basicDigester(plainText));
// return new String(this.DigestePlain(plainText));
}
/**
* 消息摘要,對字符串操作,無需密鑰key,傳入null即可,
*/
public String encrypt(String plainText, Key key) throws Exception
{
if (plainText == null)
{
return null;
}
if (key != null)
{
return null;
}
byte[] input = plainText.getBytes();
return this.byte2hex(this.basicDigester(input));
// return new String(this.DigestePlain(input));
}
/**
* 消息摘要,對文件操作,無需密鑰key,傳入null即可,
*/
public String encrypt(File file, Key key) throws Exception
{
if (file == null)
{
return null;
}
if (!file.exists()||file.isDirectory())
{
return null;
}
if (key != null)
{
return null;
}
byte[] input = new byte[(int) file.length()];
FileInputStream fis = null;
try
{
fis = new FileInputStream(file);
for (int i = 0; i < file.length(); i++)
{
input[i] = (byte) fis.read();
}
// return new String(this.DigestePlain(input));
return this.byte2hex(this.basicDigester(input));
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
fis.close();
}
}
/**
* 消息摘要,對輸入流中的數(shù)據(jù)操作,無需密鑰key,傳入null即可,
*/
public String encrypt(InputStream plainText, Key key) throws Exception
{
if (plainText == null)
{
return null;
}
if (key != null)
{
return null;
}
int length = -1;
ArrayList temp = new ArrayList();
try
{
while ((length = plainText.read()) != -1)
{
temp.add((byte) length);
}
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
byte[] input = new byte[temp.size()];
for (int i = 0; i < temp.size(); i++)
{
Byte bt = (Byte) temp.get(i);
input[i] = bt.byteValue();
}
// return new String(this.DigestePlain(input));
return this.byte2hex(this.basicDigester(input));
}
/**
* 消息摘要的基本方法,供其他方法調(diào)用。
*
* @param plain 要進行摘要處理的字節(jié)數(shù)據(jù)
* @return 摘要計算后的數(shù)據(jù)
*/
private byte[] basicDigester(byte[] input)
{
MessageDigest md = this.getSHAmes();
md.update(input);
return md.digest();
}
/**
* 將字節(jié)轉(zhuǎn)換成字符串
*
* @param b
* @return
*/
private 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;
}
}
return hs.toUpperCase();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -