|
|
@ -1,6 +1,11 @@ |
|
|
|
package awesome.group.game.service.util; |
|
|
|
|
|
|
|
import awesome.group.game.service.common.log.L; |
|
|
|
|
|
|
|
import javax.crypto.Mac; |
|
|
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.security.InvalidKeyException; |
|
|
|
import java.security.MessageDigest; |
|
|
|
import java.security.NoSuchAlgorithmException; |
|
|
|
import java.util.Formatter; |
|
|
@ -19,6 +24,29 @@ public class EncryptUtil { |
|
|
|
return sha1; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* sha256_HMAC加密 |
|
|
|
* |
|
|
|
* @param message 消息 |
|
|
|
* @param secret 秘钥 |
|
|
|
* @return 加密后字符串 |
|
|
|
*/ |
|
|
|
public static String hmacSHA256(String secret, String message) { |
|
|
|
String hash = ""; |
|
|
|
try { |
|
|
|
Mac hmacSha256 = Mac.getInstance("HmacSHA256"); |
|
|
|
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256"); |
|
|
|
hmacSha256.init(secret_key); |
|
|
|
byte[] bytes = hmacSha256.doFinal(message.getBytes()); |
|
|
|
hash = byteToHex(bytes); |
|
|
|
} catch (NoSuchAlgorithmException e) { |
|
|
|
} catch (InvalidKeyException e) { |
|
|
|
L.trace("hmacSHA256_invalid_key", "secret:" + secret + ", message:" + message, e); |
|
|
|
} |
|
|
|
|
|
|
|
return hash; |
|
|
|
} |
|
|
|
|
|
|
|
public static String byteToHex(final byte[] hash) { |
|
|
|
Formatter formatter = new Formatter(); |
|
|
|
for (byte b : hash) { |
|
|
|