duanmei1350 2014-06-11 14:15
浏览 491
已采纳

base64中的PHP SHA256校验和与Java的校验和相匹配

I have java program which calculates SHA-256 checksum as follows. For example, if I provide the input abcd123ABCD00-4000 it outputs 0QtyIu4B+lU+TLqM/zfJz5ULVpyXgfLRs5mKXCQvbHM= . It also matches the online check sum calculator.

However the PHP code I used won't match it.

PHP code :

$s = 'abcd123ABCD00-4000';
$signature = base64_encode(hash_hmac("sha256", $s, True)); 
print  base64_encode($signature); 

Output : TWpRM1pEWXpaVFl4TURoallqTXdPRFV6WVRrNU5XVTRNRGxoWkRJMU1XWTVNREk1TnpBeU4ySXhaR0psTW1ZMk16Y3hPRE01WldFelkySXhOalJrWXc9PQ==

Java Code :

private static String getSHA256Hash(String text) {


    String hash = null;
    MessageDigest md = null;


    try {

      md = MessageDigest.getInstance("SHA-256");

      md.update(text.getBytes("UTF-8"));

      byte[] shaDig = md.digest();
      // hash = Hex.encodeHexString(shaDig);
      hash = Base64.encodeBase64String(shaDig);

    } catch (NoSuchAlgorithmException ex) {


    } catch (UnsupportedEncodingException e) {

      e.printStackTrace();
    }
    return hash;

  }

Output : 0QtyIu4B+lU+TLqM/zfJz5ULVpyXgfLRs5mKXCQvbHM=

Here what I want is to get the equivalent result in PHP (i.e to change PHP code to match result of the Java program[ I won't able to change the Java code ]. Any help is appreciated

  • 写回答

1条回答 默认 最新

  • douchui3933 2014-06-11 14:24
    关注

    If you use hash() and base64 encode once you get the same result.

    $s = 'abcd123ABCD00-4000';
    $signature = base64_encode(hash("sha256", $s, True)); 
    print  $signature; // 0QtyIu4B+lU+TLqM/zfJz5ULVpyXgfLRs5mKXCQvbHM=
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私