如何用PHP重写下面这段java的Sha256WithRSA生成签名?
/**
* 签名
*
* @param: plain,明文
* @param: pkcs8 私钥
*/
public static byte[] sign(byte[] plain, String priKey) {
try {
PrivateKey privateKey = loadPriKey(priKey);
Signature signature = Signature.getInstance("Sha256WithRSA");
signature.initSign(privateKey);
signature.update(plain);
return signature.sign();
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
/**
* 加载私钥数据
*
* @param: pubKey,私钥文本
*/
public static PrivateKey loadPriKey(String priKey) {
try {
//1.读取私钥文件,获取私钥数据
byte[] bytesPrivateBase64 = readPriKey(priKey);
//2.对读取回来的数据进行Base64解码
byte[] bytesPrivate = Base64.getDecoder().decode(bytesPrivateBase64);
//3.把解码后的数据,重新封装成一个PrivateKey对象
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytesPrivate);
return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
private static byte[] readPriKey(String key) {
try {
BufferedReader bufferedReader = new BufferedReader(new StringReader(key));
String str = null;
StringBuilder stringBuilder = new StringBuilder();
while ((str = bufferedReader.readLine()) != null) {
if (str.contains("---")) {
continue;
}
stringBuilder.append(str);
}
return stringBuilder.toString().getBytes();
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}