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=="
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题