您好,登錄后才能下訂單哦!
因業務需求,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("簽名異常!");
}
}
}
String text = "lwy";
String salt = "lwy20190803";
String signData = SignUtils.toSign(text, salt);
System.ount.println(signData);
輸出:QXulYxjtlJ1e/hEaeTKSjnSY8Xf37GuLRwAsnkEWN94=
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=
本demo感謝亞博大哥一步步幫忙調試。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。