从HMAC-SHA256在线生成器工具和Java代码获得不同的值

发布于 2025-02-03 21:03:55 字数 2134 浏览 1 评论 0原文

我想从我的Java代码中生成一个签名,这是我的方法:

public static String hmacWithJava(String algorithm, String data, String key)
            throws NoSuchAlgorithmException, InvalidKeyException {
        Mac mac = Mac.getInstance(algorithm);
        mac.init(new SecretKeySpec(key.getBytes(), algorithm));
        String hexFormatSignature = "sha256=" + HexFormat.of().formatHex(mac.doFinal(data.getBytes()));
        return hexFormatSignature;
    }

   algorithm="HmacSHA256"`

    key="testKey";

    data="{
      "action" : "created",
      "installation" : {
        "id" : 2,
        "account" : {
          "login" : "testUser",
          "type" : "User"
        }
      },
      "sender" : {
        "login" : "testUser",
        "type" : "User"
      }
    }"

hexFormatSignature="sha256=11e20c46886a8e681fd2a3ad0e3a100d42579e1cf95417dd27acc80cedadabd5"

如果我将相同的有效载荷放在 https://www.devglan.com/online-tools/hmac-sha256-online 工具获得了下一个结果:

但这不是正确的签名,正确的签名必须是:“ AEF0567D41BB28ABE34D9202CD019668A8B35F65DDDDDD2222A74DE5C1982B6A

”我正在手动使用并生成正确的签名。

左侧是生成的形式Java代码,右侧是某人给我及其工作的左侧。

这是文本和它们具有相同值的比较,但是不同的凹痕格式,实际上,如果我在原始有效载荷中添加一个空间,我将具有不同的签名。有没有办法解决这个问题?也许我需要配置一些东西以从Java获取正确的格式?

这是手动有效载荷,正在工作:

{
  "action": "created",
   "installation" : {
       "id": 2,
        "account":{
            "login": "testUser",
            "type": "User"
         }
    },
    "sender" : {
        "login" : "testUser",
        "type" : "User"
    }
} 

我正在使用杰克逊从有效载荷中获取JSON字符串:

String jsonStr = obj.writerWithDefaultPrettyPrinter().writeValueAsString(payload);

我的有效负载对象看起来像:

Payload{action='created', installation=model.Installation@3d121db3, sender=model.User@3b07a0d6}

I would like to generate a signature from my Java code, this is my method:

public static String hmacWithJava(String algorithm, String data, String key)
            throws NoSuchAlgorithmException, InvalidKeyException {
        Mac mac = Mac.getInstance(algorithm);
        mac.init(new SecretKeySpec(key.getBytes(), algorithm));
        String hexFormatSignature = "sha256=" + HexFormat.of().formatHex(mac.doFinal(data.getBytes()));
        return hexFormatSignature;
    }

where

   algorithm="HmacSHA256"`

    key="testKey";

    data="{
      "action" : "created",
      "installation" : {
        "id" : 2,
        "account" : {
          "login" : "testUser",
          "type" : "User"
        }
      },
      "sender" : {
        "login" : "testUser",
        "type" : "User"
      }
    }"

hexFormatSignature="sha256=11e20c46886a8e681fd2a3ad0e3a100d42579e1cf95417dd27acc80cedadabd5"

If I put the same payload in the https://www.devglan.com/online-tools/hmac-sha256-online tool y got the next result: "11e20c46886a8e681fd2a3ad0e3a100d42579e1cf95417dd27acc80cedadabd5"

But this is not the correct signature, the correct one must be:"aef0567d41bb28abe34d9202cd019668a8b35f65dd1981d22a74de5c19823b6a"

The difference I have noticed is related with the json format, I mean the one generated from Jackson has less espaces o more enters that the one I am using manually and is generating the correct signature.

enter image description here

Left side is the one generated form Java code and right side the one that someone give me and its working.

This is the comparison between both text and they have the same values, but different indentation format, in fact if I add a space in the original payload I will have a different signature. Is there a way to solve this issue? Maybe I need to configure something to get the correct format from Java?

This is the manual payload, which is working:

{
  "action": "created",
   "installation" : {
       "id": 2,
        "account":{
            "login": "testUser",
            "type": "User"
         }
    },
    "sender" : {
        "login" : "testUser",
        "type" : "User"
    }
} 

I am using Jackson to get the Json String from the Payload:

String jsonStr = obj.writerWithDefaultPrettyPrinter().writeValueAsString(payload);

And my Payload object looks like this:

Payload{action='created', installation=model.Installation@3d121db3, sender=model.User@3b07a0d6}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文