因业务需求,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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。