我显然在这里遗漏了一些基本的东西。我有以下 Scala 代码:
package org.carlskii
import java.io._
import java.security.cert.CertificateFactory
import java.security.Security
import org.bouncycastle.jce.provider._
object Main extends App {
Security.addProvider(new BouncyCastleProvider)
val provider = new BouncyCastleProvider
val in = new FileInputStream("cert.cer")
val certificateFactory = CertificateFactory.getInstance("X509", provider)
val certificate = certificateFactory.generateCertificate(file)
println(certificate.getClass)
}
产生这个:
class org.bouncycastle.jce.provider.X509CertificateObject
所以我有一个充气城堡X509CertificateObject
对象。如果我调用该certificate.getPublicKey
方法,它会正确返回证书的公钥。
但是,如果我调用certificate.getSerialNumber
它会引发以下错误:
error: value getSerialNumber is not a member of java.security.cert.Certificate
println(certificate.getSerialNumber)
有趣的是,Scala 认为它是一个对象java.security.cert.Certificate
,而不是一个org.bouncycastle.jce.provider.X509CertificateObject
对象。为什么会这样?