在 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)