javax.crypto.Mac
類是JCA中提供信息認(rèn)證碼服務(wù)的類崖瞭,用于檢測消息的的正確性淋叶「琅伲可用于用戶身份認(rèn)證的密碼存儲和驗證環(huán)節(jié)(取代普通的消息摘要算法)码俩。
實例化
Mac
沒有公開的構(gòu)造方法度帮,所以只能調(diào)用其靜態(tài)方法getInstace
進(jìn)行實現(xiàn)化。這個方法有多個重載如下:
public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException
public static final Mac getInstance(String algorithm, Provider provider)
throws NoSuchAlgorithmException
public static final Mac getInstance(String algorithm, String provider)
throws NoSuchAlgorithmException, NoSuchProviderException
我們通常使用的是public static final Mac getInstance(String algorithm)
稿存;此方法需要一個字符串作為參數(shù)笨篷,用于說明使用哪個MAC算法。
初始化
可用的初始化方法如下:
public final void init(Key key) throws InvalidKeyException;
public final void init(Key key, AlgorithmParameterSpec algorithmParameterSpec )
throws InvalidKeyException, InvalidAlgorithmParameterException
通常我們使用public final void init(Key key) throws InvalidKeyException;
就足夠了瓣履。這個方法需要以一個Key
對象為參數(shù)對Mac
實例進(jìn)行初始化率翅。
update 方法
public final void update(byte b) throws IllegalStateException;
public final void update(byte[] bytes) throws IllegalStateException;
public final void update(byte[] bytes, int offset, int length) throws IllegalStateException;
public final void update(ByteBuffer byteBuffer);
使用指定信息更新生成的消息摘要;
reset 重置消息摘要方法
public final void reset();
重置當(dāng)前實例的消息摘要袖迎,以備后用冕臭;
doFinal 生成用于信息驗證碼的消息摘要
public final byte[] doFinal() throws IllegalStateException;
public final byte[] doFinal(byte[] bytes) throws IllegalStateException;
生成消息摘要腺晾,并以byte數(shù)組形式返回;
如果有參數(shù)辜贵,則先使用參數(shù)調(diào)用update方法悯蝉,再調(diào)用無參doFinal。
public final void doFinal(byte[] bytes, int offset) throws ShortBufferException, IllegalStateException;
把生成的消息摘要存儲到指定byte數(shù)組的指定位置托慨,返回空鼻由。
支持的算法
- HmacMd5
- hmacSha1
- hmacSha224
- hmacSha256
- hmacSha384
- hmacSha512
注:不區(qū)別大小寫
示例
Mac hmacMd5 = Mac.getInstance("HmacMd5");
hmacMd5.init(new SecretKeySpec("1".getBytes(), hmacMd5.getAlgorithm()));
hmacMd5.update("1".getBytes());
System.out.println("hmacMd5:" + DatatypeConverter.printHexBinary(hmacMd5.doFinal()));
Mac hmacSha1 = Mac.getInstance("hmacSha1");
hmacSha1.init(new SecretKeySpec("1".getBytes(), hmacSha1.getAlgorithm()));
hmacSha1.update("1".getBytes());
System.out.println("hmacSha1:" + DatatypeConverter.printHexBinary(hmacSha1.doFinal()));
Mac hmacSha224 = Mac.getInstance("hmacSha224");
hmacSha224.init(new SecretKeySpec("1".getBytes(), hmacSha224.getAlgorithm()));
hmacSha224.update("1".getBytes());
System.out.println("hmacSha224:" + DatatypeConverter.printHexBinary(hmacSha224.doFinal()));
Mac hmacSha256 = Mac.getInstance("hmacSha256");
hmacSha256.init(new SecretKeySpec("1".getBytes(), hmacSha256.getAlgorithm()));
hmacSha256.update("1".getBytes());
System.out.println("hmacSha256:" + DatatypeConverter.printHexBinary(hmacSha256.doFinal()));
Mac hmacSha384 = Mac.getInstance("hmacSha384");
hmacSha384.init(new SecretKeySpec("1".getBytes(), hmacSha384.getAlgorithm()));
hmacSha384.update("1".getBytes());
System.out.println("hmacSha384:" + DatatypeConverter.printHexBinary(hmacSha384.doFinal()));
Mac hmacSha512 = Mac.getInstance("hmacSha512");
hmacSha512.init(new SecretKeySpec("1".getBytes(), hmacSha512.getAlgorithm()));
hmacSha512.update("1".getBytes());
System.out.println("hmacSha512:" + DatatypeConverter.printHexBinary(hmacSha512.doFinal()));