TEC-007 java和php SHA-256 簽名和驗籤

因業務需求,php項目對接一個java的api 用到了SHA-256 簽名和延籤,和java同窗一塊兒研究了下php和java的SHA-256 簽名和延籤,分享下操做:
java demo以下:
public class SignUtils {
public static String toSign(String data, String salt) {
String s = data + salt;
byte[] sign;
try {
sign = SHA256.Digest.getInstance("SHA-256").digest(s.getBytes(Charset.forName("UTF-8")));
byte[] signBase = Base64.getEncoder().encode(sign);
return new String(signBase,Charset.forName("UTF-8"));
} catch (Exception e) {
throw new Exception("簽名異常!");
}
}
}php

String text = "lwy";
String salt = "lwy20190803";
String signData = SignUtils.toSign(text, salt);
System.ount.println(signData);java

輸出:QXulYxjtlJ1e/hEaeTKSjnSY8Xf37GuLRwAsnkEWN94=api

php demo以下:
class SignUtils{
public static function toSign($data,$salt){
$string = $data.$salt;
$data = hash('sha256', $string);
return base64_encode(hex2bin($data));
}
}
$sign = new SignUtils();
$salt = "lwy20190803";
$str = "lwy";
$signData = $sign->toSign($str,$salt);
echo "<br/><br/> 簽名串:",$str.$salt;
echo "<br/> 簽名結果:",$signData;
輸出結果:
簽名串:lwylwy20190803
簽名結果:QXulYxjtlJ1e/hEaeTKSjnSY8Xf37GuLRwAsnkEWN94=ide

本demo感謝亞博大哥一步步幫忙調試。調試

相關文章
相關標籤/搜索