简体   繁体   English

将 hmac function 从 python 转换为 Java

[英]Convert hmac function from python to Java

I want to convert the below python code to the same java code.我想将下面的 python 代码转换为相同的 java 代码。 I am not sure how to get the same result by using java.我不确定如何使用 java 获得相同的结果。 I tried to use hmacutils in apache codec package but the result is not the same.我尝试在 apache 编解码器 package 中使用 hmacutils 但结果不一样。 python code: python 代码:

import hmac
import hashlib
secret_key = b"NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"
total_params = b"/public/api/ver1/accounts/new?type=binance&name=binance_account&api_key=XXXXXX&secret=YYYYYY"
signature = hmac.new(secret_key, total_params, hashlib.sha384).hexdigest()
print("signature = {0}".format(signature))

With commons-codec:commons-codec:1.15使用 commons-codec:commons-codec:1.15

final byte[] key = "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j".getBytes(StandardCharsets.UTF_8);
final Mac initializedMac = HmacUtils.getInitializedMac(HmacAlgorithms.HMAC_SHA_384, key);
final byte[] bytes = "/public/api/ver1/accounts/new?type=binance&name=binance_account&api_key=XXXXXX&secret=YYYYYY".getBytes(StandardCharsets.UTF_8);
initializedMac.update(bytes);
final byte[] digest = initializedMac.doFinal();
System.out.println(Hex.encodeHexString(digest));

Gives me output matching the output of that python snippet给我 output 匹配该 python 片段的 output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM