All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scodec.codecs.CertificateCodec.scala Maven / Gradle / Ivy

There is a newer version: 1.11.3
Show newest version
package scodec
package codecs

import java.io.ByteArrayInputStream
import java.security.cert.{Certificate, CertificateException, CertificateFactory}

import scodec.bits.BitVector

/**
 * Codec that supports encoding and decoding of [[java.security.cert.Certificate]]s using their default encoding.
 */
private[codecs] final class CertificateCodec(certType: String) extends Codec[Certificate] {

  def sizeBound = SizeBound.unknown

  def encode(cert: Certificate) =
    Attempt.successful(BitVector(cert.getEncoded))

  def decode(buffer: BitVector) = {
    try {
      val factory = CertificateFactory.getInstance(certType)
      val cert = factory.generateCertificate(new ByteArrayInputStream(buffer.toByteArray))
      Attempt.successful(DecodeResult(cert, BitVector.empty))
    } catch {
      case e: CertificateException =>
        Attempt.failure(Err("Failed to decode certificate: " + e.getMessage))
    }
  }

  override def toString = s"certificate($certType)"
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy