Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RsaPublicKey.scala « crypto « savage « getbootstrap « com « scala « main « src - github.com/twbs/savage.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a91c6ed6567e2b6a8270fd3aa81481e12c0a4eea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.getbootstrap.savage.crypto

import scala.util.Try
import java.security.KeyFactory
import java.security.PublicKey
import java.security.spec.X509EncodedKeySpec


sealed case class RsaPublicKey private(publicKey: PublicKey)

object RsaPublicKey {
  private val rsaKeyFactory = KeyFactory.getInstance("RSA") // Supported in all spec-compliant JVMs

  def fromX509Spec(keySpec: X509EncodedKeySpec): Try[RsaPublicKey] = Try{ rsaKeyFactory.generatePublic(keySpec) }.map{ new RsaPublicKey(_) }
  def fromPem(pem: String): Try[RsaPublicKey] = Try{ Pem.decodePublicKeyIntoSpec(pem) }.flatMap{ fromX509Spec(_) }
}