C# SHA-1 vs. PHP SHA-1…Different Results?

前端 未结 6 1596
醉话见心
醉话见心 2020-11-29 04:11

I am trying to calculate a SHA-1 Hash from a string, but when I calculate the string using php\'s sha1 function I get something different than when I try it in C#. I need C#

6条回答
  •  没有蜡笔的小新
    2020-11-29 05:07

    FWIW, I had a similar issue in Java. It turned out that I had to use "UTF-8" encoding to produce the same SHA1 hashes in Java as the sha1 function produces in PHP 5.3.1 (running on XAMPP Vista).

        private static String SHA1(final String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
            final MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(text.getBytes("UTF-8"));
            return new String(org.apache.commons.codec.binary.Hex.encodeHex(md.digest()));
        }
    

提交回复
热议问题