ds753947 2014-10-29 18:41
浏览 80
已采纳

对于某些字符,PHP和Clojure(Java)代码之间的原始MD5 base64编码字符串的结果不同

I have a server that does create a hash using the following code:

base64_encode(md5("some value", true))

What I have to do is to produce the same hash value in Clojure (using Java interop). What I did is to create the following Clojure functions:

(defn md5-raw [s]
  (let [algorithm (java.security.MessageDigest/getInstance "MD5")
    size (* 2 (.getDigestLength algorithm))]
    (.digest algorithm (.getBytes s))))

(defn bytes-to-base64-string [bytes]
  (String. (b64/encode bytes) "UTF-8"))

Then I use that code that way:

(bytes-to-base64-string (md5-raw "some value")

Now, everything works fine with normal strings. However, after processing multiple different examples, I found the the following character is causing issues:

This is the UTF-8 character #8217.

If I run the following PHP code:

base64_encode(md5("’", true))

What is returned is:

yOy9/y97p/GfapveLVQAHA==

If I run the following Clojure code:

(bytes-to-base64-string (md5-raw "’"))

I get the following value:

aF1ZConzUtEGRN2YXaKpoQ==

Why is that? I am suspecting a character encoding issue, but everything appears to be handled as UTF-8 as far as I can see.

  • 写回答

1条回答 默认 最新

  • doqpm82240 2014-10-29 19:01
    关注

    Not everything can be guaranteed to be UTF-8 in your example, the following expression depends on your default charset:

    (.getBytes s)
    

    You should - well, actually this depends on your use case - use:

    (.getBytes s "UTF-8")
    

    Demonstration:

    (defn md5-with-charset
      [s charset]
      (let [algorithm (java.security.MessageDigest/getInstance "MD5")]
        (.digest algorithm (.getBytes s charset))))
    
    (b64 (md5-with-charset "’" "UTF-8"))  ;; => "yOy9/y97p/GfapveLVQAHA=="
    (b64 (md5-with-charset "’" "ASCII"))  ;; => "0UV7csP7MjomcRJa7z6rXQ=="
    (b64 (md5-with-charset "’" "UTF-16")) ;; => "3CLVThylT2KkrocdUpxIpg=="
    (b64 (md5-with-charset "’" "UTF-32")) ;; => "iHBMMMzkWTbPU+n8GCHitQ=="
    

    (where b64 is a base64 encoding step)


    And I found it:

    (b64 (md5-with-charset "’" "windows-1250")) ;; => "aF1ZConzUtEGRN2YXaKpoQ=="
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥30 itest不允许查看成绩怎么办
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏