package com.huatek.frame.base.util;

import java.security.Key;
import java.util.Map;

import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

/**
 * @author key
 * token工具类
 */
public class JavaWebToken {

	/**
	 * 该方法使用HS256算法和Secret:bankgl生成signKey
	 * @return
	 */
	private static Key getKeyInstance() {
		// We will sign our JavaWebToken with our ApiKey secret
		SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
		byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary("bankgl");
		Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
		return signingKey;
	}

	/**
	 * 使用HS256签名算法和生成的signingKey最终的Token,claims中是有效载荷
	 * @param claims
	 * @return
	 */
	public static String createJavaWebToken(Map<String, Object> claims) {
		return Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS256, getKeyInstance()).compact();
	}

	/**
	 *  解析Token，同时也能验证Token，当验证失败返回null
	 * @param jwt
	 * @return
	 */
	public static Map<String, Object> parserJavaWebToken(String jwt) {
		Map<String, Object> jwtClaims = Jwts.parser().setSigningKey(getKeyInstance()).parseClaimsJws(jwt).getBody();
		return jwtClaims;
	} 
}
