在Android 7.0上使用com.madgag.spongycastle:pkix:1.54.0.0
byte[] testData = "test data".getBytes("UTF_8");
Signature sign = Signature.getInstance("SHA256withRSA");
sign.initSign(privateKey);
sign.update(testData);
byte[] signature = sign.sign();
assert signature != null;
断言偶尔会失败。
任何线索为什么/何时 sign() 方法可以返回 null ?
堆栈跟踪:
(1) 尝试产生 CSR 时
Caused by java.lang.NullPointerException: data cannot be null
at org.spongycastle.asn1.ASN1BitString.(ASN1BitString.java:114)
at org.spongycastle.asn1.DERBitString.(DERBitString.java:83)
at org.spongycastle.asn1.DERBitString.(DERBitString.java:89)
at org.spongycastle.pkcs.PKCS10CertificationRequestBuilder.build(PKCS10CertificationRequestBuilder.java:149)
(2) 尝试验证测试签名时
Caused by java.security.SignatureException: java.lang.NullPointerException: Attempt to get length of null array
at com.android.org.conscrypt.OpenSSLSignature.engineVerify(OpenSSLSignature.java:258)
at java.security.Signature$Delegate.engineVerify(Signature.java:1275)
at java.security.Signature.verify(Signature.java:719)