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("签名异常!");
}
}
}

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感谢亚博大哥一步步帮忙调试。

上一篇:php mysqli_connect:客户端未知的身份验证方法[caching_sha2_password]


下一篇:对称与非对称加密算法