doushi2845 2014-06-03 05:29
浏览 109
已采纳

Perl - SHA1与PHP不匹配

My experience in perl is lacking. But, from what I read online and have seen googling this problem; perl's md5, sha1, sha256 ... should be returning the same hash as php or any other language, but aren't.

is there something I'm misunderstanding or missing?

Perl Code: (v5.14.2)

use Digest::SHA 'sha1_hex';
print Digest::SHA->sha1_hex("test");
# outputs e2412033b6d0070b931d01b0d1783b937608eb7f

PHP Code: (v5.4)

echo sha1("test");
//outputs: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
  • 写回答

1条回答 默认 最新

  • dongqin6926 2014-06-03 05:51
    关注

    Your program should look like:

    use Digest::SHA 'sha1_hex';
    print sha1_hex("test");
    

    or

    use Digest::SHA;
    print Digest::SHA::sha1_hex("test"); # note ::
    

    When you call Digest::SHA->sha1_hex("test");, you're using so called "indirect object" notation, which is equivalent to sha1_hex("Digest::SHA", "test")· That's why you get different hash:

    use Digest::SHA 'sha1_hex';
    say sha1_hex("Digest::SHAtest")
    # -> e2412033b6d0070b931d01b0d1783b937608eb7f
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?