Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 795 | blopes | 1 | package br.com.kronus.binance.futures; |
| 2 | |||
| 3 | import javax.crypto.Mac; |
||
| 4 | import javax.crypto.spec.SecretKeySpec; |
||
| 5 | import java.nio.charset.StandardCharsets; |
||
| 6 | |||
| 7 | public class UtilHmac { |
||
| 8 | |||
| 9 | private UtilHmac() {} |
||
| 10 | |||
| 11 | public static String hmacSha256Hex(String chaveSecreta, String payload) { |
||
| 12 | try { |
||
| 13 | Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); |
||
| 14 | SecretKeySpec secret_key = new SecretKeySpec( |
||
| 15 | chaveSecreta.getBytes(StandardCharsets.UTF_8), |
||
| 16 | "HmacSHA256" |
||
| 17 | ); |
||
| 18 | sha256_HMAC.init(secret_key); |
||
| 19 | byte[] hash = sha256_HMAC.doFinal(payload.getBytes(StandardCharsets.UTF_8)); |
||
| 20 | return bytesToHex(hash); |
||
| 21 | } catch (Exception e) { |
||
| 22 | throw new RuntimeException("Erro ao gerar HMAC SHA256", e); |
||
| 23 | } |
||
| 24 | } |
||
| 25 | |||
| 26 | private static String bytesToHex(byte[] bytes) { |
||
| 27 | StringBuilder sb = new StringBuilder(bytes.length * 2); |
||
| 28 | for (byte b : bytes) { |
||
| 29 | sb.append(String.format("%02x", b)); |
||
| 30 | } |
||
| 31 | return sb.toString(); |
||
| 32 | } |
||
| 33 | } |