You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					
					
						
							37 lines
						
					
					
						
							1.0 KiB
						
					
					
				
			
		
		
	
	
							37 lines
						
					
					
						
							1.0 KiB
						
					
					
				| package com.ynxbd.common.helper.common;
 | |
| 
 | |
| import javax.crypto.Mac;
 | |
| import javax.crypto.spec.SecretKeySpec;
 | |
| import java.nio.charset.StandardCharsets;
 | |
| 
 | |
| /**
 | |
|  * HMAC算法
 | |
|  */
 | |
| public class HMACHelper {
 | |
| 
 | |
|     public static String sha256(String data, String key) {
 | |
|         try {
 | |
|             Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
 | |
| 
 | |
|             SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
 | |
| 
 | |
|             sha256_HMAC.init(secret_key);
 | |
| 
 | |
|             byte[] array = sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
 | |
| 
 | |
|             StringBuilder sb = new StringBuilder();
 | |
| 
 | |
|             for (byte item : array) {
 | |
|                 sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
 | |
|             }
 | |
|             return sb.toString();
 | |
|         } catch (Exception e) {
 | |
|             e.printStackTrace();
 | |
|         }
 | |
|         return null;
 | |
|     }
 | |
| 
 | |
|     public static void main(String[] args) {
 | |
|         System.out.println(sha256("123", "1"));
 | |
|     }
 | |
| }
 | |
| 
 |