java.security.MessageDigest Scala Examples

The following examples show how to use java.security.MessageDigest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: IdGenUtilImpl.scala    From c4proto   with Apache License 2.0 5 votes vote down vote up
package ee.cone.c4actor

import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets.UTF_8
import java.security.MessageDigest
import java.util.Base64

import ee.cone.c4actor.Types.SrcId
import ee.cone.c4di.c4
import okio.ByteString

import scala.collection.immutable.TreeMap

@c4("RichDataCompApp") final case class IdGenUtilImpl()(
  proto: MessageDigest = MessageDigest.getInstance("MD5")
) extends IdGenUtil {
  private def md5(data: Array[Byte]*): String = {
    val d = proto.clone().asInstanceOf[MessageDigest] // much faster than getInstance("MD5")
    data.foreach{ bytes =>
      val l = bytes.length
      d.update((l>>24).toByte)
      d.update((l>>16).toByte)
      d.update((l>> 8).toByte)
      d.update((l>> 0).toByte)
      d.update(bytes)
    }
    Base64.getUrlEncoder.encodeToString(d.digest)
  }
  private def toBytes(value: String): Array[Byte] = value.getBytes(UTF_8)
  private def toBytes(value: Long): Array[Byte] =
    ByteBuffer.allocate(java.lang.Long.BYTES).putLong(value).array()

  def srcIdFromSrcIds(srcIdList: SrcId*): SrcId = md5(srcIdList.map(toBytes):_*)
  def srcIdFromStrings(stringList: String*): SrcId = md5(stringList.map(toBytes):_*)
  def srcIdFromSerialized(adapterId: Long, bytes: ByteString): SrcId =
    md5(toBytes(adapterId),bytes.toByteArray)
} 
Example 2
Source File: Hash.scala    From swave   with Mozilla Public License 2.0 5 votes vote down vote up
package swave.core.hash

import java.security.MessageDigest
import swave.core.Pipe
import swave.core.io.Bytes

object Hash extends HashTransformations

trait HashTransformations {

  final def md2[T: Bytes]: Pipe[T, T]    = digest(MessageDigest.getInstance("MD2"))
  final def md5[T: Bytes]: Pipe[T, T]    = digest(MessageDigest.getInstance("MD5"))
  final def sha1[T: Bytes]: Pipe[T, T]   = digest(MessageDigest.getInstance("SHA-1"))
  final def sha256[T: Bytes]: Pipe[T, T] = digest(MessageDigest.getInstance("SHA-256"))
  final def sha384[T: Bytes]: Pipe[T, T] = digest(MessageDigest.getInstance("SHA-384"))
  final def sha512[T: Bytes]: Pipe[T, T] = digest(MessageDigest.getInstance("SHA-512"))

  final def digest[T](md: MessageDigest)(implicit ev: Bytes[T]): Pipe[T, T] =
    Pipe[T]
      .fold(md) {
        new ((MessageDigest, T) => MessageDigest) {
          implicit def decorator(value: T): Bytes.Decorator[T] = Bytes.decorator(value)
          private[this] var array: Array[Byte]                 = _

          def apply(md: MessageDigest, bytes: T): MessageDigest = {
            val size =
              bytes.size match {
                case x if x > Int.MaxValue => sys.error("Cannot hash chunks with more than `Int.MaxValue` bytes")
                case x                     => x.toInt
              }
            if ((array eq null) || array.length < size) array = new Array[Byte](size)
            bytes.copyToArray(array, 0)
            md.update(array, 0, size)
            md
          }
        }
      }
      .map(md => ev(md.digest())) named "digest"
} 
Example 3
Source File: MD5Spec.scala    From swave   with Mozilla Public License 2.0 5 votes vote down vote up
package swave.docs

import java.nio.file.Files
import java.nio.charset.StandardCharsets._
import org.scalatest.{BeforeAndAfterAll, FreeSpec, Matchers}
import scala.concurrent.duration._
import swave.core.util._

class MD5Spec extends FreeSpec with Matchers with BeforeAndAfterAll {

  val testFileContent = "swave rocks!"

  val testPath = {
    val path = Files.createTempFile("md5-spec", ".tmp")
    Files.newBufferedWriter(path, UTF_8).append(testFileContent).close()
    path
  }

  "the examples in the `MD5` chapter should work as expected" - {

    "example-0" in {
      //#example-0
      import java.security.MessageDigest
      import java.io.File
      import scala.concurrent.Future
      import swave.core.io.files._   // enables `Spout.fromFile`
      import swave.compat.scodec._   // enables `ByteVector` support
      import swave.core._

      implicit val env = StreamEnv()

      def md5sum(file: File): Future[String] = {
        val md5 = MessageDigest.getInstance("MD5")
        Spout.fromFile(file)                                      // Spout[ByteVector]
          .fold(md5) { (m, bytes) => m.update(bytes.toArray); m } // Spout[MessageDigest]
          .flatMap(_.digest().iterator)                           // Spout[Byte]
          .map(_ & 0xFF)                                          // Spout[Int]
          .map("%02x" format _)                                   // Spout[String]
          .drainToMkString(limit = 32)                            // Future[String]
      }

      // don't forget to shutdown the StreamEnv at application exit with
      // env.shutdown()
      //#example-0

      try md5sum(testPath.toFile).await(2.seconds) shouldEqual "e1b2b603f9cca4a909c07d42a5788fe3"
      finally {
        Thread.sleep(100)
        env.shutdown()
      }
    }

    "example-1" in {
      //#example-1
      import java.io.File
      import scala.concurrent.Future
      import swave.core.io.files._   // enables `Spout.fromFile`
      import swave.core.hash._       // enables the `md5` transformation
      import swave.compat.scodec._   // enables `ByteVector` support
      import swave.core._

      implicit val env = StreamEnv()

      def md5sum(file: File): Future[String] =
        Spout.fromFile(file)            // Spout[ByteVector]
          .md5                          // Spout[ByteVector]
          .flattenConcat()              // Spout[Byte]
          .map(_ & 0xFF)                // Spout[Int]
          .map("%02x" format _)         // Spout[String]
          .drainToMkString(limit = 32)  // Future[String]

      // don't forget to shutdown the StreamEnv at application exit with
      // env.shutdown()
      //#example-1

      try md5sum(testPath.toFile).await(2.seconds) shouldEqual "e1b2b603f9cca4a909c07d42a5788fe3"
      finally {
        Thread.sleep(100)
        env.shutdown()
      }
    }
  }

  override protected def afterAll(): Unit = Files.delete(testPath)
} 
Example 4
Source File: ProtoUtil.scala    From cloudflow   with Apache License 2.0 5 votes vote down vote up
package cloudflow.streamlets.proto

import java.security.MessageDigest
import java.util.Base64
import scalapb.descriptors.Descriptor
import cloudflow.streamlets.SchemaDefinition

object ProtoUtil {
  val Format = "proto"

  def createSchemaDefinition(descriptor: Descriptor) = SchemaDefinition(
    name = descriptor.fullName,
    schema = descriptor.asProto.toProtoString,
    fingerprint = fingerprintSha256(descriptor),
    format = Format
  )

  private def fingerprintSha256(descriptor: Descriptor): String =
    Base64
      .getEncoder()
      .encodeToString(MessageDigest.getInstance("SHA-256").digest(descriptor.asProto.toProtoString.getBytes("UTF-8")))
} 
Example 5
Source File: HogConfig.scala    From hogzilla   with GNU General Public License v2.0 5 votes vote down vote up
package org.hogzilla.util

import java.security.MessageDigest
import org.apache.hadoop.hbase.util.Bytes
import javax.xml.bind.DatatypeConverter
import math._
import com.typesafe.config.Config
import scala.collection.mutable.HashSet



object HogConfig {
  
   
  
  def get(config:Config,key:String,valueType:String,default:Any):Any =
  {
    if(config==null)
      return default
    
    
      try {
        
        val value = config.getString(key)
        
        if(value.isEmpty())
          return default // Return default value
        
        println(f"Configuration: $key => $value")
       
        if(valueType.equals("Int"))
          value.toInt 
        else if(valueType.equals("Double"))
          value.toDouble 
        else if(valueType.equals("Long"))
          value.toLong 
        else if(valueType.equals("Set(Int)"))
        {
          val patternSet="Set\\(".r
          val patternSetEnd="\\)".r
          
          if(value.equals("Set()"))
            return Set()
          
          return (patternSetEnd replaceAllIn((patternSet replaceAllIn(value, "")),""))
                  .split(",").map({x => x.toInt}).toSet
        }
        else if(valueType.equals("Set(String)"))
        {
          val patternSet="Set\\(".r
          val patternSetEnd="\\)".r
          
          if(value.equals("Set()"))
            return Set()
          
          return (patternSetEnd replaceAllIn((patternSet replaceAllIn(value, "")),""))
                  .split(",").map({x => println(x.toString.trim()) ; x.toString.trim()}).toSet
        }
        else
          default // Create type first
          
      } catch {
        case t: Throwable => t.printStackTrace() 
        println(f"Problem parsing $key . Check if it is ok. Using default value")
        
        return default
      } 
  
  }
  
  def getInt(config:Config,key:String,default:Any):Int =
  {
    get(config,key,"Int",default).asInstanceOf[Int]
  }
  
  def getLong(config:Config,key:String,default:Any):Long =
  {
    get(config,key,"Long",default).asInstanceOf[Long]
  }
  
  def getDouble(config:Config,key:String,default:Any):Double =
  {
    get(config,key,"Double",default).asInstanceOf[Long]
  }
  
  def getSetInt(config:Config,key:String,default:Any):Set[Int] =
  {
    get(config,key,"Set(Int)",default).asInstanceOf[Set[Int]]
  }
  
  def getSetString(config:Config,key:String,default:Any):Set[String] =
  {
    get(config,key,"Set(String)",default).asInstanceOf[Set[String]]
  }
   

} 
Example 6
Source File: HogGeograph.scala    From hogzilla   with GNU General Public License v2.0 5 votes vote down vote up
package org.hogzilla.util

import java.security.MessageDigest
import org.apache.hadoop.hbase.util.Bytes
import javax.xml.bind.DatatypeConverter
import math._



object HogGeograph {
  
   val R = 6372.8 //radius in km
  
  def haversineDistance(lat1:Double, lon1:Double, lat2:Double, lon2:Double):Double =
  {
      val dLat=(lat2 - lat1).toRadians
      val dLon=(lon2 - lon1).toRadians

      val a = pow(sin(dLat/2),2) + pow(sin(dLon/2),2) * cos(lat1.toRadians) * cos(lat2.toRadians)
      val c = 2 * asin(sqrt(a))
      R * c    
  }
   
   
   def haversineDistanceFromStrings(coords1:String, coords2:String):Double =
	   {
		   try {
			   val coordsDouble1 = coords1.split(",").map({ x => x.toDouble })
				 val coordsDouble2 = coords2.split(",").map({ x => x.toDouble })

				 haversineDistance(coordsDouble1(0),coordsDouble1(1),coordsDouble2(0),coordsDouble2(1))
		   } catch {
		        case t: Throwable => // t.printStackTrace() 
             // Return a large distance 
             999999999D
		   }
	   }

} 
Example 7
Source File: MD5Utils.scala    From asura   with MIT License 5 votes vote down vote up
package asura.common.util

import java.security.MessageDigest

object MD5Utils {

  val HEX_ARRAY = "0123456789abcdef".toCharArray()

  def encodeToHex(plainText: String): String = {
    val md5Bytes = MessageDigest.getInstance("MD5").digest(plainText.getBytes)
    bytesToHex(md5Bytes)
  }

  def bytesToHex(bytes: Array[Byte]): String = {
    val hexChars = new Array[Char](bytes.length * 2)
    for (i <- 0 until bytes.length) {
      val v = bytes(i) & 0xFF
      hexChars(i * 2) = HEX_ARRAY(v >>> 4)
      hexChars(i * 2 + 1) = HEX_ARRAY(v & 0x0F)
    }
    new String(hexChars)
  }
} 
Example 8
Source File: PasswordMessage.scala    From skunk   with MIT License 5 votes vote down vote up
// Copyright (c) 2018-2020 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT

package skunk.net.message
import java.security.MessageDigest

// import scodec.codecs._

case class PasswordMessage(password: String)

object PasswordMessage {

  implicit val PasswordMessageFrontendMessage: FrontendMessage[PasswordMessage] =
    FrontendMessage.tagged('p') {
      utf8z.contramap[PasswordMessage](_.password)
    }

  // See https://www.postgresql.org/docs/9.6/protocol-flow.html#AEN113418
  // and https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/util/MD5Digest.java
  def md5(user: String, password: String, salt: Array[Byte]): PasswordMessage = {

    // Hash with this thing
    val md = MessageDigest.getInstance("MD5")

    // First round
    md.update(password.getBytes("UTF-8"))
    md.update(user.getBytes("UTF-8"))
    var hex = BigInt(1, md.digest).toString(16)
    while (hex.length < 32)
      hex = "0" + hex

    // Second round
    md.update(hex.getBytes("UTF-8"))
    md.update(salt)
    hex = BigInt(1, md.digest).toString(16)
    while (hex.length < 32)
      hex = "0" + hex

    // Done
    PasswordMessage("md5" + hex)

  }

} 
Example 9
Source File: Crypto.scala    From gospeak   with Apache License 2.0 5 votes vote down vote up
package gospeak.libs.scala

import java.nio.ByteBuffer
import java.security.{Key, MessageDigest, SecureRandom}
import java.util.Base64

import javax.crypto.spec.IvParameterSpec
import javax.crypto.{Cipher, KeyGenerator}

import scala.util.Try

object Crypto {
  private val charEnc = "UTF-8"
  private val aesKeyLength = 128
  private val aesTransformationString = "AES/CFB/NoPadding"

  def base64Encode(message: String): String =
    base64Encode(message.getBytes)

  def base64Encode(message: Array[Byte]): String =
    new String(Base64.getEncoder.encode(message), charEnc)

  def base64Decode(base64: String): Try[String] =
    base64DecodeBytes(base64).map(new String(_, charEnc))

  def base64DecodeBytes(base64: String): Try[Array[Byte]] =
    Try(Base64.getDecoder.decode(base64.getBytes(charEnc)))

  def md5(message: String): String =
    MessageDigest.getInstance("MD5").digest(message.getBytes).map("%02x".format(_)).mkString

  def sha1(message: String): String =
    MessageDigest.getInstance("SHA1").digest(message.getBytes).map("%02x".format(_)).mkString

  def secureRandom(): Try[Double] = {
    Try(SecureRandom.getInstance("SHA1PRNG")).map { sr =>
      val seedByteCount = 10
      val seed = sr.generateSeed(seedByteCount)
      sr.setSeed(seed)
      sr.nextDouble()
    }
  }

  final case class AesSecretKey(value: String) extends Key {
    override def getAlgorithm: String = "AES"

    override def getFormat: String = "RAW"

    override def getEncoded: Array[Byte] = base64DecodeBytes(value).get

    override def toString: String = "AesSecretKey(*****)"
  }

  final case class AesEncrypted(cipher: String)

  def aesGenerateKey(): Try[AesSecretKey] = for {
    // Generate an AES key of the desired length (in bits) using an AES KeyGenerator.
    keyGen <- Try(KeyGenerator.getInstance("AES"))
    _ <- Try(keyGen.init(aesKeyLength))
    secretKey <- Try(keyGen.generateKey)
  } yield AesSecretKey(base64Encode(secretKey.getEncoded))

  // adapted from https://www.owasp.org/index.php/Using_the_Java_Cryptographic_Extensions
  def aesEncrypt(message: String, secretKey: AesSecretKey): Try[AesEncrypted] = {
    for {
      // Get a Cipher instance of the desired algorithm, mode, and padding.
      aesCipherForEncryption <- Try(Cipher.getInstance(aesTransformationString))
      // Generate an initialization vector for our message of the same size as the Cipher's blocksize.
      iv <- Try(aesCipherForEncryption.getBlockSize).map(new Array[Byte](_))
      prng <- Try(new SecureRandom())
      _ = prng.nextBytes(iv)
      // Initialize the Cipher instance for encryption using the key and initialization vector.
      p <- Try(new IvParameterSpec(iv))
      _ <- Try(aesCipherForEncryption.init(Cipher.ENCRYPT_MODE, secretKey, p))
      // Use the Cipher to encrypt the message (after encoding it to a byte[] using the named Charset), and then append the encrypted data to the IV and Base64-encode the result.
      m <- Try(message.getBytes(charEnc))
      encrypted <- Try(aesCipherForEncryption.doFinal(m))
      cipherData = ByteBuffer.allocate(iv.length + encrypted.length)
      _ = cipherData.put(iv)
      _ = cipherData.put(encrypted)
    } yield AesEncrypted(base64Encode(cipherData.array))
  }

  def aesDecrypt(message: AesEncrypted, secretKey: AesSecretKey): Try[String] = {
    for {
      // Get a new Cipher instance of the same algorithm, mode, and padding used for encryption.
      aesCipherForDecryption <- Try(Cipher.getInstance(aesTransformationString))
      // Base64-decode and split the data into the IV and the encrypted data, and then initialize the cipher for decryption with the same key used for encryption (symmetric), the IV, and the encrypted data.
      cipherData <- base64DecodeBytes(message.cipher).map(ByteBuffer.wrap)
      iv = new Array[Byte](aesCipherForDecryption.getBlockSize)
      _ <- Try(cipherData.get(iv))
      encrypted = new Array[Byte](cipherData.remaining)
      _ <- Try(cipherData.get(encrypted))
      _ <- Try(aesCipherForDecryption.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)))
      // Use the Cipher to decrypt the data, convert it to a String using the named Charset, and display the message.
      decrypted <- Try(aesCipherForDecryption.doFinal(encrypted))
    } yield new String(decrypted, charEnc)
  }
} 
Example 10
Source File: package.scala    From swave   with Mozilla Public License 2.0 5 votes vote down vote up
package swave.core

import java.security.MessageDigest
import swave.core.io.Bytes

package object hash {

  implicit class RichBytesStreamOpsHash[T, S[X] <: StreamOps[X]](val underlying: S[T]) extends AnyVal {

    def md2(implicit ev: Bytes[T]): S[T]#Repr[T]    = underlying.via(Hash.md2[T])
    def md5(implicit ev: Bytes[T]): S[T]#Repr[T]    = underlying.via(Hash.md5[T])
    def sha1(implicit ev: Bytes[T]): S[T]#Repr[T]   = underlying.via(Hash.sha1[T])
    def sha256(implicit ev: Bytes[T]): S[T]#Repr[T] = underlying.via(Hash.sha256[T])
    def sha384(implicit ev: Bytes[T]): S[T]#Repr[T] = underlying.via(Hash.sha384[T])
    def sha512(implicit ev: Bytes[T]): S[T]#Repr[T] = underlying.via(Hash.sha512[T])

    def digest(md: MessageDigest)(implicit ev: Bytes[T]): S[T]#Repr[T] =
      underlying.via(Hash.digest(md))
  }

} 
Example 11
Source File: Crypto.scala    From akka-http-extensions   with Mozilla Public License 2.0 5 votes vote down vote up
package akka.http.extensions.security

import java.security.MessageDigest
import javax.crypto.Cipher
import javax.crypto.spec.{IvParameterSpec, SecretKeySpec}

import org.apache.commons.codec.binary.Base64

object CryptoConfig {

  implicit def fromString(secret:String): CryptoConfig = CryptoConfig(secret)
}

case class CryptoConfig( secret: String, transformation: String = "AES/CTR/NoPadding")

class CryptoException(val message: String = null, val throwable: Throwable = null)
  extends RuntimeException(message, throwable)

trait Encryption {

  def algorithm:String

  def encrypt(value: String, config:CryptoConfig): String

  def decrypt(value: String, config:CryptoConfig): String
}

object AES extends Encryption {

  val algorithm = "AES"

  protected def secretKeyWithSha256(privateKey: String) = {
    val messageDigest = MessageDigest.getInstance("SHA-256")
    messageDigest.update(privateKey.getBytes("utf-8"))
    // max allowed length in bits / (8 bits to a byte)
    val maxAllowedKeyLength = Cipher.getMaxAllowedKeyLength(algorithm) / 8
    val raw = messageDigest.digest().slice(0, maxAllowedKeyLength)
    new SecretKeySpec(raw, algorithm)
  }


  def encrypt(value: String, config:CryptoConfig): String = {
    val skeySpec = secretKeyWithSha256(config.secret)
    val cipher = Cipher.getInstance(config.transformation)
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec)
    val encryptedValue = cipher.doFinal(value.getBytes("utf-8"))
    Option(cipher.getIV) match {
      case Some(iv) => s"2-${Base64.encodeBase64String(iv ++ encryptedValue)}"
      case None => s"1-${Base64.encodeBase64String(encryptedValue)}"
    }
  }



  def decrypt(value: String, config:CryptoConfig): String =  value.indexOf("-") match
  {
    case sepIndex if sepIndex<0=> throw new CryptoException("Outdated AES version")
    case sepIndex=>
      val data = value.substring(sepIndex + 1, value.length())
      val version = value.substring(0, sepIndex)
      version match {
        case "1" => decryptAES1(data, config)
        case "2" => decryptAES2(data, config)
        case _ =>  throw new CryptoException("Unknown version")
        }
  }


  
  private def decryptAES2(value: String, config:CryptoConfig): String = {
    val cipher = Cipher.getInstance(config.transformation)
    val blockSize = cipher.getBlockSize
    val data = Base64.decodeBase64(value)
    val iv = data.slice(0, blockSize)
    val payload = data.slice(blockSize, data.size)
    val skeySpec = secretKeyWithSha256(config.secret)
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv))
    new String(cipher.doFinal(payload), "utf-8")
  }

} 
Example 12
Source File: CipherUtil.scala    From spark-highcharts   with Apache License 2.0 5 votes vote down vote up
package com.knockdata.spark.utils

import java.security.MessageDigest
import javax.crypto.Cipher
import javax.crypto.spec.{IvParameterSpec, SecretKeySpec}

import sun.misc.{BASE64Decoder, BASE64Encoder}

object CipherUtil {
  val cipherName = "AES/CBC/PKCS5Padding"

  val ivspec = new IvParameterSpec(Array[Byte](0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))

  def getKey(key: String): Array[Byte] = {
    val raw = MessageDigest.getInstance("MD5").digest(key.getBytes)
    raw
  }

  def encrypt(key: String, password: String): String = {
    val spec = new SecretKeySpec(getKey(key), "AES")
    val cipher = Cipher.getInstance(cipherName)
    cipher.init(Cipher.ENCRYPT_MODE, spec, ivspec)

    val encrypted = cipher.doFinal(password.getBytes("UTF8"))

    new BASE64Encoder().encode(encrypted)
  }

  def decrypt(key: String, encryptedPassword: String): String = {
    val spec = new SecretKeySpec(getKey(key), "AES")
    val cipher = Cipher.getInstance(cipherName)
    cipher.init(Cipher.DECRYPT_MODE, spec, ivspec)

    val encrypted = new BASE64Decoder().decodeBuffer(encryptedPassword)

    val decrypted = cipher.doFinal(encrypted)
    new String(decrypted, "UTF8")
  }
} 
Example 13
Source File: AuthUtil.scala    From shield   with MIT License 5 votes vote down vote up
package shield.aws

import java.nio.charset.StandardCharsets
import java.security.MessageDigest
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec

import org.apache.commons.codec.binary.Hex
import org.joda.time.format.DateTimeFormat
import org.joda.time.{DateTime, DateTimeZone}
import spray.http.HttpHeaders.RawHeader
import spray.http.HttpRequest


object HexBytesUtil {

  def hex2bytes(hex: String): Array[Byte] = {
    hex.replaceAll("[^0-9A-Fa-f]", "").sliding(2, 2).toArray.map(Integer.parseInt(_, 16).toByte)
  }

  def bytes2hex(bytes: Array[Byte], sep: Option[String] = None): String = {
    sep match {
      case None => bytes.map("%02x".format(_)).mkString
      case _ => bytes.map("%02x".format(_)).mkString(sep.get)
    }
  }
} 
Example 14
Source File: UtilCommands.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
import java.io.{BufferedInputStream, File}
import java.nio.file.{Files, Paths}
import java.security.MessageDigest
import scala.util.control.Breaks._

import javax.xml.bind.DatatypeConverter
import org.apache.commons.compress.archivers.ArchiveEntry
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream
import org.apache.commons.io.IOUtils
object UtilCommands {
  val OSX_NAME = "Mac OS X"

  val linuxSshpass = if (Files.exists(Paths.get("bin/utils/sshpass"))) "bin/utils/sshpass" else "sshpass"
  val osxSshpass = "/usr/local/bin/sshpass"

  val sshpass = if (isOSX) osxSshpass else linuxSshpass

  def isOSX = System.getProperty("os.name") == OSX_NAME

  def verifyComponentConfNotChanged(componentName:String, configFilePath:String, expectedHash:String) = {
    val confContent = UtilCommands.unTarGz("./components", componentName, configFilePath)
    UtilCommands.checksum(componentName, configFilePath, confContent, expectedHash)
  }

  def checksum(componentName:String, configFilePath:String, confContent:Array[Byte], expectedHash:String) = {
    val actualHash = MessageDigest.getInstance("MD5").digest(confContent)
    val actualHashStr = DatatypeConverter.printHexBinary(actualHash)
    if (!expectedHash.equalsIgnoreCase(actualHashStr))
      throw new Exception(s"$componentName configuration file $configFilePath has been changed, please change the template accordingly " +
        s"(the new digest is $actualHashStr)")
  }

  def unTarGz(rootFolder:String, componentName: String, configFilePath:String):Array[Byte] = {
    var tarArchiveInputStream:TarArchiveInputStream = null
    var bufferInputstream:BufferedInputStream = null
    val gzipCompressor:GzipCompressorInputStream = null
    var confContent: Array[Byte] = null
    try {
      val libDir = new File(rootFolder)
      val pathInput = libDir.listFiles().filter(file => file.getName.contains(componentName))
      val path = Paths.get(pathInput(0).getAbsolutePath)
      val bufferInputStream = new BufferedInputStream(Files.newInputStream(path))
      val gzipCompressor = new GzipCompressorInputStream(bufferInputStream)
      tarArchiveInputStream = new TarArchiveInputStream(gzipCompressor)

      var archiveEntry: ArchiveEntry = null
      archiveEntry = tarArchiveInputStream.getNextEntry
      if(archiveEntry.getName == "./")
        archiveEntry = tarArchiveInputStream.getNextEntry

      val extractFolder = archiveEntry.getName.replaceAll("^\\./","").split("/")(0)
      while (archiveEntry != null) {
        breakable {
        if (archiveEntry.getName.replaceAll("^\\./","") == s"$extractFolder/$configFilePath") {
            confContent = IOUtils.toByteArray(tarArchiveInputStream)
            break
          }
        }
        archiveEntry = tarArchiveInputStream.getNextEntry
      }
    }
    finally {
      if(tarArchiveInputStream != null)
        tarArchiveInputStream.close()
      if(bufferInputstream != null)
        bufferInputstream.close()
      if(gzipCompressor != null)
        gzipCompressor.close()
    }
    confContent
  }
} 
Example 15
Source File: render.scala    From odin   with Apache License 2.0 5 votes vote down vote up
package io.odin.extras.derivation

import io.odin.meta.Render
import magnolia.{CaseClass, Magnolia, Param, SealedTrait}
import java.security.MessageDigest
import java.math.BigInteger

object render {

  type Typeclass[A] = Render[A]

  def combine[A](ctx: CaseClass[Typeclass, A]): Typeclass[A] = value => {
    if (ctx.isValueClass) {
      ctx.parameters.headOption.fold("")(param => param.typeclass.render(param.dereference(value)))
    } else {
      val includeMemberNames = RenderUtils.includeMemberNames(ctx)

      val params = ctx.parameters
        .filterNot(RenderUtils.isHidden)
        .collect {
          case param if RenderUtils.isSecret(param) =>
            RenderUtils.renderParam(param.label, RenderUtils.SecretPlaceholder, includeMemberNames)

          case param if RenderUtils.shouldBeHashed(param) =>
            val label = s"${param.label} (sha256 hash)"
            val plaintext = param.typeclass.render(param.dereference(value))
            RenderUtils.renderParam(label, RenderUtils.sha256(plaintext), includeMemberNames)

          case RenderUtils.hasLengthLimit(param, limit) =>
            RenderUtils.renderWithLimit(param, value, limit, includeMemberNames)

          case p =>
            RenderUtils.renderParam(p.label, p.typeclass.render(p.dereference(value)), includeMemberNames)
        }

      s"${ctx.typeName.short}(${params.mkString(", ")})"
    }
  }

  def dispatch[A](ctx: SealedTrait[Typeclass, A]): Typeclass[A] = value => {
    ctx.dispatch(value)(sub => sub.typeclass.render(sub.cast(value)))
  }

  
  implicit def derive[A]: Typeclass[A] = macro Magnolia.gen[A]

}

private object RenderUtils {

  val SecretPlaceholder = "<secret>"

  @inline def includeMemberNames[A](ctx: CaseClass[Render, A]): Boolean =
    ctx.annotations
      .collectFirst {
        case rendered(v) => v
      }
      .getOrElse(true)

  @inline def isSecret[A](param: Param[Render, A]): Boolean =
    param.annotations.contains(secret())

  @inline def shouldBeHashed[A](param: Param[Render, A]): Boolean =
    param.annotations.contains(hash())

  @inline def isHidden[A](param: Param[Render, A]): Boolean =
    param.annotations.contains(hidden())

  @inline def renderParam(label: String, value: String, includeMemberName: Boolean): String =
    if (includeMemberName) s"$label = $value" else value

  def renderWithLimit[A](param: Param[Render, A], value: A, limit: Int, includeMemberNames: Boolean): String =
    param.dereference(value) match {
      case c: Iterable[_] =>
        val diff = c.iterator.length - limit
        val suffix = if (diff > 0) s"($diff more)" else ""
        val value = param.typeclass.render(c.take(limit).asInstanceOf[param.PType]) + suffix

        renderParam(param.label, value, includeMemberNames)

      case other =>
        renderParam(param.label, param.typeclass.render(other), includeMemberNames)
    }

  def sha256(value: String): String = {
    val digest = MessageDigest.getInstance("SHA-256")
    digest.update(value.getBytes(java.nio.charset.StandardCharsets.UTF_8))
    String.format("%064x", new BigInteger(1, digest.digest()))
  }

  object hasLengthLimit {
    def unapply[A](arg: Param[Render, A]): Option[(Param[Render, A], Int)] =
      arg.annotations.collectFirst {
        case length(limit) => (arg, limit)
      }
  }

} 
Example 16
Source File: AccountsDal.scala    From slick-akka-http-oauth2   with Apache License 2.0 5 votes vote down vote up
package persistence.dals

import java.security.MessageDigest

import persistence.entities.Account
import persistence.entities.SlickTables.AccountsTable
import slick.driver.H2Driver.api._
import slick.driver.JdbcProfile

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

trait AccountsDal extends BaseDalImpl[AccountsTable,Account] {
  def authenticate(email: String, password: String): Future[Option[Account]]
  def findByAccountId(id : Long) : Future[Option[Account]] = findById(id)
}

class AccountsDalImpl()(implicit override val db: JdbcProfile#Backend#Database) extends AccountsDal {
  private def digestString(s: String): String = {
    val md = MessageDigest.getInstance("SHA-1")
    md.update(s.getBytes)
    md.digest.foldLeft("") { (s, b) =>
      s + "%02x".format(if (b < 0) b + 256 else b)
    }
  }
  def authenticate(email: String, password: String): Future[Option[Account]] = {
    val hashedPassword = digestString(password)
    findByFilter( acc => acc.password === hashedPassword && acc.email === email).map(_.headOption)
  }
} 
Example 17
Source File: FileHelper.scala    From spark-nlp   with Apache License 2.0 5 votes vote down vote up
package com.johnsnowlabs.util

import java.io.{File, IOException}
import java.nio.charset.Charset
import java.nio.file.{Files, Paths}
import java.security.MessageDigest
import java.text.DecimalFormat

import org.apache.commons.io.FileUtils
object FileHelper {
  def writeLines(file: String, lines: Seq[String], encoding: String = "UTF-8"): Unit = {
    val writer = Files.newBufferedWriter(Paths.get(file), Charset.forName(encoding))
    try {
      var cnt = 0
      for (line <- lines) {
        writer.write(line)
        if (cnt > 0)
          writer.write(System.lineSeparator())
        cnt += 1
      }
    }
    catch {
      case ex: IOException =>
        ex.printStackTrace()
    }
    finally if (writer != null) writer.close()
  }

  def delete(file: String, throwOnError: Boolean = false): Unit = {
    val f = new File(file)
    if (f.exists()) {
      try {
        if (f.isDirectory)
          FileUtils.deleteDirectory(f)
        else
          FileUtils.deleteQuietly(f)
      }
      catch {
        case e: Exception =>
          if (throwOnError)
            throw e
          else
            FileUtils.forceDeleteOnExit(f)
      }
    }

  }

  def generateChecksum(path: String): String = {
    val arr = Files readAllBytes (Paths get path)
    val checksum = MessageDigest.getInstance("MD5") digest arr
    checksum.map("%02X" format _).mkString
  }

  def getHumanReadableFileSize(size: Long): String = {
    if (size <= 0) return "0"
    val units = Array[String]("B", "KB", "MB", "GB", "TB", "PB", "EB")
    val digitGroups = (Math.log10(size) / Math.log10(1024)).toInt
    new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units(digitGroups)
  }
} 
Example 18
Source File: PlatformTestHelpers.scala    From coursier   with Apache License 2.0 5 votes vote down vote up
package coursier

import java.math.BigInteger
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Paths}
import java.security.MessageDigest
import java.util.Locale

import coursier.cache.{Cache, MockCache}
import coursier.paths.Util
import coursier.util.{Sync, Task}

import scala.concurrent.{ExecutionContext, Future}

abstract class PlatformTestHelpers {

  private lazy val pool = Sync.fixedThreadPool(6)

  private val mockDataLocation = {
    val dir = Paths.get("modules/tests/metadata")
    assert(Files.isDirectory(dir))
    dir
  }

  val handmadeMetadataLocation = {
    val dir = Paths.get("modules/tests/handmade-metadata/data")
    assert(Files.isDirectory(dir))
    dir
  }

  val handmadeMetadataBase = handmadeMetadataLocation
    .toAbsolutePath
    .toFile // .toFile.toURI gives file:/ URIs, whereas .toUri gives file:/// (the former appears in some test fixtures now)
    .toURI
    .toASCIIString
    .stripSuffix("/") + "/"

  val writeMockData = Option(System.getenv("FETCH_MOCK_DATA"))
    .exists(s => s == "1" || s.toLowerCase(Locale.ROOT) == "true")

  val cache: Cache[Task] =
    MockCache.create[Task](mockDataLocation, pool = pool, writeMissing = writeMockData)
      .withDummyArtifact(_.url.endsWith(".jar"))

  val handmadeMetadataCache: Cache[Task] =
    MockCache.create[Task](handmadeMetadataLocation, pool = pool)

  val cacheWithHandmadeMetadata: Cache[Task] =
    MockCache.create[Task](mockDataLocation, pool = pool, Seq(handmadeMetadataLocation), writeMissing = writeMockData)
      .withDummyArtifact(_.url.endsWith(".jar"))

  def textResource(path: String)(implicit ec: ExecutionContext): Future[String] =
    Future {
      val p = Paths.get(path)
      val b = Files.readAllBytes(p)
      new String(b, StandardCharsets.UTF_8)
    }

  def maybeWriteTextResource(path: String, content: String): Unit = {
    val p = Paths.get(path)
    Util.createDirectories(p.getParent)
    Files.write(p, content.getBytes(StandardCharsets.UTF_8))
  }

  def sha1(s: String): String = {
    val md = MessageDigest.getInstance("SHA-1")
    val b = md.digest(s.getBytes(StandardCharsets.UTF_8))
    new BigInteger(1, b).toString(16)
  }

} 
Example 19
Source File: Auth.scala    From asyncdb   with Apache License 2.0 5 votes vote down vote up
package io.asyncdb
package netty
package mysql

import java.security.MessageDigest
import java.nio.charset.Charset
import io.netty.buffer._

object Auth {

  def nativePassword(
    seed: Array[Byte],
    password: String,
    charset: Charset
  ): Array[Byte] = {
    val md = MessageDigest.getInstance("SHA-1")
    val hash1 = md.digest(password.getBytes(charset))
    md.reset()
    val hash2 = md.digest(hash1)
    md.reset()
    md.update(seed)
    md.update(hash2)
    val digest = md.digest()
    (0 until digest.length) foreach { i =>
      digest(i) = (digest(i) ^ hash1(i)).toByte
    }
    digest
  }
} 
Example 20
Source File: SpringConfigController.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package controllers

import java.security.MessageDigest

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import com.google.common.base.Charsets
import controllers.actions.SecuredAuthContext
import domains.Key
import domains.config.{ConfigContext, ConfigService}
import env.IzanamiConfig
import org.apache.commons.codec.binary.Hex
import libs.logs.IzanamiLogger
import play.api.libs.json._
import play.api.mvc._
import libs.http.HttpContext
import libs.http.HttpContext

class SpringConfigController(izanamiConfig: IzanamiConfig,
                             AuthAction: ActionBuilder[SecuredAuthContext, AnyContent],
                             val cc: ControllerComponents)(implicit system: ActorSystem, R: HttpContext[ConfigContext])
    extends AbstractController(cc) {

  import libs.http._

  val digester = MessageDigest.getInstance("SHA-256")

  def byteToHexString(bytes: Array[Byte]): String = String.valueOf(Hex.encodeHex(bytes))

  def raw(rootKey: String, appName: String, profileName: String): Action[Unit] = AuthAction.asyncZio(parse.empty) {
    ctx =>
      val appConfigKey     = Key(s"$rootKey:$appName:$profileName:spring-config")
      val profileConfigKey = Key(s"$rootKey:spring-profiles:$profileName:spring-config")
      val globalConfigKey  = Key(s"$rootKey:spring-globals:spring-config")

      val host: String = ctx.request.headers
        .get(izanamiConfig.headerHost)
        .orElse(ctx.request.headers.get("Host"))
        .getOrElse("localhost:9000")

      val result = for {
        app     <- ConfigService.getById(appConfigKey)
        profile <- ConfigService.getById(profileConfigKey)
        global  <- ConfigService.getById(globalConfigKey)
      } yield {
        (app, profile, global) match {
          case (None, None, None) => NotFound(Json.obj("error" -> "No config found !"))
          case _ => {
            val propertySources = JsArray(
              Seq(
                app
                  .map(_.value)
                  .collect { case o: JsObject => o }
                  .map(
                    c =>
                      Json.obj(
                        "name"   -> s"${ctx.request.protocol}://$host/api/configs/$rootKey:$profileName:$appName:spring-config",
                        "source" -> c
                    )
                  ),
                profile
                  .map(_.value)
                  .collect { case o: JsObject => o }
                  .map(
                    c =>
                      Json.obj(
                        "name"   -> s"${ctx.request.protocol}://$host/api/configs/$rootKey:spring-profiles:$profileName:spring-config",
                        "source" -> c
                    )
                  ),
                global
                  .map(_.value)
                  .collect { case o: JsObject => o }
                  .map(
                    c =>
                      Json.obj(
                        "name"   -> s"${ctx.request.protocol}://$host/api/configs/$rootKey:spring-globals:spring-config",
                        "source" -> c
                    )
                  )
              ).flatten
            )
            val payload = Json.obj(
              "name"            -> s"$appName",
              "profiles"        -> Json.arr(s"://$profileName"),
              "label"           -> JsNull,
              "state"           -> JsNull,
              "propertySources" -> propertySources
            )
            IzanamiLogger.debug(s"Spring config request for $rootKey, $appName, $profileName: \n $payload")
            val version: String = byteToHexString(digester.digest(Json.stringify(payload).getBytes(Charsets.UTF_8)))
            Ok(payload ++ Json.obj("version" -> version))
          }
        }
      }
      result.mapError { _ =>
        InternalServerError("")
      }
  }
} 
Example 21
Source File: BasicAuthentication.scala    From sumobot   with Apache License 2.0 5 votes vote down vote up
package com.sumologic.sumobot.http_frontend.authentication
import java.security.MessageDigest

import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.model.{HttpRequest, HttpResponse}
import com.typesafe.config.Config

class BasicAuthentication(config: Config) extends HttpAuthentication {
  private val username = config.getString("username")
  private val password = config.getString("password")

  override def authentication(request: HttpRequest): AuthenticationResult = {
    request.header[`Authorization`] match {
      case Some(header) =>
        val receivedCredentials = BasicHttpCredentials(header.credentials.token())

        val authenticated = MessageDigest.isEqual(receivedCredentials.username.getBytes, username.getBytes) &&
          MessageDigest.isEqual(receivedCredentials.password.getBytes, password.getBytes)
        if (authenticated) AuthenticationSucceeded(AuthenticationInfo(Some(s"Logged in as: $username"), None, Seq.empty))
        else AuthenticationForbidden(HttpResponse(403))
      case None =>
        val header = `WWW-Authenticate`(List(HttpChallenge("basic", Some("SumoBot"))))
        AuthenticationForbidden(HttpResponse(401, headers = List(header)))
    }
  }
} 
Example 22
Source File: Utils.scala    From akka-pusher   with MIT License 5 votes vote down vote up
package com.github.dtaniwaki.akka_pusher

import java.math.BigInteger
import java.security.MessageDigest
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec

import akka.http.scaladsl.model.Uri

object Utils {
  val HEX = 16
  val LENGTH = 32

  def byteArrayToString(data: Array[Byte]): String = {
    val bigInteger = new BigInteger(1, data)
    var hash = bigInteger.toString(HEX)

    while (hash.length() < LENGTH) {
      hash = "0" + hash
    }

    hash
  }

  def md5(string: String): String = {
    val bytesOfMessage = string.getBytes("UTF-8")
    val md = MessageDigest.getInstance("MD5")
    val digest = md.digest(bytesOfMessage)
    byteArrayToString(digest)
  }

  def sha256(secret: String, string: String): String = {
    val signingKey = new SecretKeySpec(secret.getBytes(), "HmacSHA256")

    val mac = Mac.getInstance("HmacSHA256")
    mac.init(signingKey)

    val digest = mac.doFinal(string.getBytes("UTF-8"))

    val bigInteger = new BigInteger(1, digest)
    String.format("%0" + (digest.length << 1) + "x", bigInteger)
  }

  def normalizeQuery(query: Uri.Query): Uri.Query = {
    Uri.Query(query.map { case (k, v) => (k.toString.toLowerCase, v) }.sortBy(_._1): _*)
  }
} 
Example 23
Source File: sha1.scala    From libisabelle   with Apache License 2.0 5 votes vote down vote up
package isabelle


import java.io.{File => JFile, FileInputStream}
import java.security.MessageDigest


object SHA1
{
  final class Digest private[SHA1](val rep: String)
  {
    override def hashCode: Int = rep.hashCode
    override def equals(that: Any): Boolean =
      that match {
        case other: Digest => rep == other.rep
        case _ => false
      }
    override def toString: String = rep
  }

  private def make_result(digest: MessageDigest): Digest =
  {
    val result = new StringBuilder
    for (b <- digest.digest()) {
      val i = b.asInstanceOf[Int] & 0xFF
      if (i < 16) result += '0'
      result ++= Integer.toHexString(i)
    }
    new Digest(result.toString)
  }

  def fake(rep: String): Digest = new Digest(rep)

  def digest(file: JFile): Digest =
  {
    val stream = new FileInputStream(file)
    val digest = MessageDigest.getInstance("SHA")

    val buf = new Array[Byte](65536)
    var m = 0
    try {
      do {
        m = stream.read(buf, 0, buf.length)
        if (m != -1) digest.update(buf, 0, m)
      } while (m != -1)
    }
    finally { stream.close }

    make_result(digest)
  }

  def digest(path: Path): Digest = digest(path.file)

  def digest(bytes: Array[Byte]): Digest =
  {
    val digest = MessageDigest.getInstance("SHA")
    digest.update(bytes)

    make_result(digest)
  }

  def digest(bytes: Bytes): Digest = bytes.sha1_digest
  def digest(string: String): Digest = digest(Bytes(string))

  val digest_length: Int = digest("").rep.length
} 
Example 24
Source File: sha1.scala    From libisabelle   with Apache License 2.0 5 votes vote down vote up
package isabelle


import java.io.{File => JFile, FileInputStream}
import java.security.MessageDigest


object SHA1
{
  final class Digest private[SHA1](val rep: String)
  {
    override def hashCode: Int = rep.hashCode
    override def equals(that: Any): Boolean =
      that match {
        case other: Digest => rep == other.rep
        case _ => false
      }
    override def toString: String = rep
  }

  private def make_result(digest: MessageDigest): Digest =
  {
    val result = new StringBuilder
    for (b <- digest.digest()) {
      val i = b.asInstanceOf[Int] & 0xFF
      if (i < 16) result += '0'
      result ++= Integer.toHexString(i)
    }
    new Digest(result.toString)
  }

  def fake(rep: String): Digest = new Digest(rep)

  def digest(file: JFile): Digest =
  {
    val digest = MessageDigest.getInstance("SHA")

    using(new FileInputStream(file))(stream =>
    {
      val buf = new Array[Byte](65536)
      var m = 0
      try {
        do {
          m = stream.read(buf, 0, buf.length)
          if (m != -1) digest.update(buf, 0, m)
        } while (m != -1)
      }
    })

    make_result(digest)
  }

  def digest(path: Path): Digest = digest(path.file)

  def digest(bytes: Array[Byte]): Digest =
  {
    val digest = MessageDigest.getInstance("SHA")
    digest.update(bytes)

    make_result(digest)
  }

  def digest(bytes: Bytes): Digest = bytes.sha1_digest
  def digest(string: String): Digest = digest(Bytes(string))

  val digest_length: Int = digest("").rep.length
} 
Example 25
Source File: sha1.scala    From libisabelle   with Apache License 2.0 5 votes vote down vote up
package isabelle


import java.io.{File => JFile, FileInputStream}
import java.security.MessageDigest


object SHA1
{
  final class Digest private[SHA1](val rep: String)
  {
    override def hashCode: Int = rep.hashCode
    override def equals(that: Any): Boolean =
      that match {
        case other: Digest => rep == other.rep
        case _ => false
      }
    override def toString: String = rep
  }

  private def make_result(digest: MessageDigest): Digest =
  {
    val result = new StringBuilder
    for (b <- digest.digest()) {
      val i = b.asInstanceOf[Int] & 0xFF
      if (i < 16) result += '0'
      result ++= Integer.toHexString(i)
    }
    new Digest(result.toString)
  }

  def fake(rep: String): Digest = new Digest(rep)

  def digest(file: JFile): Digest =
  {
    val stream = new FileInputStream(file)
    val digest = MessageDigest.getInstance("SHA")

    val buf = new Array[Byte](65536)
    var m = 0
    try {
      do {
        m = stream.read(buf, 0, buf.length)
        if (m != -1) digest.update(buf, 0, m)
      } while (m != -1)
    }
    finally { stream.close }

    make_result(digest)
  }

  def digest(path: Path): Digest = digest(path.file)

  def digest(bytes: Array[Byte]): Digest =
  {
    val digest = MessageDigest.getInstance("SHA")
    digest.update(bytes)

    make_result(digest)
  }

  def digest(bytes: Bytes): Digest = bytes.sha1_digest
  def digest(string: String): Digest = digest(Bytes(string))

  val digest_length: Int = digest("").rep.length
} 
Example 26
Source File: FutureAndPromise.scala    From scala-tutorials   with MIT License 5 votes vote down vote up
package com.baeldung.scala.concurrency

import java.math.BigInteger
import java.net.URL
import java.security.MessageDigest

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext, Future, Promise}
import scala.util.control.NonFatal
import scala.util.{Failure, Success}

object ScalaAndPromise {
  def sampleFuture(): Future[Int] = Future {
    println("Long running computation started.")
    val result = {
      Thread.sleep(5)
      5
    }
    println("Our computation, finally finished.")
    result
  }

  type Name = String
  type Email = String
  type Password = String
  type Avatar = URL

  case class User(name: Name, email: Email, password: Password, avatar: Avatar)

  def exist(email: Email): Future[Boolean] = Future {
    Thread.sleep(100) // Call to the database takes time
    true
  }

  def md5hash(str: String): String =
    new BigInteger(1,
      MessageDigest
        .getInstance("MD5")
        .digest(str.getBytes)
    ).toString(16)

  def avatar(email: Email): Future[Avatar] = Future {
    Thread.sleep(200) // Call to the database takes time
    new Avatar("http://avatar.example.com/user/23k520f23f4.png")
  }

  def createUser(name: Name, email: Email, password: Password): Future[User] =
    for {
      _ <- exist(email)
      avatar <- avatar(email)
      hashedPassword = md5hash(password)
    } yield User(name, email, hashedPassword, avatar)

  def runByPromise[T](block: => T)(implicit ec: ExecutionContext): Future[T] = {
    val p = Promise[T]
    ec.execute { () =>
      try {
        p.success(block)
      } catch {
        case NonFatal(e) => p.failure(e)
      }
    }
    p.future
  }
}

object FutureAndPromiseApp extends App {

  import ScalaAndPromise._

  // Access to the value of Future by passing callback to the onComplete
  val userFuture: Future[User] = createUser("John", "[email protected]", "secret")
  userFuture.onComplete {
    case Success(user) =>
      println(s"User created: $user")
    case Failure(exception) =>
      println(s"Creating user failed due to the exception: $exception")
  }

  // Access to the value of Future by applying the result function on the Future value
  val user: User = Await.result(userFuture, Duration.Inf)

  // Forcing the Future value to be complete
  val completedFuture: Future[User] = Await.ready(userFuture, Duration.Inf)
  completedFuture.value.get match {
    case Success(user) =>
      println(s"User created: $user")
    case Failure(exception) =>
      println(s"Creating user failed due to the exception: $exception")
  }

} 
Example 27
Source File: Encoder.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.util

import java.security.MessageDigest
import java.util.Base64
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec

private[twitter4s] trait Encoder {

  def toHmacSha1(base: String, secret: String): String = {
    val HMAC_SHA1 = "HmacSHA1"
    val UTF8 = "UTF-8"
    val secretKeySpec = new SecretKeySpec(secret.getBytes(UTF8), HMAC_SHA1)
    val mac = Mac.getInstance(HMAC_SHA1)
    mac.init(secretKeySpec)
    val bytesToSign = base.getBytes(UTF8)
    val digest = mac.doFinal(bytesToSign)
    Base64.getEncoder.encodeToString(digest)
  }

  def toBase64(data: Array[Byte]): String =
    Base64.getEncoder.encodeToString(data)

  def toSha1(base: String): String = {
    val SHA1 = "SHA-1"
    val messageDigest = MessageDigest.getInstance(SHA1)
    val bytes = messageDigest.digest(base.getBytes)

    val stringBuffer = new StringBuffer
    bytes.foreach { byte =>
      stringBuffer.append(Integer.toString((byte & 0xff) + 0x100, 16).substring(1))
    }
    stringBuffer.toString
  }

} 
Example 28
Source File: FetchCache.scala    From coursier   with Apache License 2.0 5 votes vote down vote up
package coursier.internal

import java.io.File
import java.math.BigInteger
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path, Paths, StandardCopyOption}
import java.security.MessageDigest

import coursier.cache.CacheLocks
import coursier.core.{Classifier, Dependency, Repository, Type}
import coursier.params.ResolutionParams
import coursier.paths.CachePath
import dataclass.data

@data class FetchCache(base: Path) {

  def dir(key: FetchCache.Key): Path =
    base.resolve(s"${key.sha1.take(2)}/${key.sha1.drop(2)}")
  def resultFile(key: FetchCache.Key): Path =
    dir(key).resolve("artifacts")
  def lockFile(key: FetchCache.Key): Path =
    dir(key).resolve("lock")

  def read(key: FetchCache.Key): Option[Seq[File]] = {
    val resultFile0 = resultFile(key)
    if (Files.isRegularFile(resultFile0)) {
      val artifacts = Predef.augmentString(new String(Files.readAllBytes(resultFile0), StandardCharsets.UTF_8))
        .lines
        .map(_.trim)
        .filter(_.nonEmpty)
        .map(Paths.get(_))
        .toVector

      if (artifacts.forall(Files.isRegularFile(_)))
        Some(artifacts.map(_.toFile))
      else
        None
    } else
      None
  }

  def write(key: FetchCache.Key, artifacts: Seq[File]): Boolean = {
    val resultFile0 = resultFile(key)
    val tmpFile = CachePath.temporaryFile(resultFile0.toFile).toPath

    def doWrite(): Unit = {
      Files.write(tmpFile, artifacts.map(_.getAbsolutePath).mkString("\n").getBytes(StandardCharsets.UTF_8))
      Files.move(tmpFile, resultFile0, StandardCopyOption.ATOMIC_MOVE)
    }

    CacheLocks.withLockOr(
      base.toFile,
      resultFile0.toFile
    )(
      { doWrite(); true },
      Some(false)
    )
  }

}

object FetchCache {

  private[coursier] final case class Key(
    dependencies: Seq[Dependency],
    repositories: Seq[Repository],
    resolutionParams: ResolutionParams,

    // these 4 come from ResolutionParams, but are ordered here
    forceVersion: Seq[(coursier.core.Module, String)],
    properties: Seq[(String, String)],
    forcedProperties: Seq[(String, String)],
    profiles: Seq[String],

    cacheLocation: String,
    classifiers: Seq[Classifier],
    mainArtifacts: Option[Boolean],
    artifactTypesOpt: Option[Seq[Type]]
  ) {
    lazy val repr: String =
      productIterator.mkString("(", ", ", ")")
    lazy val sha1: String = {
      val md = MessageDigest.getInstance("SHA-1")
      val b = md.digest(repr.getBytes(StandardCharsets.UTF_8))
      val s = new BigInteger(1, b).toString(16)
      ("0" * (40 - s.length)) + s
    }
  }

} 
Example 29
Source File: FingerprintOps.scala    From metabrowse   with Apache License 2.0 5 votes vote down vote up
package metabrowse.cli

import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets
import java.security.MessageDigest

object FingerprintOps {

  def md5(string: String): String = {
    md5(ByteBuffer.wrap(string.getBytes(StandardCharsets.UTF_8)))
  }

  def md5(buffer: ByteBuffer): String = {
    val md = MessageDigest.getInstance("MD5")
    md.update(buffer)
    bytesToHex(md.digest())
  }

  private val hexArray = "0123456789ABCDEF".toCharArray
  def bytesToHex(bytes: Array[Byte]): String = {
    val hexChars = new Array[Char](bytes.length * 2)
    var j = 0
    while (j < bytes.length) {
      val v: Int = bytes(j) & 0xFF
      hexChars(j * 2) = hexArray(v >>> 4)
      hexChars(j * 2 + 1) = hexArray(v & 0x0F)
      j += 1
    }
    new String(hexChars)
  }

} 
Example 30
Source File: HashUtil.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.common.util

import java.math.BigInteger
import java.security.MessageDigest

object HashUtil {

  def hexSha1(content: String, salt: String = "0000"): String = hexHash("SHA1", content, salt)

  @inline
  def hex(content: String): String = hex(content.getBytes("UTF-8"))

  @inline
  def hex(content: Array[Byte]): String = new BigInteger(1, content).toString(16)

  def hexHash(algorithm: String, content: String, salt: String): String = {
    val messageDigest = MessageDigest.getInstance(algorithm)
    messageDigest.update(s"$content$salt".getBytes("UTF-8"))
    hex(messageDigest.digest())
  }
} 
Example 31
Source File: ModelConfigurationParser.scala    From modelmatrix   with Apache License 2.0 5 votes vote down vote up
package com.collective.modelmatrix

import java.nio.charset.CodingErrorAction
import java.security.MessageDigest
import java.util.function.BiConsumer

import com.typesafe.config.{Config, ConfigValue}

import scala.io.Codec
import scalaz.{Failure, Success, ValidationNel}

class ModelConfigurationParser(config: Config, path: String = "features") {

  type FeatureDefinition = (String, ValidationNel[String, ModelFeature])

  private lazy val configLines: Seq[(String, Int)] = {
    implicit val codec = Codec("UTF-8")
    codec.onMalformedInput(CodingErrorAction.REPLACE)
    codec.onUnmappableCharacter(CodingErrorAction.REPLACE)
    contentLines.zipWithIndex
  }

  // Try to find feature row index in original config if possible
  private def featureIndex(f: String): Int = {
    configLines.find(_._1.contains(f)).map(_._2).getOrElse(0)
  }

  private[this] val originUrl = config.origin().url()

  // configuration file as lines
  lazy val contentLines: Seq[String] = {
    if (originUrl != null) {
      scala.io.Source.fromURL(originUrl).getLines().toSeq
      // ideally this case below should never happen unless the Config passed in argument is not parsed from a file
    } else Seq.empty
  }

  // configuration file as a String
  lazy val content: String = contentLines.mkString(System.lineSeparator())

  // md5sum of the configuration content
  lazy val checksum: String = MessageDigest.getInstance("MD5").digest(content.getBytes).map("%02X".format(_)).mkString

  def features(): Seq[FeatureDefinition] = {
    val builder = collection.mutable.ListBuffer.empty[FeatureDefinition]

    config.getObject(path).forEach(new BiConsumer[String, ConfigValue] {
      def accept(t: String, u: ConfigValue): Unit = {
        val parsedFeature = ModelFeature.parse(t, u.atKey(t), t)
        builder += (t -> parsedFeature)
      }
    })

    builder.toSeq.sortBy {
      case (f, Success(feature)) => (true, featureIndex(feature.feature), feature.group, feature.feature)
      case (f, Failure(_)) => (false, featureIndex(f), "", f)
    }
  }
} 
Example 32
Source File: ModelDefinition.scala    From modelmatrix   with Apache License 2.0 5 votes vote down vote up
package com.collective.modelmatrix.catalog

import java.security.MessageDigest
import java.time.Instant

import org.slf4j.LoggerFactory

import scala.concurrent.ExecutionContext
import scalaz.{Tag, @@}

case class ModelDefinition(
  id: Int,
  name: Option[String],
  checksum: String,
  source: String,
  createdBy: String,
  createdAt: Instant,
  comment: Option[String],
  features: Int
)

class ModelDefinitions(val catalog: ModelMatrixCatalog)(implicit val ec: ExecutionContext @@ ModelMatrixCatalog) {
  private val log = LoggerFactory.getLogger(classOf[ModelDefinitions])

  import catalog.tables._
  import catalog.driver.api._

  private implicit val executionContext = Tag.unwrap(ec)

  // TODO: place this outside so that we have one implementation
  private def md5(s: String): String = {
    MessageDigest.getInstance("MD5").digest(s.getBytes).map("%02X".format(_)).mkString
  }

  private def q(definitions: catalog.modelDefinitionsT): DBIO[Seq[ModelDefinition]] = {
    val grouped = (for {
      m <- definitions
      f <- featureDefinitions if m.id === f.modelDefinitionId
    } yield (m, f)).groupBy(t => t._1.*)

    val counted = grouped.map { case (model, features) =>
      (model._1, model._2, model._3, model._4, model._5, model._6, model._7, features.length)
    }

    counted.result.map(_.map(ModelDefinition.tupled)).map(_.sortBy(_.id))
  }

  def all: DBIO[Seq[ModelDefinition]] = {
    log.trace(s"Get all model definitions")
    q(modelDefinitions)
  }

  def findById(id: Int): DBIO[Option[ModelDefinition]] = {
    q(modelDefinitions.filter(_.id === id)).map(_.headOption)
  }

  def findByContent(content: String): DBIO[Option[ModelDefinition]] = {
    val checksum = md5(content)
    findByChecksum(checksum)
  }

  private def findByChecksum(checksum: String): DBIO[Option[ModelDefinition]] = {
    q(modelDefinitions.filter(_.checksum === checksum)).map(_.headOption)
  }

  def list(name: Option[String] = None): DBIO[Seq[ModelDefinition]] = {
    var m: catalog.modelDefinitionsT = modelDefinitions
    name.foreach(n => m = m.filter(_.name like s"%$n%"))
    q(m)
  }

  def add(
    name: Option[String],
    source: String,
    createdBy: String,
    createdAt: Instant,
    comment: Option[String]
    ): DBIO[Int] = {

    log.trace(s"Add model definition. " +
      s"Created by: $createdBy @ $createdAt. " +
      s"Comment: ${comment.getOrElse("")}")

    (modelDefinitions returning modelDefinitions.map(_.id)) +=
      ((AutoIncId, name, md5(source), source, createdBy, createdAt, comment))
  }

} 
Example 33
Source File: HasBackupValidation.scala    From recogito2   with Apache License 2.0 5 votes vote down vote up
package controllers.document

import collection.JavaConverters._
import java.io.{ File, InputStream }
import java.math.BigInteger
import java.security.{ DigestInputStream, MessageDigest }
import java.util.zip.ZipFile
import controllers.HasConfig
import scala.concurrent.{ ExecutionContext, Future }
import scala.io.Source

object HasBackupValidation {

  class InvalidSignatureException extends RuntimeException

  class InvalidBackupException extends RuntimeException

  class DocumentExistsException extends RuntimeException

}

trait HasBackupValidation { self: HasConfig =>

  protected val ALGORITHM = "SHA-256"

  protected val SECRET = self.config.get[String]("play.http.secret.key")

  private def computeHash(stream: InputStream) = {
    val md = MessageDigest.getInstance(ALGORITHM)
    val din = new DigestInputStream(stream, md)

    // Weird, but din is pure side-effect - consume the stream & din computes the hash
    while (din.read() != -1) { }
    din.close()

    new BigInteger(1, md.digest()).toString(16)
  }

  def computeSignature(metadataHash: String, fileHashes: Seq[String], annotationsHash: String) = {
    val str = SECRET + metadataHash + fileHashes.mkString + annotationsHash
    val md = MessageDigest.getInstance(ALGORITHM).digest(str.getBytes)
    new BigInteger(1, md).toString(16)
  }

  def validateBackup(file: File)(implicit ctx: ExecutionContext): Future[Boolean] = Future {
    scala.concurrent.blocking {
      val zipFile = new ZipFile(file)
      val entries = zipFile.entries.asScala.toSeq.filter(!_.getName.startsWith("__MACOSX"))

      def hash(filename: String) = {
        val entry = entries.filter(_.getName == filename).head
        computeHash(zipFile.getInputStream(entry))
      }

      val expectedSignature = {
        val metadataHash = hash("metadata.json")
        val fileHashes = entries.filter(_.getName.startsWith("parts" + File.separator))
          .map(entry => hash(entry.getName))
        val annotationsHash = hash("annotations.jsonl")

        computeSignature(metadataHash, fileHashes, annotationsHash)
      }

      val storedSignature = {
        val signatureEntry = entries.filter(_.getName == "signature").head
        Source.fromInputStream(zipFile.getInputStream(signatureEntry), "UTF-8").getLines.mkString("\n")
      }

      expectedSignature == storedSignature
    }
  }

} 
Example 34
Source File: BackupWriter.scala    From recogito2   with Apache License 2.0 5 votes vote down vote up
package controllers.document

import controllers.HasConfig
import java.io.{File, FileInputStream, FileOutputStream, BufferedInputStream, ByteArrayInputStream, InputStream, PrintWriter}
import java.nio.file.Paths
import java.math.BigInteger
import java.security.{MessageDigest, DigestInputStream}
import java.util.UUID
import java.util.zip.{ZipEntry, ZipOutputStream}
import services.HasDate
import services.annotation.{Annotation, AnnotationService}
import services.document.{ExtendedDocumentMetadata, DocumentToJSON}
import services.generated.tables.records.{DocumentRecord, DocumentFilepartRecord}
import play.api.libs.json.Json
import play.api.libs.Files.TemporaryFileCreator
import scala.concurrent.{ExecutionContext, Future}
import storage.TempDir
import storage.uploads.Uploads

trait BackupWriter extends HasBackupValidation { self: HasConfig =>
  
  // Frontend annotation format
  import services.annotation.FrontendAnnotation._
  
  private val BUFFER_SIZE = 2048
  
  private def writeToZip(inputStream: InputStream, filename: String, zip: ZipOutputStream) = {
    zip.putNextEntry(new ZipEntry(filename))
     
    val md = MessageDigest.getInstance(ALGORITHM)    
    val in = new DigestInputStream(new BufferedInputStream(inputStream), md)

    var data= new Array[Byte](BUFFER_SIZE)
    var count: Int = 0

    while ({ count = in.read(data, 0, BUFFER_SIZE); count } > -1) {
      zip.write(data, 0, count)
    }

    in.close()
    zip.closeEntry()
    
    new BigInteger(1, md.digest()).toString(16)
  }
  
  def createBackup(doc: ExtendedDocumentMetadata)(implicit ctx: ExecutionContext, uploads: Uploads, 
      annotations: AnnotationService, tmpFile: TemporaryFileCreator): Future[File] = {
    
    def getFileAsStream(owner: String, documentId: String, filename: String) = {
      val dir = uploads.getDocumentDir(owner, documentId).get // Fail hard if the dir doesn't exist
      new FileInputStream(new File(dir, filename))
    }
    
    def getManifestAsStream() = {
      val manifest = "Recogito-Version: 2.0.1-alpha"
      new ByteArrayInputStream(manifest.getBytes)
    }
    
    def getMetadataAsStream(doc: ExtendedDocumentMetadata) = {
      
      // DocumentRecord JSON serialization
      import services.document.DocumentToJSON._
      
      val json = Json.prettyPrint(Json.toJson((doc.document, doc.fileparts)))
      new ByteArrayInputStream(json.getBytes)
    }
    
    def getAnnotationsAsStream(docId: String, annotations: Seq[Annotation], parts: Seq[DocumentFilepartRecord]): InputStream = {
      val path = Paths.get(TempDir.get()(self.config), s"${docId}_annotations.json")
      val tmp = tmpFile.create(path)
      val writer = new PrintWriter(path.toFile)
      annotations.foreach(a => writer.println(Json.stringify(Json.toJson(a))))
      writer.close()
      new FileInputStream(path.toFile)
    }
    
    Future {
      tmpFile.create(Paths.get(TempDir.get()(self.config), s"${doc.id}.zip"))
    } flatMap { zipFile =>
      val zipStream = new ZipOutputStream(new FileOutputStream(zipFile.path.toFile))

      writeToZip(getManifestAsStream(), "manifest", zipStream)
      val metadataHash = writeToZip(getMetadataAsStream(doc), "metadata.json", zipStream)

      val fileHashes = doc.fileparts.map { part =>
        writeToZip(getFileAsStream(doc.ownerName, doc.id, part.getFile), "parts" + File.separator + part.getFile, zipStream)
      }

      annotations.findByDocId(doc.id).map { annotations =>
        val annotationsHash = writeToZip(getAnnotationsAsStream(doc.id, annotations.map(_._1), doc.fileparts), "annotations.jsonl", zipStream)
        
        val signature = computeSignature(metadataHash, fileHashes, annotationsHash)
        writeToZip(new ByteArrayInputStream(signature.getBytes), "signature", zipStream)
        
        zipStream.close()
        zipFile.path.toFile
      }
    }
  }
  
} 
Example 35
Source File: HasEncryption.scala    From recogito2   with Apache License 2.0 5 votes vote down vote up
package services.user

import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
import org.apache.commons.codec.binary.Base64
import java.security.MessageDigest
import java.util.Arrays
import controllers.HasConfig

trait HasEncryption { self: HasConfig =>

  private val CIPHER = "AES/ECB/PKCS5Padding"

  private lazy val keySpec = self.config.getOptional[String]("recogito.email.key").flatMap { key =>
    if (key.isEmpty) {
      None
    } else {
      val md = MessageDigest.getInstance("SHA-1")
      val keyDigest = md.digest(key.getBytes)
      Some(new SecretKeySpec(Arrays.copyOf(keyDigest, 16), "AES"))
    }
  }

  def encrypt(plaintext: String) = keySpec match {
    case Some(spec) => {
      val cipher = Cipher.getInstance(CIPHER)
      cipher.init(Cipher.ENCRYPT_MODE, spec)
      Base64.encodeBase64String(cipher.doFinal(plaintext.getBytes("UTF-8")))
    }

    case None => plaintext
  }

  def decrypt(encrypted: String) = keySpec match {
    case Some(spec) => {
      val cipher = Cipher.getInstance(CIPHER)
      cipher.init(Cipher.DECRYPT_MODE, spec)
      new String(cipher.doFinal(Base64.decodeBase64(encrypted)))
    }

    case None => encrypted
  }

} 
Example 36
Source File: NsUUID.scala    From seals   with Apache License 2.0 5 votes vote down vote up
package dev.tauri.seals
package core

import java.util.UUID
import java.security.MessageDigest
import java.nio.{ ByteBuffer, Buffer }
import java.nio.charset.StandardCharsets

import scodec.bits.ByteVector

private[seals] object NsUUID {

  def uuid5(ns: UUID, name: String): UUID =
    uuid5bytes(ns, ByteBuffer.wrap(name.getBytes(StandardCharsets.UTF_8)))

  def uuid5bv(ns: UUID, name: ByteVector): UUID =
    uuid5bytes(ns, name.toByteBuffer)

  private def uuid5bytes(ns: UUID, name: ByteBuffer): UUID = {
    val buf = ByteBuffer.allocate(16) // network byte order by default
    putUUIDToBuf(ns, buf)
    (buf : Buffer).rewind()
    val h = sha1()
    h.update(buf)
    h.update(name)
    val arr: Array[Byte] = h.digest().take(16)
    arr(6) = (arr(6) & 0x0f).toByte // clear version
    arr(6) = (arr(6) | 0x50).toByte // version 5
    arr(8) = (arr(8) & 0x3f).toByte // clear variant
    arr(8) = (arr(8) | 0x80).toByte // variant RFC4122
    (buf : Buffer).rewind()
    buf.put(arr)
    (buf : Buffer).rewind()
    val msl = buf.getLong()
    val lsl = buf.getLong()
    new UUID(msl, lsl)
  }

  def uuid5nested(root: UUID, names: String*): UUID =
    names.foldLeft(root)(uuid5)

  def uuid5nestedBv(root: UUID, names: ByteVector*): UUID = {
    val buf = ByteVector.concat(names).toByteBuffer
    uuid5bytes(root, buf)
  }

  def uuid5nestedNsNm(name: String, ns1: UUID, nss: UUID*): UUID =
    uuid5(uuid5nestedNs(ns1, nss: _*), name)

  def uuid5nestedNs(ns1: UUID, nss: UUID*): UUID = {
    val buf = ByteBuffer.allocate(16)
    nss.foldLeft(ns1) { (st, u) =>
      putUUIDToBuf(u, buf)
      (buf : Buffer).rewind()
      val r = uuid5bytes(st, buf)
      (buf : Buffer).rewind()
      r
    }
  }

  private def putUUIDToBuf(u: UUID, buf: ByteBuffer): Unit = {
    buf.putLong(u.getMostSignificantBits)
    buf.putLong(u.getLeastSignificantBits)
  }

  def bvFromUUID(u: UUID): ByteVector = {
    val buf = ByteBuffer.allocate(16)
    putUUIDToBuf(u, buf)
    (buf : Buffer).rewind()
    ByteVector.view(buf)
  }

  private def sha1(): MessageDigest =
    MessageDigest.getInstance("SHA-1")
} 
Example 37
Source File: JHasher.scala    From tsec   with MIT License 5 votes vote down vote up
package tsec.hashing.jca

import java.security.MessageDigest

import cats.Applicative
import fs2.{Chunk, Pipe, Stream}
import tsec.hashing.{CryptoHash, CryptoHasher}

final class JHasher[F[_], A] private[jca] (val algorithm: String)(
    implicit F: Applicative[F]
) extends CryptoHasher[F, A] {

  private def genInstance = MessageDigest.getInstance(algorithm)

  def hash(bytes: Array[Byte]): F[CryptoHash[A]] =
    F.pure(CryptoHash[A](genInstance.digest(bytes)))

  
  def hashPipe: Pipe[F, Byte, Byte] =
    in =>
      Stream.suspend[F, Byte] {
        in.chunks
          .fold(genInstance) { (d, c) =>
            val bytes = c.toBytes
            d.update(bytes.values, bytes.offset, bytes.size)
            d
          }
          .flatMap { d =>
            Stream.chunk(Chunk.bytes(d.digest()))
          }
    }
} 
Example 38
Source File: ByteUtils.scala    From tsec   with MIT License 5 votes vote down vote up
package tsec.common

import java.security.MessageDigest

import cats.Eq
import cats.syntax.eq._

object ByteUtils {

  
  @inline final def contains[A: Eq](arr: Array[A], elem: A): Boolean =
    if (arr.isEmpty)
      false
    else {
      var i = 0
      while (i < arr.length) {
        if (arr(i) === elem)
          return true
        i += 1
      }
      false
    }
  @inline final def zeroByteArray(a: Array[Byte]): Unit =
    java.util.Arrays.fill(a, 0.toByte)

  @inline final def zeroCharArray(a: Array[Char]): Unit =
    java.util.Arrays.fill(a, 0.toChar)

  @inline final def intToBytes(a: Int): Array[Byte] =
    Array(
      (a >> 24).toByte,
      (a >> 16).toByte,
      (a >> 8).toByte,
      a.toByte
    )

  @inline final def unsafeBytesToInt(a: Array[Byte]): Int =
    a(0) << 24 | (a(1) & 0xFF) << 16 | (a(2) & 0xFF) << 8 | (a(3) & 0xFF)

  @inline final def bytesToInt(a: Array[Byte]): Option[Int] =
    if (a.length != Integer.BYTES) None
    else Some(unsafeBytesToInt(a))

  @inline final def longToBytes(x: Long): Array[Byte] =
    Array[Byte](
      (x >> 56).toByte,
      (x >> 48).toByte,
      (x >> 40).toByte,
      (x >> 32).toByte,
      (x >> 24).toByte,
      (x >> 16).toByte,
      (x >> 8).toByte,
      x.toByte
    )

  @inline def unsafeBytesToLong(a: Array[Byte]): Long =
    (a(0).toLong << 56) |
      ((a(1).toLong & 0xff) << 48) |
      ((a(2).toLong & 0xff) << 40) |
      ((a(3).toLong & 0xff) << 32) |
      ((a(4).toLong & 0xff) << 24) |
      ((a(5).toLong & 0xff) << 16) |
      ((a(6).toLong & 0xff) << 8) |
      a(7).toLong & 0xff

  @inline def bytesToLong(a: Array[Byte]): Option[Long] =
    if (a.length != java.lang.Long.BYTES) None
    else Some(unsafeBytesToLong(a))

} 
Example 39
Source File: MacTests.scala    From tsec   with MIT License 5 votes vote down vote up
package tsec

import java.security.MessageDigest

import cats.effect.IO
import tsec.common._
import tsec.keygen.symmetric.SymmetricKeyGen
import tsec.mac.jca._

class MacTests extends TestSpec {

  def macTest[A](
      implicit keyGen: SymmetricKeyGen[IO, A, MacSigningKey],
      pureinstance: JCAMessageAuth[IO, A]
  ): Unit = {
    behavior of pureinstance.algorithm

    //Todo: Should be with scalacheck
    it should "Sign then verify the same encrypted data properly" in {
      val dataToSign = "awwwwwwwwwwwwwwwwwwwwwww YEAH".utf8Bytes

      val res = for {
        k        <- keyGen.generateKey
        signed   <- pureinstance.sign(dataToSign, k)
        verified <- pureinstance.verifyBool(dataToSign, signed, k)
      } yield verified

      res.unsafeRunSync() mustBe true
    }

    it should "sign to the same message" in {
      val dataToSign = "awwwwwwwwwwwwwwwwwwwwwww YEAH".utf8Bytes

      val res: IO[Boolean] = for {
        k       <- keyGen.generateKey
        signed1 <- pureinstance.sign(dataToSign, k)
        signed2 <- pureinstance.sign(dataToSign, k)
      } yield MessageDigest.isEqual(signed1, signed2)
      res.unsafeRunSync() mustBe true
    }

    it should "not verify for different messages" in {
      val dataToSign = "awwwwwwwwwwwwwwwwwwwwwww YEAH".utf8Bytes
      val incorrect  = "hello my kekistanis".utf8Bytes

      val res = for {
        k       <- keyGen.generateKey
        signed1 <- pureinstance.sign(dataToSign, k)
        cond    <- pureinstance.verifyBool(incorrect, signed1, k)
      } yield cond

      res.unsafeRunSync() mustBe false
    }

    it should "not verify for different keys" in {

      val dataToSign = "awwwwwwwwwwwwwwwwwwwwwww YEAH".utf8Bytes

      val res = for {
        k       <- keyGen.generateKey
        k2      <- keyGen.generateKey
        signed1 <- pureinstance.sign(dataToSign, k)
        cond    <- pureinstance.verifyBool(dataToSign, signed1, k2)
      } yield cond

      res.unsafeRunSync() mustBe false

    }
  }

  macTest[HMACSHA1]
  macTest[HMACSHA256]
  macTest[HMACSHA384]
  macTest[HMACSHA512]

} 
Example 40
Source File: SCryptUtil.scala    From tsec   with MIT License 5 votes vote down vote up
package tsec.passwordhashers.jca

import java.security.MessageDigest

import cats.syntax.either._
import com.lambdaworks.codec.Base64
import com.lambdaworks.crypto.{SCrypt => JSCrypt}
import tsec.common.ManagedRandom


  def scrypt(passwd: Array[Byte], N: Int, r: Int, p: Int): String = {
    val salt = new Array[Byte](16)

    nextBytes(salt)
    val derived = JSCrypt.scrypt(passwd, salt, N, r, p, DerivedKeyLen)
    val params  = java.lang.Long.toString(log2(N) << 16L | r << 8 | p, 16)

    val sb = new java.lang.StringBuilder((salt.length + derived.length) * 2)
    sb.append(SCryptPrepend).append(params).append('$')
    sb.append(Base64.encode(salt)).append('$')
    sb.append(Base64.encode(derived))
    sb.toString
  }

} 
Example 41
Source File: HasherTest.scala    From tsec   with MIT License 5 votes vote down vote up
package tsec.hashing.bouncy

import java.security.MessageDigest

import cats.Id
import cats.effect.IO
import fs2._
import tsec.TestSpec
import tsec.common._
import tsec.hashing.{CryptoHashAPI, CryptoHasher}

class HasherTest extends TestSpec {
  val str     = "hello World"
  val strList = List("a", "a", "bcd")

  def hashTests[A](implicit P1: CryptoHasher[Id, A], P2: CryptoHasher[IO, A]): Unit = {
    s"A cryptographic hash function for ${P1.algorithm}" should s"generate an equal hash for two equal byte arrays" in {
      forAll { (s1: String, s2: String) =>
        val h1 = P1.hash(s1.utf8Bytes)
        val h2 = P1.hash(s2.utf8Bytes)

        MessageDigest.isEqual(h1, h2) mustBe s1 == s2

      }
    }

    it should "generate an equal hash for piped byte arrays" in {
      forAll { (s1: String, s2: String) =>
        val h1 = Stream.emits(s1.utf8Bytes).covary[IO].through(P2.hashPipe)
        val h2 = Stream.emits(s2.utf8Bytes).covary[IO].through(P2.hashPipe)

        h1.compile.toList.unsafeRunSync() == h2.compile.toList.unsafeRunSync() mustBe s1 == s2
      }
    }

  }

  hashTests[Keccak224]
  hashTests[Keccak256]
  hashTests[Keccak384]
  hashTests[Keccak512]
  hashTests[Whirlpool]
  hashTests[RipeMD128]
  hashTests[RipeMD160]
  hashTests[RipeMD256]
  hashTests[RipeMD320]
  hashTests[Streebog256]
  hashTests[Streebog512]
  hashTests[Blake2B160]
  hashTests[Blake2B256]
  hashTests[Blake2B384]
  hashTests[Blake2B512]

} 
Example 42
Source File: BouncyHasher.scala    From tsec   with MIT License 5 votes vote down vote up
package tsec.hashing.bouncy

import java.security.MessageDigest

import cats.Applicative
import fs2.{Chunk, Pipe, Stream}
import tsec.hashing.{CryptoHash, CryptoHasher}

final class BouncyHasher[F[_], A] private[bouncy](val algorithm: String)(
  implicit F: Applicative[F]
) extends CryptoHasher[F, A] {

  private def genInstance = MessageDigest.getInstance(algorithm, "BC")

  def hash(bytes: Array[Byte]): F[CryptoHash[A]] =
    F.pure(CryptoHash[A](genInstance.digest(bytes)))

  
  def hashPipe: Pipe[F, Byte, Byte] =
    in =>
      Stream.suspend[F, Byte] {
        in.chunks
          .fold(genInstance) { (d, c) =>
            val bytes = c.toBytes
            d.update(bytes.values, bytes.offset, bytes.size)
            d
          }
          .flatMap { d =>
            Stream.chunk(Chunk.bytes(d.digest()))
          }
      }
} 
Example 43
Source File: MessageSpec.scala    From roc   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package roc
package postgresql

import java.nio.charset.StandardCharsets
import java.security.MessageDigest
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Prop.forAll
import org.scalacheck.{Arbitrary, Gen}
import org.specs2._

final class MessagesSpec extends Specification with ScalaCheck { def is = s2"""

  PasswordMessage
    should MD5 encrypt a password with given salt           $pmEncrypt
                                                                            """

  val pmEncrypt = forAll { (user: String, pm: PasswordMessage, salt: Array[Byte]) =>
    val md = MessageDigest.getInstance("MD5")
    md.update((pm.password+ user).getBytes(StandardCharsets.UTF_8))
    val unsaltedHexStr = md.digest().map(x => "%02x".format(x.byteValue)).foldLeft("")(_ + _)
    val saltedBytes = unsaltedHexStr.getBytes ++ salt
    md.reset()
    md.update(saltedBytes)
    val passwd = md.digest().map(x => "%02x".format(x.byteValue)).foldLeft("md5")(_ + _)
    passwd must_== PasswordMessage.encryptMD5Passwd(user, pm.password, salt)
  }
  
  lazy val genByte: Gen[Byte] = arbitrary[Byte]
  lazy val genSalt: Gen[Array[Byte]] = Gen.containerOfN[Array, Byte](4, genByte)
  lazy val genPasswordMessage: Gen[PasswordMessage] = for {
    password    <-  arbitrary[String]
  } yield new PasswordMessage(password)
  implicit lazy val implicitPasswordMessage: Arbitrary[PasswordMessage] = 
    Arbitrary(genPasswordMessage)
} 
Example 44
Source File: Digest.scala    From Converter   with GNU General Public License v3.0 5 votes vote down vote up
package org.scalablytyped.converter.internal

import java.security.MessageDigest

final class Digest private (bytes: Array[Byte]) {
  lazy val hexString: String = Digest.asString(bytes)
}

object Digest {
  private def asString(buf: Array[Byte]): String = buf.map("%02x".format(_)).mkString

  trait Digestable[T] {
    def bytesFrom(t: T): Array[Byte]
  }

  object Digestable {
    def apply[T: Digestable]: Digestable[T] = implicitly

    implicit val StringDigestable: Digestable[String] =
      (t: String) => t.filterNot(_.isWhitespace).getBytes(constants.Utf8)
    implicit val ByteArrayDigestable: Digestable[Array[Byte]] =
      (bs: Array[Byte]) => bs
  }

  def of[T: Digestable](ts: scala.collection.Iterable[T]): Digest =
    new Digest(
      ts.foldLeft(MessageDigest.getInstance("MD5")) {
          case (digest, t) =>
            digest.update(Digestable[T].bytesFrom(t))
            digest
        }
        .digest(),
    )
} 
Example 45
Source File: HasherTest.scala    From tsec   with MIT License 5 votes vote down vote up
package tsec

import java.security.MessageDigest

import cats.effect.IO
import cats.Id
import fs2._
import tsec.common._
import tsec.hashing._
import tsec.hashing.jca._

class HasherTest extends TestSpec {

  def hashTests[A](implicit P1: CryptoHasher[Id, A], P2: CryptoHasher[IO, A]): Unit = {
    s"A cryptographic hash function for ${P1.algorithm}" should s"generate an equal hash for two equal byte arrays" in {
      forAll { (s1: String, s2: String) =>
        val h1 = P1.hash(s1.utf8Bytes)
        val h2 = P1.hash(s2.utf8Bytes)

        MessageDigest.isEqual(h1, h2) mustBe s1 == s2

      }
    }

    it should "generate an equal hash for piped byte arrays" in {
      forAll { (s1: String, s2: String) =>
        val h1 = Stream.emits(s1.utf8Bytes).covary[IO].through(P2.hashPipe)
        val h2 = Stream.emits(s2.utf8Bytes).covary[IO].through(P2.hashPipe)

        h1.compile.toList.unsafeRunSync() == h2.compile.toList.unsafeRunSync() mustBe s1 == s2
      }
    }

  }

  hashTests[MD5]
  hashTests[SHA1]
  hashTests[SHA256]
  hashTests[SHA512]
} 
Example 46
Source File: Hashes.scala    From matcher   with MIT License 5 votes vote down vote up
import java.io.InputStream

object Hashes {
  def mk(algorithm: String, stream: InputStream): Array[Byte] = {
    import java.security.{DigestInputStream, MessageDigest}
    val digest = MessageDigest.getInstance(algorithm)
    try {
      val dis    = new DigestInputStream(stream, digest)
      val buffer = new Array[Byte](8192)
      while (dis.read(buffer) >= 0) {}
      dis.close()
      digest.digest
    } finally { stream.close() }
  }
} 
Example 47
Source File: AvatarService.scala    From crm-seed   with Apache License 2.0 5 votes vote down vote up
package com.dataengi.crm.profiles.services

import java.net.URLEncoder._
import java.security.MessageDigest

import cats.instances.all._
import com.dataengi.crm.profiles.services.errors.ProfileServiceErrors
import com.google.inject.{Inject, Singleton}
import com.dataengi.crm.common.context.types._
import play.api.libs.ws.WSClient

import scala.concurrent.ExecutionContext

trait AvatarService {

  def retrieveURL(email: String): Or[Option[String]]

}

@Singleton
class AvatarServiceImplementation @Inject()(wSClient: WSClient,
                                            implicit val executionContext: ExecutionContext,
                                            settings: GravatarServiceSettings)
    extends AvatarService {

  override def retrieveURL(email: String): Or[Option[String]] = hash(email) match {
    case Some(hash) =>
      val url = settings.getUrl(hash)
      gerAvatarUrl(url)
    case None => ProfileServiceErrors.avatarCreatingUrlError(email).toErrorOr
  }

  private def gerAvatarUrl(url: String): Or[Option[String]] =
    wSClient
      .url(url)
      .get()
      .toOr
      .flatMap(response =>
        response.status match {
          case 200  => Option(url).toOr
          case code => ProfileServiceErrors.avatarReceivingError(code, url).toErrorOr
      })

  private def hash(email: String): Option[String] = {
    val s = email.trim.toLowerCase
    if (s.length > 0) {
      Some(MessageDigest.getInstance(GravatarService.MD5).digest(s.getBytes).map("%02x".format(_)).mkString)
    } else {
      None
    }
  }

}

object GravatarService {
  val InsecureURL = "http://www.gravatar.com/avatar/%s%s"
  val SecureURL   = "https://secure.gravatar.com/avatar/%s%s"
  val MD5         = "MD5"
}

case class GravatarServiceSettings(secure: Boolean = true, params: Map[String, String] = Map("d" -> "identicon")) {

  def getUrl(hash: String): String = {
    val encodedParams = params.map { p =>
      encode(p._1, "UTF-8") + "=" + encode(p._2, "UTF-8")
    }
    (if (secure) GravatarService.SecureURL else GravatarService.InsecureURL)
      .format(hash, encodedParams.mkString("?", "&", ""))
  }

} 
Example 48
Source File: Sig.scala    From devbox   with Apache License 2.0 5 votes vote down vote up
package devbox.common
import Util.permsetRw
import upickle.default.{ReadWriter, macroRW}
import java.security.MessageDigest

import os.{Path, StatInfo}

import scala.collection.mutable


  def compute(p: Path, buffer: Array[Byte], fileType: os.FileType) = {
    fileType match {
      case os.FileType.Other => None
      case os.FileType.SymLink => Some(Symlink(os.readLink(p).toString))
      case os.FileType.Dir => Some(Dir(os.perms(p).toInt()))
      case os.FileType.File =>
        val digest = MessageDigest.getInstance("MD5")
        val chunks = mutable.ArrayBuffer.empty[Bytes]
        var size = 0L
        for ((buffer, n) <- os.read.chunks(p, buffer)) {
          size += n
          digest.reset()
          digest.update(buffer, 0, n)

          chunks.append(new Bytes(digest.digest()))
        }
        Some(File(os.perms(p).toInt, chunks.toSeq, size))
    }
  }

  case class File(perms: os.PermSet, blockHashes: Seq[Bytes], size: Long) extends Sig
  object File{ implicit val rw: ReadWriter[File] = macroRW }

  case class Dir(perms: os.PermSet) extends Sig
  object Dir{ implicit val rw: ReadWriter[Dir] = macroRW }

  case class Symlink(dest: String) extends Sig
  object Symlink{ implicit val rw: ReadWriter[Symlink] = macroRW }

  implicit val rw: ReadWriter[Sig] = macroRW
} 
Example 49
Source File: PlatformSpecificMethods.scala    From fuuid   with MIT License 5 votes vote down vote up
package io.chrisdavenport.fuuid

import cats._
import cats.implicits._
import java.util.UUID
import java.security.MessageDigest

object PlatformSpecificMethods {
  def nameBased[F[_]]: (FUUID, String, ApplicativeError[F, Throwable]) => F[FUUID] = (namespace, name, AE) =>
    Either
      .catchNonFatal(
        MessageDigest
          .getInstance("SHA-1")
          .digest(
            uuidBytes(namespace) ++
              name.getBytes("UTF-8")
          )
      )
      .map { bs =>
        val cs = bs.take(16) // Truncate 160 bits (20 bytes) SHA-1 to fit into our 128 bits of space
        cs(6) = (cs(6) & 0x0f).asInstanceOf[Byte] 
        cs
      }
      .flatMap(
        bs =>
          Either.catchNonFatal {
            val bb = java.nio.ByteBuffer.allocate(java.lang.Long.BYTES)
            bb.put(bs, 0, 8)
            bb.flip
            val most = bb.getLong
            bb.flip
            bb.put(bs, 8, 8)
            bb.flip
            val least = bb.getLong
            FUUID.fromUUID(new UUID(most, least))
          }
      )
      .liftTo[F](AE)

  private def uuidBytes(fuuid: FUUID): Array[Byte] = {
    val bb = java.nio.ByteBuffer.allocate(2 * java.lang.Long.BYTES)
    val uuid = FUUID.Unsafe.toUUID(fuuid)
    bb.putLong(uuid.getMostSignificantBits)
    bb.putLong(uuid.getLeastSignificantBits)
    bb.array
  }
} 
Example 50
Source File: BouncyHash.scala    From iotchain   with MIT License 5 votes vote down vote up
package jbok.crypto.hash
import java.security.{MessageDigest, Security}

import org.bouncycastle.jcajce.provider.digest.Keccak
import org.bouncycastle.jce.provider.BouncyCastleProvider
import scodec.bits.ByteVector

object BouncyHash {
  if (Security.getProvider("BC") == null) {
    Security.addProvider(new BouncyCastleProvider())
  }

  def genInstance(algorithm: String): MessageDigest = MessageDigest.getInstance(algorithm, "BC")

  val sha256                    = genInstance("SHA-256")
  val ripemd160                 = genInstance("RipeMD160")
  def kec256(bytes: ByteVector) = new Keccak.Digest256().digest(bytes.toArray)
  def kec512(bytes: ByteVector) = new Keccak.Digest512().digest(bytes.toArray)
} 
Example 51
Source File: Encode.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.lf.archive.testing

import java.security.MessageDigest

import com.daml.lf.data.Ref.PackageId
import com.daml.lf.language.Ast.Package
import com.daml.lf.language.{LanguageMajorVersion, LanguageVersion}
import com.daml.daml_lf_dev.{DamlLf => PLF}

// Important: do not use this in production code. It is designed for testing only.
object Encode {

  private def encodePayloadOfVersion(
      idAndPkg: (PackageId, Package),
      version: LanguageVersion
  ): PLF.ArchivePayload = {

    val (pkgId, pkg) = idAndPkg
    val LanguageVersion(major, minor) = version

    major match {
      case LanguageMajorVersion.V1 =>
        PLF.ArchivePayload
          .newBuilder()
          .setMinor(minor.toProtoIdentifier)
          .setDamlLf1(new EncodeV1(minor).encodePackage(pkgId, pkg))
          .build()
      case _ =>
        sys.error(s"$version not supported")
    }
  }

  final def encodeArchive(pkg: (PackageId, Package), version: LanguageVersion): PLF.Archive = {

    val payload = encodePayloadOfVersion(pkg, version).toByteString
    val hash = PackageId.assertFromString(
      MessageDigest.getInstance("SHA-256").digest(payload.toByteArray).map("%02x" format _).mkString
    )

    PLF.Archive
      .newBuilder()
      .setHashFunction(PLF.HashFunction.SHA256)
      .setPayload(payload)
      .setHash(hash)
      .build()

  }

  case class EncodeError(message: String) extends RuntimeException

  private[testing] def unexpectedError(): Unit = throw EncodeError("unexpected error")

  private[testing] def expect(b: Boolean): Unit = if (!b) unexpectedError()
} 
Example 52
Source File: FlywayMigrationsSpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.platform.store

import java.math.BigInteger
import java.nio.charset.Charset
import java.security.MessageDigest

import com.daml.platform.store.FlywayMigrationsSpec._
import org.apache.commons.io.IOUtils
import org.flywaydb.core.api.configuration.FluentConfiguration
import org.flywaydb.core.api.migration.JavaMigration
import org.flywaydb.core.internal.resource.LoadableResource
import org.flywaydb.core.internal.scanner.{LocationScannerCache, ResourceNameCache, Scanner}
import org.scalatest.Matchers._
import org.scalatest.WordSpec

import scala.collection.JavaConverters._

// SQL MIGRATION AND THEIR DIGEST FILES SHOULD BE CREATED ONLY ONCE AND NEVER CHANGED AGAIN,
// OTHERWISE MIGRATIONS BREAK ON EXISTING DEPLOYMENTS!
class FlywayMigrationsSpec extends WordSpec {

  "Postgres flyway migration files" should {
    "always have a valid SHA-256 digest file accompanied" in {
      assertFlywayMigrationFileHashes(DbType.Postgres)
    }
  }

  "H2 database flyway migration files" should {
    "always have a valid SHA-256 digest file accompanied" in {
      assertFlywayMigrationFileHashes(DbType.H2Database)
    }
  }

}

object FlywayMigrationsSpec {

  private val digester = MessageDigest.getInstance("SHA-256")

  private def assertFlywayMigrationFileHashes(dbType: DbType): Unit = {
    val config = FlywayMigrations.configurationBase(dbType)
    val resourceScanner = scanner(config)
    val resources = resourceScanner.getResources("", ".sql").asScala.toSeq
    resources.size should be > 10

    resources.foreach { res =>
      val fileName = res.getFilename
      val expectedDigest =
        getExpectedDigest(fileName, fileName.dropRight(4) + ".sha256", resourceScanner)
      val currentDigest = getCurrentDigest(res, config.getEncoding)

      assert(
        currentDigest == expectedDigest,
        s"Digest of migration file $fileName has changed! It is NOT allowed to change neither existing sql migrations files nor their digests!"
      )
    }
  }

  private def scanner(config: FluentConfiguration) =
    new Scanner(
      classOf[JavaMigration],
      config.getLocations.toList.asJava,
      getClass.getClassLoader,
      config.getEncoding,
      new ResourceNameCache,
      new LocationScannerCache,
    )

  private def getExpectedDigest(
      sourceFile: String,
      digestFile: String,
      resourceScanner: Scanner[_],
  ) =
    IOUtils.toString(
      Option(resourceScanner.getResource(digestFile))
        .getOrElse(sys.error(s"""Missing sha-256 file $digestFile!
           |Are you introducing a new Flyway migration step?
           |You need to create a sha-256 digest file by either running:
           | - shasum -a 256 $sourceFile | awk '{print $$1}' > $digestFile (under the db/migration folder)
           | - or ledger/sandbox/src/main/resources/db/migration/recompute-sha256sums.sh
           |""".stripMargin))
        .read())

  private def getCurrentDigest(res: LoadableResource, encoding: Charset) = {
    val digest = digester.digest(IOUtils.toByteArray(res.read(), encoding))
    String.format(s"%0${digest.length * 2}x\n", new BigInteger(1, digest))
  }
} 
Example 53
Source File: Account.scala    From scala-oauth2-provider-example-skinny-orm   with MIT License 5 votes vote down vote up
package models

import java.security.MessageDigest

import org.joda.time.DateTime
import scalikejdbc._
import skinny.orm._

case class Account(id: Long, email: String, password: String, createdAt: DateTime)
object Account extends SkinnyCRUDMapper[Account] {

  override def defaultAlias = createAlias("a")
  val ownerAlias = createAlias("owner")

  override def extract(rs: WrappedResultSet, a: ResultName[Account]) = new Account(
    id = rs.get(a.id),
    email = rs.get(a.email),
    password = rs.get(a.password),
    createdAt = rs.get(a.createdAt)
  )

  private def digestString(s: String): String = {
    val md = MessageDigest.getInstance("SHA-1")
    md.update(s.getBytes)
    md.digest.foldLeft("") { (s, b) =>
      s + "%02x".format(if (b < 0) b + 256 else b)
    }
  }

  def authenticate(email: String, password: String)(implicit s: DBSession): Option[Account] = {
    val hashedPassword = digestString(password)
    val a = Account.defaultAlias
    Account.where(sqls.eq(a.email, email).and.eq(a.password, hashedPassword)).apply().headOption
  }
} 
Example 54
Source File: ArtifactsLock.scala    From coursier   with Apache License 2.0 5 votes vote down vote up
package coursier.install

import java.io.{File, FileInputStream}
import java.math.BigInteger
import java.security.MessageDigest

import cats.implicits._
import coursier.cache.internal.FileUtil
import coursier.util.Artifact
import dataclass.data

@data class ArtifactsLock (
  entries: Set[ArtifactsLock.Entry]
) {
  def repr: String =
    entries
      .toVector
      .map { e =>
        s"${e.url}#${e.checksumType}:${e.checksum}"
      }
      .sorted
      .mkString("\n")
}

object ArtifactsLock {

  @data class Entry(
    url: String,
    checksumType: String,
    checksum: String
  )

  def read(input: String): Either[String, ArtifactsLock] =
    input
      .split('\n')
      .map(_.trim)
      .zipWithIndex
      .filter(_._1.nonEmpty)
      .toList
      .traverse {
        case (line, lineNum) =>
          val idx = line.indexOf('#')
          if (idx < 0)
            Left(s"Malformed line ${lineNum + 1}")
          else {
            val url = line.take(idx)
            val checksumPart = line.drop(idx + 1)
            val idx0 = checksumPart.indexOf(':')
            if (idx0 < 0)
              Left(s"Malformed line ${lineNum + 1}")
            else {
              val checksumType = checksumPart.take(idx0)
              val checksum = checksumPart.drop(idx0 + 1)
              Right(Entry(url, checksumType, checksum))
            }
          }
      }
      .map { entries =>
        ArtifactsLock(entries.toSet)
      }

  private def sha1(f: File): String = {
    val md = MessageDigest.getInstance("SHA-1")

    var is: FileInputStream = null
    try {
      is = new FileInputStream(f)
      FileUtil.withContent(is, new FileUtil.UpdateDigest(md))
    } finally is.close()

    val b = md.digest()
    new BigInteger(1, b).toString(16)
  }

  def ofArtifacts(artifacts: Seq[(Artifact, File)]): ArtifactsLock = {

    val entries = artifacts.map {
      case (a, f) =>
        Entry(a.url, "SHA-1", sha1(f))
    }

    ArtifactsLock(entries.toSet)
  }
} 
Example 55
Source File: AttributesComputation.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.storage.attributes

import java.nio.file.{Files, Path}
import java.security.MessageDigest

import akka.http.scaladsl.model.HttpCharsets.`UTF-8`
import akka.http.scaladsl.model.MediaTypes.{`application/octet-stream`, `application/x-tar`}
import akka.http.scaladsl.model.{ContentType, MediaType, MediaTypes}
import akka.stream.Materializer
import akka.stream.scaladsl.{Keep, Sink}
import akka.util.ByteString
import cats.effect.Effect
import cats.implicits._
import ch.epfl.bluebrain.nexus.storage.File.{Digest, FileAttributes}
import ch.epfl.bluebrain.nexus.storage.StorageError.InternalError
import ch.epfl.bluebrain.nexus.storage._
import org.apache.commons.io.FilenameUtils

import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}

trait AttributesComputation[F[_], Source] {

  
  implicit def akkaAttributes[F[_]](implicit
      ec: ExecutionContext,
      mt: Materializer,
      F: Effect[F]
  ): AttributesComputation[F, AkkaSource] =
    (path: Path, algorithm: String) => {
      if (!Files.exists(path)) F.raiseError(InternalError(s"Path not found '$path'"))
      else
        Try(MessageDigest.getInstance(algorithm)) match {
          case Success(msgDigest) =>
            val isDir  = Files.isDirectory(path)
            val source = if (isDir) folderSource(path) else fileSource(path)
            source
              .alsoToMat(sinkSize)(Keep.right)
              .toMat(sinkDigest(msgDigest)) { (bytesF, digestF) =>
                (bytesF, digestF).mapN {
                  case (bytes, digest) => FileAttributes(path.toAkkaUri, bytes, digest, detectMediaType(path, isDir))
                }
              }
              .run()
              .to[F]
          case Failure(_)         => F.raiseError(InternalError(s"Invalid algorithm '$algorithm'."))
        }

    }
} 
Example 56
Source File: Crypto.scala    From tepkin   with Apache License 2.0 5 votes vote down vote up
package net.fehmicansaglam.tepkin.util

import java.security.MessageDigest
import javax.crypto.spec.{PBEKeySpec, SecretKeySpec}
import javax.crypto.{Mac, SecretKeyFactory}

import net.fehmicansaglam.bson.util.Codec

trait Crypto extends Codec {

  def sha1(value: String): String = {
    val digest = sha1(value.getBytes("UTF-8"))
    encodeBase64(digest)
  }

  def sha1(value: Array[Byte]): Array[Byte] = {
    MessageDigest.getInstance("SHA-1").digest(value)
  }

  def hmac(value: Array[Byte], key: String): Array[Byte] = {
    val signingKey = new SecretKeySpec(value, "HmacSHA1")
    val mac = Mac.getInstance("HmacSHA1")
    mac.init(signingKey)
    mac.doFinal(decodeUtf8(key))
  }

  def keyDerive(password: String, salt: Array[Byte], iterations: Int): Array[Byte] = {
    val spec = new PBEKeySpec(password.toCharArray, salt, iterations, 20 * 8 )
    val keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
    keyFactory.generateSecret(spec).getEncoded
  }

  def xor(as: Array[Byte], bs: Array[Byte]): Array[Byte] = {
    as.zip(bs).map { case (a, b) => (a ^ b).toByte }
  }
}

object Crypto extends Crypto 
Example 57
Source File: FileUtils.scala    From codepropertygraph   with Apache License 2.0 5 votes vote down vote up
import java.io.File
import java.nio.file.Files
import java.security.{DigestInputStream, MessageDigest}
import scala.collection.JavaConverters._

object FileUtils {

  def listFilesRecursively(roots: File*): Seq[File] = {
    roots.flatMap { root =>
      Files.walk(root.toPath).iterator.asScala.map(_.toFile).filter(!_.isDirectory)
    }
  }

  def md5(root: File): String =
    md5(List(root))

  def md5(roots: List[File]): String = {
    val md = MessageDigest.getInstance("MD5")
    roots.foreach { root =>
      Files.walk(root.toPath).filter(!_.toFile.isDirectory).forEach { path =>
        val dis = new DigestInputStream(Files.newInputStream(path), md)
        // fully consume the inputstream
        while (dis.available > 0) {
          dis.read
        }
        dis.close
      }
    }
    md.digest.map(b => String.format("%02x", Byte.box(b))).mkString
  }

}

object CodeGenGlobalState {
  // this is very ugly, but I can't define it like that in the build.sbt
  var lastMd5: String = ""
}

object MergeSchemaTaskGlobalState {
  // this is very ugly, but I can't define it like that in the build.sbt
  var lastMd5: String = ""
}

object GenerateProtobufTaskGlobalState {
  // this is very ugly, but I can't define it like that in the build.sbt
  var lastMd5: String = ""
}

object CopyLatestCpgProtoTaskGlobalState {
  // this is very ugly, but I can't define it like that in the build.sbt
  var lastMd5: String = ""
} 
Example 58
Source File: WardenHandlerMoP18414.scala    From wowchat   with GNU General Public License v3.0 5 votes vote down vote up
package wowchat.game.warden

import java.security.MessageDigest

import io.netty.buffer.{ByteBuf, PooledByteBufAllocator}
import wowchat.common.Packet

class WardenHandlerMoP18414(sessionKey: Array[Byte]) extends WardenHandler(sessionKey) {

  override protected val WARDEN_MODULE_LENGTH = 32

  override protected def getEncryptedMessageLength(msg: Packet): Int = {
    msg.byteBuf.readIntLE
  }

  override protected def formResponse(out: ByteBuf): ByteBuf = {
    val newLength = out.readableBytes + 4
    val withHeader = PooledByteBufAllocator.DEFAULT.buffer(newLength, newLength)
    withHeader.writeIntLE(out.readableBytes)
    withHeader.writeBytes(out)
    out.release
    withHeader
  }

  override protected def formCheatChecksRequestDigest(ret: ByteBuf, key: Array[Byte]): Unit = {
    val mdSHA1 = MessageDigest.getInstance("SHA1")
    mdSHA1.update(key)
    ret.writeBytes(mdSHA1.digest)

    val mdSHA256 = MessageDigest.getInstance("SHA-256")
    mdSHA256.update(key)
    ret.writeBytes(mdSHA256.digest)
  }
} 
Example 59
Source File: SHA1Randx.scala    From wowchat   with GNU General Public License v3.0 5 votes vote down vote up
package wowchat.game.warden

import java.security.MessageDigest

class SHA1Randx(sessionKey: Array[Byte]) {

  private val md = MessageDigest.getInstance("SHA1")
  private val keyHalfSize = sessionKey.length / 2

  md.update(sessionKey, 0, keyHalfSize)
  private val o1 = md.digest

  md.update(sessionKey, keyHalfSize, keyHalfSize)
  private val o2 = md.digest

  private var o0 = new Array[Byte](20)
  private var index = 0
  fillUp

  private def fillUp: Unit = {
    md.update(o1)
    md.update(o0)
    md.update(o2)

    o0 = md.digest
    index = 0
  }

  def generate(size: Int): Array[Byte] = {
    (0 until size).map(i => {
      if (index >= o0.length) {
        fillUp
      }
      val ret = o0(index)
      index += 1
      ret
    }).toArray
  }
} 
Example 60
Source File: Iroha.scala    From iroha-scala   with Apache License 2.0 5 votes vote down vote up
package net.cimadai.iroha

import java.security.MessageDigest

import Api.api.BaseObject.Value.{ValueInt, ValueString}
import Api.api._
import net.cimadai.iroha.MethodType.MethodType
import net.i2p.crypto.eddsa.spec.{EdDSANamedCurveTable, EdDSAPrivateKeySpec, EdDSAPublicKeySpec}
import net.i2p.crypto.eddsa.{EdDSAEngine, EdDSAPrivateKey, EdDSAPublicKey}



object MethodType {
  sealed abstract class MethodType(val name: String)
  case object ADD extends MethodType("add")
  case object TRANSFER extends MethodType("transfer")
  case object UPDATE extends MethodType("update")
  case object REMOVE extends MethodType("remove")
  case object CONTRACT extends MethodType("contract")
}

object Iroha {
  case class Ed25519KeyPair(privateKey: EdDSAPrivateKey, publicKey: EdDSAPublicKey)
  private val spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512)
  private def withEd25519[T](f: EdDSAEngine => T): T = {
    val signature = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm))
    f(signature)
  }

  def createKeyPair(): Ed25519KeyPair = {
    val seed = Array.fill[Byte](32) {0x0}
    new scala.util.Random(new java.security.SecureRandom()).nextBytes(seed)
    val sKey = new EdDSAPrivateKey(new EdDSAPrivateKeySpec(seed, spec))
    val vKey = new EdDSAPublicKey(new EdDSAPublicKeySpec(sKey.getAbyte, spec))
    Ed25519KeyPair(sKey, vKey)
  }

  def sign(keyPair: Ed25519KeyPair, message: Array[Byte]): Array[Byte] = {
    withEd25519 { ed25519 =>
      ed25519.initSign(keyPair.privateKey)
      ed25519.signOneShot(message)
    }
  }

  def verify(keyPair: Ed25519KeyPair, signature: Array[Byte], message: Array[Byte]): Boolean = {
    withEd25519 { ed25519 =>
      ed25519.initVerify(keyPair.publicKey)
      ed25519.verifyOneShot(message, signature)
    }
  }

  def createTransaction(methodType: MethodType, publicKeyBase64: String): Transaction = {
    Transaction(
      txSignatures = Seq.empty,
      `type` = methodType.name,
      senderPubkey = publicKeyBase64,
      timestamp = System.currentTimeMillis(),
      asset = None,
      simpleAsset = None,
      domain = None,
      account = None,
      peer = None
    )
  }

  def createAccountQuery(publicKeyBase64: String): Query = {
    Query(
      `type` = "account",
      senderPubkey = publicKeyBase64
    )
  }

  def createAssetQuery(publicKeyBase64: String, assetName: String): Query = {
    Query(
      `type` = "asset",
      value = Map("name" -> BaseObject(ValueString(assetName))),
      senderPubkey = publicKeyBase64
    )
  }

  def createAccount(publicKeyBase64: String, accountName: String, assetNames: Seq[String]): Account = {
    Account(publicKey = publicKeyBase64, name = accountName, assets = assetNames)
  }

  def createAsset(assetName: String, amount: Long): Asset = {
    Asset(name = assetName, value = Map("value" -> BaseObject(ValueInt(amount))))
  }
} 
Example 61
Source File: Crypto.scala    From akka-http-session   with Apache License 2.0 5 votes vote down vote up
package com.softwaremill.session

import java.security.MessageDigest
import java.util

import com.softwaremill.session.SessionUtil._
import javax.crypto.spec.SecretKeySpec
import javax.crypto.{Cipher, Mac}

object Crypto {
  def sign_HmacSHA1_hex(message: String, secret: String): String = {
    val key = secret.getBytes("UTF-8")
    val mac = Mac.getInstance("HmacSHA1")
    mac.init(new SecretKeySpec(key, "HmacSHA1"))
    toHexString(mac.doFinal(message.getBytes("utf-8")))
  }

  def sign_HmacSHA256_base64_v0_5_2(message: String, secret: String): String = {
    val key = secret.getBytes("UTF-8")
    val mac = Mac.getInstance("HmacSHA256")
    mac.init(new SecretKeySpec(key, "HmacSHA256"))
    SessionUtil.toBase64_v0_5_2(mac.doFinal(message.getBytes("utf-8")))
  }

  def encrypt_AES(value: String, secret: String): String = {
    val raw = util.Arrays.copyOf(secret.getBytes("utf-8"), 16)
    val skeySpec = new SecretKeySpec(raw, "AES")
    val cipher = Cipher.getInstance("AES")
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec)
    toHexString(cipher.doFinal(value.getBytes("utf-8")))
  }

  def decrypt_AES(value: String, secret: String): String = {
    val raw = util.Arrays.copyOf(secret.getBytes("utf-8"), 16)
    val skeySpec = new SecretKeySpec(raw, "AES")
    val cipher = Cipher.getInstance("AES")
    cipher.init(Cipher.DECRYPT_MODE, skeySpec)
    new String(cipher.doFinal(hexStringToByte(value)))
  }

  def hash_SHA256(value: String): String = {
    val digest = MessageDigest.getInstance("SHA-256")
    toHexString(digest.digest(value.getBytes("UTF-8")))
  }
} 
Example 62
Source File: ImmutableMigrationsSpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.on.sql

import java.io.{BufferedReader, FileNotFoundException}
import java.math.BigInteger
import java.nio.charset.Charset
import java.security.MessageDigest
import java.util

import com.daml.ledger.on.sql.ImmutableMigrationsSpec._
import org.flywaydb.core.Flyway
import org.flywaydb.core.api.configuration.FluentConfiguration
import org.flywaydb.core.internal.resource.LoadableResource
import org.flywaydb.core.internal.scanner.{LocationScannerCache, ResourceNameCache, Scanner}
import org.scalatest.Matchers._
import org.scalatest.WordSpec

import scala.collection.JavaConverters._

class ImmutableMigrationsSpec extends WordSpec {
  "migration files" should {
    "never change, according to their accompanying digest file" in {
      val configuration = Flyway
        .configure()
        .locations(s"classpath:/$migrationsResourcePath")
      val resourceScanner = flywayScanner(configuration)
      val resources = resourceScanner.getResources("", ".sql").asScala.toSeq
      resources.size should be >= 3

      resources.foreach { resource =>
        val migrationFile = resource.getRelativePath
        val digestFile = migrationFile + ".sha256"
        val expectedDigest = readExpectedDigest(migrationFile, digestFile, resourceScanner)
        val currentDigest = computeCurrentDigest(resource, configuration.getEncoding)
        assert(
          currentDigest == expectedDigest,
          s"""The contents of the migration file "$migrationFile" have changed! Migrations are immutable; you must not change their contents or their digest.""",
        )
      }
    }
  }
}

object ImmutableMigrationsSpec {
  private val migrationsResourcePath = "com/daml/ledger/on/sql/migrations"
  private val hashMigrationsScriptPath = "ledger/ledger-on-sql/hash-migrations.sh"

  private def flywayScanner(configuration: FluentConfiguration) =
    new Scanner(
      classOf[Object],
      util.Arrays.asList(configuration.getLocations: _*),
      getClass.getClassLoader,
      configuration.getEncoding,
      new ResourceNameCache,
      new LocationScannerCache,
    )

  private def readExpectedDigest(
      sourceFile: String,
      digestFile: String,
      resourceScanner: Scanner[_],
  ): String = {
    val resource = Option(resourceScanner.getResource(digestFile))
      .getOrElse(throw new FileNotFoundException(
        s"""\"$digestFile\" is missing. If you are introducing a new Flyway migration step, you need to create an SHA-256 digest file by running $hashMigrationsScriptPath."""))
    new BufferedReader(resource.read()).readLine()
  }

  private def computeCurrentDigest(resource: LoadableResource, encoding: Charset): String = {
    val sha256 = MessageDigest.getInstance("SHA-256")
    new BufferedReader(resource.read())
      .lines()
      .forEach(line => sha256.update((line + "\n").getBytes(encoding)))
    val digest = sha256.digest()
    String.format(s"%0${digest.length * 2}x", new BigInteger(1, digest))
  }
} 
Example 63
Source File: PasswordUtil.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.security

import java.security.MessageDigest
import scala.util.Try

import sun.misc.{BASE64Decoder, BASE64Encoder}


  def hash(password: String): String = {
    // Salt generation 64 bits long
    val salt = new Array[Byte](SALT_LENGTH)
    new java.util.Random().nextBytes(salt)
    hash(password, salt)
  }

  private def hash(password: String, salt: Array[Byte]): String = {
    val digest = MessageDigest.getInstance("SHA-1")
    digest.reset()
    digest.update(salt)
    var input = digest.digest(password.getBytes("UTF-8"))
    digest.reset()
    input = digest.digest(input)
    val withSalt = salt ++ input
    base64Encode(withSalt)
  }

  private def base64Encode(data: Array[Byte]): String = {
    val endecoder = new BASE64Encoder()
    endecoder.encode(data)
  }

  private def base64Decode(data: String): Array[Byte] = {
    val decoder = new BASE64Decoder()
    decoder.decodeBuffer(data)
  }

  // scalastyle:off println
  private def help() = {
    Console.println("usage: gear org.apache.gearpump.security.PasswordUtil -password " +
      "<your password>")
  }

  def main(args: Array[String]): Unit = {
    if (args.length != 2 || args(0) != "-password") {
      help()
    } else {
      val pass = args(1)
      val result = hash(pass)
      Console.println("Here is the hashed password")
      Console.println("==============================")
      Console.println(result)
    }
  }
  // scalastyle:on println
} 
Example 64
Source File: ByteArrays.scala    From scala-stellar-sdk   with Apache License 2.0 5 votes vote down vote up
package stellar.sdk.util

import java.security.MessageDigest

import okio.ByteString
import org.apache.commons.codec.binary.Base64

import scala.annotation.tailrec
import scala.language.implicitConversions

object ByteArrays {

  object Implicits {
    implicit def byteArrayToByteString(arr: Array[Byte]): ByteString =
      new ByteString(arr)

    implicit def byteStringToByteArray(byteString: ByteString): Array[Byte] =
      byteString.toByteArray
  }

  def paddedByteArray(bs: Array[Byte], length: Int): Array[Byte] = {
    val padded = Array.ofDim[Byte](math.max(length, bs.length))
    System.arraycopy(bs, 0, padded, 0, bs.length)
    padded
  }

  def paddedByteArray(s: String, length: Int): Array[Byte] = paddedByteArray(s.getBytes("US-ASCII"), length)

  def paddedByteArrayToString(bs: Array[Byte]): String = new String(bs, "US-ASCII").split("\u0000")(0)

  def trimmedByteArray(bs: Array[Byte]): Seq[Byte] = trimmedByteArray(bs.toIndexedSeq)
  def trimmedByteArray(bs: Seq[Byte]): Seq[Byte] = bs.reverse.dropWhile(_ == 0).reverse

  def sha256(bs: Array[Byte]): Array[Byte] = sha256(bs.toIndexedSeq)
  def sha256(bs: Seq[Byte]): Array[Byte] = {
    val md = MessageDigest.getInstance("SHA-256")
    md.update(bs.toArray)
    md.digest
  }

  def base64(bs: Seq[Byte]): String = base64(bs.toArray)
  def base64(bs: Array[Byte]): String = Base64.encodeBase64String(bs)

  def base64(s: String): Array[Byte] = Base64.decodeBase64(s)

  def bytesToHex(bs: Array[Byte]): String = bytesToHex(bs.toIndexedSeq)
  def bytesToHex(bs: Seq[Byte]): String = bs.map("%02X".format(_)).mkString

  def hexToBytes(hex: String): Seq[Byte] = hex.toSeq.sliding(2, 2).map(_.unwrap).map(Integer.parseInt(_, 16).toByte).toIndexedSeq

  def checksum(bytes: Array[Byte]): Array[Byte] = {
    // This code calculates CRC16-XModem checksum
    // Ported from https://github.com/alexgorbatchev/node-crc, via https://github.com/stellar/java-stellar-sdk

    @tailrec
    def loop(bs: Seq[Byte], crc: Int): Int = {
      bs match {
        case h +: t =>
          var code = crc >>> 8 & 0xFF
          code ^= h & 0xFF
          code ^= code >>> 4
          var crc_ = crc << 8 & 0xFFFF
          crc_ ^= code
          code = code << 5 & 0xFFFF
          crc_ ^= code
          code = code << 7 & 0xFFFF
          crc_ ^= code
          loop(t, crc_)
        case Nil => crc
      }
    }

    val crc = loop(bytes.toIndexedSeq, 0x0000)
    Array(crc.toByte, (crc >>> 8).toByte)
  }

} 
Example 65
Source File: Checksum.scala    From schedoscope   with Apache License 2.0 5 votes vote down vote up
package org.schedoscope.dsl.transformations

import java.security.MessageDigest

import org.apache.hadoop.fs.{FileStatus, Path}
import org.schedoscope.Schedoscope
import org.schedoscope.scheduler.driver.FilesystemDriver._

import scala.Array.canBuildFrom
import scala.collection.mutable.HashMap

object Checksum {
  private def md5 = MessageDigest.getInstance("MD5")

  private def listFiles(path: String): Array[FileStatus] = {
    val files = fileSystem(path, Schedoscope.settings.hadoopConf).globStatus(new Path(path))
    if (files != null)
      files
    else Array()
  }

  private def fileChecksum(path: String) =
    if (path == null)
      "null-checksum"
    else if (path.endsWith(".jar"))
      path
    else try {
      val cs = fileSystem(path, Schedoscope.settings.hadoopConf).getFileChecksum(new Path(path))
      if (cs == null)
        path
      else
        cs.toString()
    } catch {
      case _: Throwable => path
    }

  def fileChecksums(paths: List[String], recursive: Boolean): List[String] =
    paths.flatMap(path => {
      if (fileSystem(path, Schedoscope.settings.hadoopConf).isFile(new Path(path)))
        List(fileChecksum(path))
      else if (recursive)
        fileChecksums(listFiles(path + "/*").map(f => f.getPath.toString()).toList, recursive)
      else
        List()
    }).sorted

  val resourceHashCache = new HashMap[List[String], List[String]]()

  def resourceHashes(resources: List[String]): List[String] = synchronized {
    resourceHashCache.getOrElseUpdate(resources, fileChecksums(resources, true))
  }

  val defaultDigest = "0"

  def digest(stringsToDigest: String*): String =
    if (stringsToDigest.isEmpty)
      defaultDigest
    else
      md5.digest(stringsToDigest.sorted.mkString.toCharArray().map(_.toByte)).map("%02X" format _).mkString

  object SchemaChecksum {
    val checksumProperty = "schema.checksum"
  }

  object TransformationChecksum {
    val checksumProperty = "transformation.checksum"
    val timestampProperty = "transformation.timestamp"
  }

} 
Example 66
Source File: Utils.scala    From finagle-postgres   with Apache License 2.0 5 votes vote down vote up
package com.twitter.finagle.postgres.values

import java.nio.charset.Charset
import java.security.MessageDigest

import io.netty.buffer.ByteBuf

import scala.annotation.tailrec

object Charsets {
  val Utf8 = Charset.forName("UTF-8")
}

object Buffers {
  
  @throws(classOf[IllegalArgumentException])
  def encrypt(user: Array[Byte], password: Array[Byte], salt: Array[Byte]): Array[Byte] = {

    require(user != null && user.length > 0, "user should not be empty")
    require(password != null && password.length > 0, "password should not be empty")
    require(salt != null && salt.length > 0, "salt should not be empty")

    val inner = MessageDigest.getInstance("MD5")
    inner.update(password)
    inner.update(user)

    val outer = MessageDigest.getInstance("MD5")
    outer.update(Hex.valueOf(inner.digest).getBytes)
    outer.update(salt)

    ("md5" + Hex.valueOf(outer.digest)).getBytes
  }
}

object Hex {
  def valueOf(buf: Array[Byte]): String = buf.map("%02X" format _).mkString.toLowerCase
}

object Convert {
  def asShort(i : Int) = i.asInstanceOf[Short]
}

object Strings {
  val empty = new String
} 
Example 67
Source File: MerkleDigest.scala    From JustinDB   with Apache License 2.0 5 votes vote down vote up
package justin.db.merkletrees

trait MerkleDigest[T] {
  def digest(t: T): Digest
}

object MerkleDigest {

  implicit object CRC32 extends MerkleDigest[Block] {
    import java.nio.ByteBuffer
    import java.util.zip.CRC32

    override def digest(t: Block): Digest = {
      val digest = new CRC32()
      digest.update(t)

      val buffer = ByteBuffer.allocate(8)
      buffer.putLong(digest.getValue)

      Digest(buffer.array())
    }
  }

  implicit object MD5 extends MerkleDigest[Block] {
    import java.security.MessageDigest

    override def digest(t: Block): Digest = {
      Digest(MessageDigest.getInstance("MD5").digest(t))
    }
  }
}

case class Digest(hash: Array[Byte]) extends AnyVal {
  def +(that: Digest): Digest = Digest(this.hash ++ that.hash)
  def ==(that: Digest): Boolean = this.hash.deep == that.hash.deep
} 
Example 68
Source File: GenericHash.scala    From tsec   with MIT License 5 votes vote down vote up
package tsec.libsodium

import java.security.MessageDigest

import cats.effect.IO
import fs2._
import tsec.common._
import tsec.hashing.CryptoHasher
import tsec.hashing.libsodium._
import tsec.hashing.libsodium.internal.SodiumHashPlatform

class GenericHash extends SodiumSpec {

  def hashTest[A](platform: SodiumHashPlatform[A])(
      implicit hasher: CryptoHasher[IO, A]
  ) = {
    behavior of "Sodium hash for " + platform.algorithm

    it should "hash two byte arrays into equal hash values" in {
      forAll { (s: String) =>
        val program = for {
          h1 <- platform.hash[IO](s.utf8Bytes)
          h2 <- platform.hash[IO](s.utf8Bytes)
        } yield (h1, h2)

        val (h1, h2) = program.unsafeRunSync()
        h1.toHexString mustBe h2.toHexString
      }
    }

    it should "work equally for streaming as for single chunk" in {
      forAll { (s: String) =>
        val program = for {
          h1 <- platform.hash[IO](s.utf8Bytes)
          h2 <- Stream.emits(s.utf8Bytes).covary[IO].through(platform.hashPipe).compile.toVector
        } yield (h1, h2.toArray)

        val (h1, h2) = program.unsafeRunSync()
        h1.toHexString mustBe h2.toHexString
      }
    }
  }

  hashTest(Blake2b)
  hashTest(SodiumSHA256)
  hashTest(SodiumSHA512)

  behavior of "Blake2b-specific functions"

  it should "hash properly for a particular key" in {
    forAll { (s: String) =>
      val program = for {
        k  <- Blake2b.generateKey[IO]
        h1 <- Blake2b.hashKeyed[IO](s.utf8Bytes, k)
        h2 <- Blake2b.hashKeyed[IO](s.utf8Bytes, k)
      } yield (h1, h2)

      val (h1, h2) = program.unsafeRunSync()
      h1.toHexString mustBe h2.toHexString
    }
  }

  it should "not authenticate for an incorrect key" in {
    forAll { (s: String) =>
      val program = for {
        k1 <- Blake2b.generateKey[IO]
        k2 <- Blake2b.generateKey[IO]
        h1 <- Blake2b.hashKeyed[IO](s.utf8Bytes, k1)
        h2 <- Blake2b.hashKeyed[IO](s.utf8Bytes, k2)
      } yield MessageDigest.isEqual(h1, h2)

      program.unsafeRunSync() mustBe false
    }
  }

} 
Example 69
Source File: Blake2b.scala    From tsec   with MIT License 5 votes vote down vote up
package tsec.hashing.libsodium

import java.security.MessageDigest

import cats.effect.Sync
import tsec.hashing._
import tsec.hashing.libsodium.internal.SodiumHashPlatform
import tsec.libsodium.ScalaSodium
import tsec.libsodium.ScalaSodium.NullPtrBytes

sealed trait Blake2b

object Blake2b extends SodiumHashPlatform[Blake2b]("Blake2b") {
  val MinKeyLen     = ScalaSodium.crypto_generichash_blake2b_KEYBYTES_MIN
  val DefaultKeyLen = ScalaSodium.crypto_generichash_blake2b_KEYBYTES
  val MaxKeyLen     = ScalaSodium.crypto_generichash_blake2b_KEYBYTES_MAX

  val MinHashLen = ScalaSodium.crypto_generichash_blake2b_BYTES_MIN
  val MaxHashLen = ScalaSodium.crypto_generichash_blake2b_BYTES_MAX

  
  val hashLen: Int = ScalaSodium.crypto_generichash_blake2b_BYTES

  def generateKey[F[_]](implicit F: Sync[F], S: ScalaSodium): F[BlakeKey] = F.delay {
    BlakeKey(ScalaSodium.randomBytesUnsafe(DefaultKeyLen))
  }

  def generateMinKey[F[_]](implicit F: Sync[F], S: ScalaSodium): F[BlakeKey] = F.delay {
    BlakeKey(ScalaSodium.randomBytesUnsafe(MinKeyLen))
  }

  def generateMaxKey[F[_]](implicit F: Sync[F], S: ScalaSodium): F[BlakeKey] = F.delay {
    BlakeKey(ScalaSodium.randomBytesUnsafe(MaxKeyLen))
  }

  def generateKeyVarLen[F[_]](len: Int)(implicit F: Sync[F], S: ScalaSodium): F[BlakeKey] = F.delay {
    val outLen = math.max(MinKeyLen, math.min(MaxKeyLen, len))
    BlakeKey(ScalaSodium.randomBytesUnsafe(outLen))
  }

  def hashVarLen[F[_]](
      in: Array[Byte],
      len: Int = hashLen
  )(implicit F: Sync[F], S: ScalaSodium): F[CryptoHash[Blake2b]] =
    F.delay {
      val outLen = math.max(MinHashLen, math.min(MaxHashLen, len))
      val out    = new Array[Byte](outLen)
      S.crypto_generichash(out, outLen, in, in.length, NullPtrBytes, 0)
      CryptoHash[Blake2b](out)
    }

  def verify[F[_]](in: Array[Byte], compare: CryptoHash[Blake2b], key: BlakeKey)(
      implicit F: Sync[F],
      S: ScalaSodium
  ): F[Boolean] =
    F.delay {
      val out = new Array[Byte](compare.length)
      S.crypto_generichash(out, compare.length, in, in.length, key, key.length)
      MessageDigest.isEqual(out, compare)
    }

  def hashKeyed[F[_]](in: Array[Byte], key: BlakeKey)(implicit F: Sync[F], S: ScalaSodium): F[CryptoHash[Blake2b]] =
    F.delay {
      val out = new Array[Byte](hashLen)
      S.crypto_generichash(out, hashLen, in, in.length, key, key.length)
      CryptoHash[Blake2b](out)
    }

  def hashKeyedVarLen[F[_]](in: Array[Byte], key: BlakeKey, len: Int = hashLen)(
      implicit F: Sync[F],
      S: ScalaSodium
  ): F[CryptoHash[Blake2b]] = F.delay {
    val outLen = math.max(MinHashLen, math.min(MaxHashLen, len))
    val out    = new Array[Byte](outLen)
    S.crypto_generichash(out, outLen, in, in.length, key, key.length)
    CryptoHash[Blake2b](out)
  }

  def stateSize(implicit S: ScalaSodium): Int = S.crypto_generichash_statebytes

  def sodiumHash(in: Array[Byte], out: Array[Byte])(implicit S: ScalaSodium): Int =
    S.crypto_generichash(out, hashLen, in, in.length, NullPtrBytes, 0)

  def sodiumHashInit(state: HashState[Blake2b])(implicit S: ScalaSodium): Int =
    S.crypto_generichash_blake2b_init(state, NullPtrBytes, 0, hashLen)

  def sodiumHashChunk(state: HashState[Blake2b], in: Array[Byte])(implicit S: ScalaSodium): Int =
    S.crypto_generichash_update(state, in, in.length)

  def sodiumHashFinal(state: HashState[Blake2b], out: Array[Byte])(implicit S: ScalaSodium): Int =
    S.crypto_generichash_final(state, out, hashLen)

}