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

im.actor.server.persist.MasterKeyRepo.scala Maven / Gradle / Ivy

The newest version!
package im.actor.server.persist

import java.security.MessageDigest

import com.google.common.primitives.Longs
import com.google.protobuf.ByteString
import im.actor.server.db.ActorPostgresDriver.api._
import im.actor.server.model.MasterKey

import scala.concurrent.ExecutionContext

final class MasterKeyTable(tag: Tag) extends Table[MasterKey](tag, "master_keys") {
  def authId = column[Long]("auth_id", O.PrimaryKey)

  def body = column[ByteString]("body")

  def * = (authId, body) <> ((MasterKey.apply _).tupled, MasterKey.unapply)
}

object MasterKeyRepo {
  val masterKeys = TableQuery[MasterKeyTable]

  val byIdC = Compiled { id: Rep[Long] ⇒
    masterKeys filter (_.authId === id)
  }

  def create(body: Array[Byte])(implicit ec: ExecutionContext) = {
    val md = MessageDigest.getInstance("SHA-256")
    val authId = Longs.fromByteArray(md.digest(body).take(java.lang.Long.BYTES))
    val masterKey = MasterKey(authId, ByteString.copyFrom(body))
    (masterKeys += masterKey) map (_ ⇒ masterKey)
  }

  def find(authId: Long) = byIdC(authId).result.headOption
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy