[Java] java 带MD5检验的输入流 →→→→→进入此内容的聊天室

来自 , 2020-01-18, 写在 Java, 查看 189 次.
URL http://www.code666.cn/view/df6d2338
  1. package com.javaonly.hmac.test;
  2.  
  3. import java.io.IOException;
  4. import java.security.InvalidKeyException;
  5. import java.security.KeyManagementException;
  6. import java.security.NoSuchAlgorithmException;
  7. import javax.crypto.BadPaddingException;
  8. import javax.crypto.Cipher;
  9. import javax.crypto.IllegalBlockSizeException;
  10. import javax.crypto.NoSuchPaddingException;
  11. import javax.crypto.SecretKey;
  12. import javax.crypto.spec.SecretKeySpec;
  13. import org.apache.commons.codec.binary.Hex;
  14. import javax.crypto.Mac;
  15.  
  16. public class ComputopTest {
  17.  
  18.     public static void main(String args[]) throws NoSuchAlgorithmException,
  19.             KeyManagementException,
  20.              InvalidKeyException,
  21.             IllegalBlockSizeException, BadPaddingException {
  22.  
  23.         String macKey = "The HMAC key";
  24.         String macData ="the data string"
  25.         System.out.println("MACDATA:"+macData);
  26.  
  27.         Mac mac = Mac.getInstance("HmacSHA256");
  28.                 //get the bytes of the hmac key and data string
  29.         byte[] secretByte = macKey.getBytes("UTF-8");
  30.         byte[] dataBytes = macData.getBytes("UTF-8");
  31.         SecretKey secret = new SecretKeySpec(secretByte, "HMACSHA256");
  32.  
  33.         mac.init(secret);
  34.         byte[] doFinal = mac.doFinal(dataBytes);
  35.         byte[] hexB = new Hex().encode(doFinal);
  36.         String checksum = new String(hexB);
  37.  
  38.     }
  39. }
  40. //该片段来自于http://yuncode.net
  41.  

回复 "java 带MD5检验的输入流"

这儿你可以回复上面这条便签

captcha