java.math.BigInteger Scala Examples

The following examples show how to use java.math.BigInteger. 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: ApplicationKeystore.scala    From OUTDATED_ledger-wallet-android   with MIT License 5 votes vote down vote up
package co.ledger.wallet.core.security

import java.io.File
import java.math.BigInteger
import java.security.KeyStore.{PasswordProtection, PrivateKeyEntry}
import java.security.{KeyStore, SecureRandom}
import java.util.Calendar
import javax.security.auth.x500.X500Principal

import android.content.Context
import co.ledger.wallet.core.crypto.Crypto
import org.spongycastle.x509.X509V3CertificateGenerator

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

class ApplicationKeystore(context: Context, val keystoreName: String) extends Keystore(context) {

  def install(passwordProtection: PasswordProtection): Future[Keystore] = {
    assert(!file.exists(), "The keystore already exists")
    val keyStore = KeyStore.getInstance(KeyStore.getDefaultType)
    keyStore.load(null)
    val output = context.openFileOutput(keystoreName, Context.MODE_PRIVATE)
    keyStore.store(output, passwordProtection.getPassword)
    output.close()
    load(passwordProtection)
  }

  def setPassword(password: String): Unit = {
    require(password != null, "Password cannot be null")
    _password = password.toCharArray
    store()
  }

  override protected def loadJavaKeyStore(passwordProtection: PasswordProtection): Future[JavaKeyStore] = {
    Future {
      assert(file.exists(), "The keystore is not installed yet")
      val keystore = KeyStore.getInstance(KeyStore.getDefaultType)
      val input = context.openFileInput(keystoreName)
      keystore.load(input, passwordProtection.getPassword)
      input.close()
      _password = passwordProtection.getPassword
      keystore
    }
  }

  override def generateKey(alias: String): JavaKeyPair = {
    Crypto.ensureSpongyIsInserted()
    val kpg = java.security.KeyPairGenerator.getInstance("RSA")
    val calendar = Calendar.getInstance()
    val now = calendar.getTime
    calendar.add(Calendar.YEAR, 100)
    val end = calendar.getTime
    kpg.initialize(1024, new SecureRandom())
    val keypair = kpg.generateKeyPair()
    val certGen = new X509V3CertificateGenerator()
    val dnName = new X500Principal("CN=Ledger")
    certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()))
    certGen.setSubjectDN(dnName)
    certGen.setIssuerDN(dnName)
    certGen.setNotBefore(now)
    certGen.setNotAfter(end)
    certGen.setPublicKey(keypair.getPublic)
    certGen.setSignatureAlgorithm("SHA256WithRSAEncryption")
    val certificate = certGen.generate(keypair.getPrivate, "SC")
    javaKeystore.get.setEntry(alias, new PrivateKeyEntry(keypair.getPrivate, Array(certificate)), null)
    store()
    Crypto.ensureSpongyIsRemoved()
    keypair
  }

  def delete(): Unit = {
    file.delete()
  }

  def isInstalled = file.exists()

  private[this] def store(): Unit = {
    file.delete()
    val output = context.openFileOutput(keystoreName, Context.MODE_PRIVATE)
    javaKeystore.get.store(output, _password)
    output.close()
  }

  @inline private[this] def file = new File(context.getFilesDir, keystoreName)
  private var _password: Array[Char] = null
} 
Example 2
Source File: Extensions.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate.eval

import java.math.BigInteger

import scalan.RType
import sigmastate.SType
import sigmastate.Values.Constant
import sigmastate.lang.DefaultSigmaBuilder
import special.collection.Coll
import special.sigma._
import SType.AnyOps
import org.ergoplatform.ErgoBox
import spire.syntax.all._

object Extensions {
  private val Colls = CostingSigmaDslBuilder.Colls

  implicit class ByteExt(val b: Byte) extends AnyVal {
    @inline def toBigInt: BigInt = CostingSigmaDslBuilder.BigInt(BigInteger.valueOf(b.toLong))
  }

  implicit class IntExt(val x: Int) extends AnyVal {
    
      CostingBox(isCost, ebox)
    }
  }

} 
Example 3
Source File: BigIntegerOps.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate.eval

import java.math.BigInteger

import scalan.{ExactNumeric, ExactIntegral, ExactOrderingImpl}

import scala.math.{LowPriorityOrderingImplicits, Integral, Ordering}
import special.sigma._
import scalan.util.Extensions._
import sigmastate.eval.Extensions._
import sigmastate.eval.NumericOps.BigIntIsExactNumeric.n

object OrderingOps extends LowPriorityOrderingImplicits {
  def apply[T](implicit ord: Ordering[T]) = ord

  trait BigIntegerOrdering extends Ordering[BigInteger] {
    def compare(x: BigInteger, y: BigInteger) = x.compareTo(y)
  }
  implicit object BigInteger extends BigIntegerOrdering

  trait BigIntOrdering extends Ordering[BigInt] {
    def compare(x: BigInt, y: BigInt) = x.compareTo(y)
  }
  implicit object BigInt extends BigIntOrdering
}

object NumericOps {

  trait BigIntegerIsIntegral extends Integral[BigInteger] {
//    private val BI = implicitly[Integral[BigInt]]
    def quot(x: BigInteger, y: BigInteger): BigInteger = x.divide(y)
    def rem(x: BigInteger, y: BigInteger): BigInteger = x.remainder(y)
    def plus(x: BigInteger, y: BigInteger): BigInteger = x.add(y)
    def minus(x: BigInteger, y: BigInteger): BigInteger = x.subtract(y)
    def times(x: BigInteger, y: BigInteger): BigInteger = x.multiply(y)
    def negate(x: BigInteger): BigInteger = x.negate()
    def fromInt(x: Int): BigInteger = BigInteger.valueOf(x)
    def toInt(x: BigInteger): Int = x.intValueExact()
    def toLong(x: BigInteger): Long = x.longValueExact()
    def toFloat(x: BigInteger): Float = x.floatValue()
    def toDouble(x: BigInteger): Double = x.doubleValue()
  }
  implicit object BigIntegerIsIntegral extends BigIntegerIsIntegral with OrderingOps.BigIntegerOrdering

  trait BigIntIsIntegral extends Integral[BigInt] {
    def quot(x: BigInt, y: BigInt): BigInt = x.divide(y)
    def rem(x: BigInt, y: BigInt): BigInt = x.remainder(y)
    def plus(x: BigInt, y: BigInt): BigInt = x.add(y)
    def minus(x: BigInt, y: BigInt): BigInt = x.subtract(y)
    def times(x: BigInt, y: BigInt): BigInt = x.multiply(y)
    def negate(x: BigInt): BigInt = x.negate()
    def fromInt(x: Int): BigInt = x.toBigInt
    def toInt(x: BigInt): Int = x.toInt
    def toLong(x: BigInt): Long = x.toLong
    def toFloat(x: BigInt): Float = CostingSigmaDslBuilder.toBigInteger(x).floatValue()
    def toDouble(x: BigInt): Double = CostingSigmaDslBuilder.toBigInteger(x).doubleValue()
  }
  implicit object BigIntIsIntegral extends BigIntIsIntegral with OrderingOps.BigIntOrdering

  implicit object BigIntIsExactNumeric extends ExactNumeric[BigInt] {
    val n = BigIntIsIntegral
    override def plus(x: BigInt, y: BigInt): BigInt = n.plus(x, y)
    override def minus(x: BigInt, y: BigInt): BigInt = n.minus(x, y)
    override def times(x: BigInt, y: BigInt): BigInt = n.times(x, y)
  }

  implicit object BigIntIsExactIntegral extends ExactIntegral[BigInt] {
    val n = BigIntIsIntegral
    override def plus(x: BigInt, y: BigInt): BigInt = n.plus(x, y)
    override def minus(x: BigInt, y: BigInt): BigInt = n.minus(x, y)
    override def times(x: BigInt, y: BigInt): BigInt = n.times(x, y)
  }

  implicit object BigIntIsExactOrdering extends ExactOrderingImpl[BigInt](BigIntIsIntegral)
} 
Example 4
Source File: PolynomialTest.scala    From scalaprops   with MIT License 5 votes vote down vote up
package scalaprops

import java.math.BigInteger

object PolynomialTest extends Scalaprops {
  val testPolynomialString = Property.forAll {
    new F2Polynomial("1101").toString == "1101"
  }

  val testPolynomialStringInt = Property.forAll {
    new F2Polynomial("f", 16).toString == "1111"
  }

  val testDegree = Property.forAll {
    assert(new F2Polynomial("100").degree == 2)
    assert(new F2Polynomial("1").degree == 0)
    assert(new F2Polynomial("0").degree == -1)
    true
  }

  val testAdd = Property.forAll {
    new F2Polynomial("1101").add(new F2Polynomial("110")) == new F2Polynomial("1011")
  }

  val testMul = Property.forAll {
    assert(new F2Polynomial("110") == new F2Polynomial("11").mul(new F2Polynomial("10")))
    assert(new F2Polynomial("1110") == new F2Polynomial("10").mul(new F2Polynomial("111")))
    true
  }

  val testMod = Property.forAll {
    new F2Polynomial("1011").mod(new F2Polynomial("11")) == new F2Polynomial("1")
  }

  val testPower = Property.forAll {
    new F2Polynomial("10").power(new BigInteger("3")) == new F2Polynomial("1000")
  }

  val testPowerMod = Property.forAll {
    new F2Polynomial("10").powerMod(new BigInteger("3"), new F2Polynomial("11")) == new F2Polynomial("1")
  }

  val testGetCoefficient = Property.forAll {
    assert(1 == new F2Polynomial("10").getCoefficient(1))
    assert(0 == new F2Polynomial("10").getCoefficient(0))
    true
  }

  val testToString = Property.forAll {
    assert("101" == new F2Polynomial("101").toString)
    assert("5" == new F2Polynomial("101").toString(16))
    true
  }

  val testHashCode = Property.forAll {
    new BigInteger("1101", 2).hashCode() == new F2Polynomial("1101").hashCode()
  }

  val testEquals = Property.forAll {
    assert(new F2Polynomial("11010") == new F2Polynomial("11010"))
    assert(new F2Polynomial("11010") != new F2Polynomial("11011"))
    val p = new F2Polynomial("101")
    assert(p == p)
    true
  }
} 
Example 5
Source File: Channel.scala    From zio-keeper   with Apache License 2.0 5 votes vote down vote up
package zio.keeper.transport

import java.math.BigInteger

import zio.keeper.TransportError
import zio.keeper.TransportError.ExceptionWrapper
import zio.{ Chunk, IO, _ }


  def withLock(
    read: Int => IO[TransportError, Chunk[Byte]],
    write: Chunk[Byte] => IO[TransportError, Unit],
    isOpen: IO[TransportError, Boolean],
    finalizer: IO[TransportError, Unit]
  ): UIO[Channel] =
    for {
      writeLock <- Semaphore.make(1)
      readLock  <- Semaphore.make(1)
    } yield {
      new Channel(
        bytes => readLock.withPermit(read(bytes)),
        chunk => writeLock.withPermit(write(chunk)),
        isOpen,
        finalizer
      )
    }
} 
Example 6
Source File: ECKeyPair.scala    From OUTDATED_ledger-wallet-android   with MIT License 5 votes vote down vote up
package co.ledger.wallet.core.crypto

import java.math.BigInteger
import java.security.SecureRandom

import org.spongycastle.asn1.sec.SECNamedCurves
import org.spongycastle.crypto.AsymmetricCipherKeyPair
import org.spongycastle.crypto.generators.ECKeyPairGenerator
import org.spongycastle.crypto.params.{ECPrivateKeyParameters, ECPublicKeyParameters, ECDomainParameters, ECKeyGenerationParameters}
import org.spongycastle.math.ec.ECPoint
import org.spongycastle.util.encoders.Hex

import scala.util.Try

abstract class ECKeyPair {

  Try(PRNGFixes.apply())

  def publicKeyPoint: ECPoint
  def privateKeyInteger: BigInteger

  def privateKey = privateKeyInteger.toByteArray
  def publicKey = publicKeyPoint.getEncoded
  def compressedPublicKey = new ECPoint.Fp(ECKeyPair.Domain.getCurve, publicKeyPoint.getXCoord, publicKeyPoint.getYCoord, true).getEncoded

  def privateKeyHexString = Hex.toHexString(privateKey)
  def publicKeyHexString = Hex.toHexString(publicKey)
  def compressedPublicKeyHexString = Hex.toHexString(compressedPublicKey)

  def generateAgreementSecret(publicKeyHex: String): Array[Byte] = generateAgreementSecret(Hex.decode(publicKeyHex))

  def generateAgreementSecret(publicKey: Array[Byte]): Array[Byte] = {
    Crypto.ensureSpongyIsInserted()
    val publicKeyPoint = ECKeyPair.bytesToEcPoint(publicKey)
    val P = publicKeyPoint.multiply(ECKeyPair.Domain.getH.multiply(privateKeyInteger))
    P.normalize().getXCoord.getEncoded
  }
}

object ECKeyPair {
 Crypto.ensureSpongyIsInserted()
  val Curve = SECNamedCurves.getByName("secp256k1")
  val Domain = new ECDomainParameters(Curve.getCurve, Curve.getG, Curve.getN, Curve.getH)
  val SecureRandom = new SecureRandom
  private[this] val generator = new ECKeyPairGenerator
  private[this] val keygenParams = new ECKeyGenerationParameters(ECKeyPair.Domain, ECKeyPair.SecureRandom)
  generator.init(keygenParams)

  def generate(): ECKeyPair = new ECKeyPairFromAsymmetricCipherKeyPair(generator.generateKeyPair())
  def create(privateKey: Array[Byte]): ECKeyPair = new ECKeyPairFromPrivateKey(privateKey)
  def bytesToEcPoint(bytes: Array[Byte]): ECPoint = Curve.getCurve.decodePoint(bytes)

  private sealed class ECKeyPairFromAsymmetricCipherKeyPair(keyPair: AsymmetricCipherKeyPair) extends ECKeyPair {
    def publicKeyParameters = keyPair.getPublic.asInstanceOf[ECPublicKeyParameters]
    def privateKeyParameters = keyPair.getPrivate.asInstanceOf[ECPrivateKeyParameters]

    def publicKeyPoint = publicKeyParameters.getQ
    def privateKeyInteger = privateKeyParameters.getD
  }

  private sealed class ECKeyPairFromPrivateKey(bytes: Array[Byte]) extends ECKeyPair {

    private[this] val _privateKey = new BigInteger(1, bytes).mod(Curve.getN)
    private[this] val _publicKey = Curve.getG.multiply(_privateKey)

    def publicKeyPoint = _publicKey
    def privateKeyInteger = _privateKey
  }

} 
Example 7
Source File: AmountFormatter.scala    From OUTDATED_ledger-wallet-android   with MIT License 5 votes vote down vote up
package co.ledger.wallet.core.bitcoin

import java.math.BigInteger
import java.text.DecimalFormat

trait AmountFormatter {
  def format(value: BigInt, precision: Int = -1): String
}

object AmountFormatter {

  lazy val Bitcoin: AmountFormatter = new BitcoinFormatter

  private class MagnitudeDependantFormatter(magnitude: Int) extends AmountFormatter {
    override def format(value: BigInt, precision: Int = -1): String = {
      val df = new DecimalFormat()
      val (integralPart, decimalPart) = value /% BigInteger.valueOf(10).pow(magnitude)
      val doubleValue = integralPart.longValue() + (decimalPart.longValue() / Math.pow(10, magnitude))
      val pattern = "###,###,###,##0" + {
        if (precision > 0) {
         "." + "0" * precision
        } else if (precision < 0) {
          "." + "#" * magnitude
        } else {
          ""
        }
      }
      df.applyPattern(pattern)
      df.format(doubleValue)
    }
  }

  private class BitcoinFormatter extends MagnitudeDependantFormatter(8)

} 
Example 8
Source File: Base58.scala    From OUTDATED_ledger-wallet-android   with MIT License 5 votes vote down vote up
package co.ledger.wallet.core.bitcoin

import java.math.BigInteger

import org.spongycastle.crypto.digests.SHA256Digest

import scala.util.Try

// Based on https://gist.github.com/CodesInChaos/3175971#file_base58_encoding.cs

object Base58 {

  val Digits = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

  @throws(classOf[InvalidBase58FormatException])
  def decode(s: String): Array[Byte] = {
    var intData = BigInteger.ZERO
    for (i <- 0 until s.length) {
      val digit = Digits.indexOf(s(i))
      if (digit < 0)
        throw new InvalidBase58FormatException("Invalid character '" + digit + "'")
      intData = intData.multiply(BigInteger.valueOf(58)).add(BigInteger.valueOf(digit))
    }
    Array.fill[Byte](s.takeWhile(_ == '1').length)(0) ++ intData.toByteArray.dropWhile(_ == 0)
  }

  def tryDecode(s: String): Try[Array[Byte]] = Try(decode(s))

  def decodeWithChecksum(s: String): (Array[Byte], Array[Byte]) = {
    val decoded = decode(s)
    (decoded.slice(0, decoded.length - 4), decoded.slice(decoded.length - 4, decoded.length))
  }

  def checkAndDecode(s: String): Option[Array[Byte]] = {
    val (data, checksum) = decodeWithChecksum(s)
    val validChecksum = computeChecksum(data)
    if (validChecksum.deep == checksum.deep)
      Some(data)
    else
      None
  }

  def computeChecksum(b: Array[Byte]): Array[Byte] = {
    val digest = new SHA256Digest
    digest.update(b, 0, b.length)
    val firstPass = new Array[Byte](32)
    val secondPass = new Array[Byte](32)
    digest.doFinal(firstPass, 0)
    digest.reset()
    digest.update(firstPass, 0, firstPass.length)
    digest.doFinal(secondPass, 0)
    secondPass.slice(0, 4)
  }

  def encode(b: Array[Byte]): String = {
    var intData = BigInteger.ZERO
    for (i <- 0 until b.length) {
      intData = intData.multiply(BigInteger.valueOf(256)).add(BigInteger.valueOf(b(i) & 0xFF))
    }
    var result = ""
    while (intData.compareTo(BigInteger.ZERO) > 0) {
      val r = intData.mod(BigInteger.valueOf(58)).intValue()
      intData = intData.divide(BigInteger.valueOf(58))
      result = Digits(r).toString + result
    }
    "1" * b.takeWhile(_ == 0).length + result
  }

  def encodeWitchChecksum(b: Array[Byte]): String = encode(b ++ computeChecksum(b))
  def verify(s: String): Boolean = checkAndDecode(s).isDefined

  class InvalidBase58FormatException(reason: String) extends Exception(reason)

} 
Example 9
Source File: BytesReader.scala    From OUTDATED_ledger-wallet-android   with MIT License 5 votes vote down vote up
package co.ledger.wallet.core.utils

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

import org.bitcoinj.core.VarInt

class BytesReader(val bytes: Array[Byte]) {

  private[this] var _offset = 0

  def read(length: Int): Array[Byte]  = {
    val offset = _offset
    seek(length)
    bytes.slice(offset, _offset)
  }

  def seek(length: Int): Unit = {
    if (length > available) {
      throw new IndexOutOfBoundsException(s"Invalid length ($length) is greater than available byte to read ($available)")
    }
    val offset = _offset
    _offset = offset + (if (length >= 0) length else available)
  }

  def readString(length: Int, charset: Charset = Charset.defaultCharset()): String = {
    new String(read(length), charset)
  }

  def readBigInteger(length: Int, signum: Int = 1): BigInteger = {
    new BigInteger(1, read(length))
  }
  def readNextShort(): Short = readBigInteger(2).shortValue()
  def readNextInt(): Int = readBigInteger(4).intValue()
  def readNextLong(): Long = readBigInteger(8).longValue()

  def readLeBigInteger(length: Int, signum: Int = 1): BigInteger = {
    new BigInteger(1, read(length).reverse)
  }
  def readNextLeShort(): Int = readLeBigInteger(2).intValue()
  def readNextLeInt(): Int = readLeBigInteger(4).intValue()
  def readNextLeLong(): Long = readLeBigInteger(8).longValue()

  def readNextByte(): Byte = read(1)(0)

  def readNextVarInt(): VarInt = {
    val varInt = new VarInt(bytes, _offset)
    seek(varInt.getOriginalSizeInBytes)
    varInt
  }

  def available: Int = bytes.length - _offset
  def length: Int = bytes.length

  def apply(index: Int): Byte = bytes(index)
} 
Example 10
Source File: Zero.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate.eval

import java.math.BigInteger

import scalan.{Nullable, RType}
import special.collection.{CSizePrim, CSizePair, Size, CSizeOption, CollType, Coll, CSizeColl}
import scalan.RType._
import sigmastate._
import sigmastate.SBigInt.MaxSizeInBytes
import special.sigma._
import SType.AnyOps
import scorex.crypto.authds.avltree.batch.BatchAVLProver
import scorex.crypto.hash.{Digest32, Blake2b256}
import sigmastate.interpreter.CryptoConstants
import sigmastate.interpreter.CryptoConstants.EcPointType

trait Zero[T] {
    def zero: T
}

case class CZero[T](zero: T) extends Zero[T]

trait ZeroLowPriority {
  implicit def collIsZero[T: Zero: RType]: Zero[Coll[T]] = CZero(Colls.emptyColl[T])
  implicit def optionIsZero[T: Zero]: Zero[Option[T]] = CZero(Some(Zero.zeroOf[T]))
  implicit def pairIsZero[A: Zero, B: Zero]: Zero[(A,B)] = CZero(Zero[A].zero, Zero[B].zero)
}
object Zero extends ZeroLowPriority {
  def apply[T](implicit z: Zero[T]): Zero[T] = z
  def zeroOf[T: Zero]: T = Zero[T].zero

  implicit val BooleanIsZero: Zero[Boolean] = CZero(false)
  implicit val ByteIsZero: Zero[Byte] = CZero(0.toByte)
  implicit val ShortIsZero: Zero[Short] = CZero(0.toShort)
  implicit val IntIsZero: Zero[Int] = CZero(0)
  implicit val LongIsZero: Zero[Long] = CZero(0L)
  implicit val BigIntIsZero: Zero[BigInt] = CZero(CBigInt(BigInteger.ZERO))
  implicit val GroupElementIsZero: Zero[GroupElement] = CZero(CGroupElement(CryptoConstants.dlogGroup.identity))
  implicit val AvlTreeIsZero: Zero[AvlTree] = CZero({
    val avlProver = new BatchAVLProver[Digest32, Blake2b256.type](keyLength = 32, None)
    val digest = avlProver.digest
    val treeData = new AvlTreeData(digest, AvlTreeFlags.AllOperationsAllowed, 32, None)
    CAvlTree(treeData)
  })

  def typeToZero[T](t: RType[T]): Zero[T] = (t match {
    case BooleanType => Zero[Boolean]
    case ByteType => Zero[Byte]
    case ShortType => Zero[Short]
    case IntType => Zero[Int]
    case LongType => Zero[Long]
    case BigIntRType => Zero[BigInt]
    case GroupElementRType => Zero[GroupElement]
    case AvlTreeRType => Zero[AvlTree]
    case SigmaPropRType => sigmaPropIsZero
    case ct: CollType[a] => collIsZero(typeToZero(ct.tItem), ct.tItem)
    case ct: OptionType[a] => optionIsZero(typeToZero(ct.tA))
    case ct: PairType[a, b] => pairIsZero(typeToZero(ct.tFst), typeToZero(ct.tSnd))
    case _ => sys.error(s"Don't know how to compute Zero for type $t")
  }).asInstanceOf[Zero[T]]

  implicit val sigmaPropIsZero: Zero[SigmaProp] = CZero(CSigmaProp(TrivialProp.FalseProp))
} 
Example 11
Source File: AndroidKeystore.scala    From OUTDATED_ledger-wallet-android   with MIT License 5 votes vote down vote up
package co.ledger.wallet.core.security

import java.math.BigInteger
import java.security.KeyStore
import java.security.KeyStore.PasswordProtection
import java.util.Calendar
import javax.security.auth.x500.X500Principal

import android.content.Context
import android.security.KeyPairGeneratorSpec
import co.ledger.wallet.core.crypto.Crypto

import scala.concurrent.{Promise, Future}

class AndroidKeystore(context: Context) extends Keystore(context) {

  override protected def loadJavaKeyStore(passwordProtection: PasswordProtection): Future[JavaKeyStore] = {
    Crypto.ensureSpongyIsRemoved()
    val keystore = KeyStore.getInstance("AndroidKeyStore")
    keystore.load(null)
    Future.successful(keystore)
  }

  override def generateKey(alias: String): JavaKeyPair = {
    Crypto.ensureSpongyIsRemoved()
    val kpg = java.security.KeyPairGenerator.getInstance("RSA", "AndroidKeyStore")
    val calendar = Calendar.getInstance()
    val now = calendar.getTime
    calendar.add(Calendar.YEAR, 100)
    val end = calendar.getTime
    kpg.initialize(
      new KeyPairGeneratorSpec.Builder(context.getApplicationContext)
        .setAlias(alias)
        .setStartDate(now)
        .setEndDate(end)
        .setSerialNumber(BigInteger.valueOf(1))
        .setSubject(new X500Principal("CN=Ledger"))
        .build()
    )
    kpg.generateKeyPair()
  }
} 
Example 12
Source File: Application.scala    From auth0-scala-samples   with MIT License 5 votes vote down vote up
package controllers

import javax.inject.Inject
import play.api.cache._
import play.api.mvc.{Action, AnyContent, Controller}
import helpers.Auth0Config
import java.security.SecureRandom
import java.math.BigInteger
import java.util.UUID.randomUUID

import play.api.Configuration


class Application @Inject() (cache: CacheApi, configuration: Configuration) extends Controller {

  private val config = Auth0Config.get(configuration)

  def index: Action[AnyContent] = Action {
    Ok(views.html.index())
  }

  def login: Action[AnyContent] = Action {
    // Generate random state parameter
    object RandomUtil {
      private val random = new SecureRandom()

      def alphanumeric(nrChars: Int = 24): String = {
        new BigInteger(nrChars * 5, random).toString(32)
      }
    }
    val state = RandomUtil.alphanumeric()

    var audience = config.audience
    if (config.audience == ""){
      audience = String.format("https://%s/userinfo", config.domain)
    }

    val id = randomUUID().toString
    cache.set(id + "state", state)
    val query = String.format(
      "authorize?client_id=%s&redirect_uri=%s&response_type=code&scope=openid profile&audience=%s&state=%s",
      config.clientId,
      config.callbackURL,
      audience,
      state
    )
    Redirect(String.format("https://%s/%s", config.domain, query)).withSession("id" -> id)
  }

  def logout: Action[AnyContent] = Action { request =>
    val host = request.host
    var scheme = "http"
    if (request.secure) {
      scheme = "https"
    }
    val returnTo = scheme + "://" + host
    Redirect(String.format(
      "https://%s/v2/logout?client_id=%s&returnTo=%s",
      config.domain,
      config.clientId,
      returnTo)
    ).withNewSession
  }
} 
Example 13
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 14
Source File: Service.scala    From reactive-microservices   with MIT License 5 votes vote down vote up
import akka.actor.ActorSystem
import akka.event.Logging
import java.math.BigInteger
import java.security.SecureRandom
import scala.concurrent.{ExecutionContext, Future}

class Service(repository: Repository)(implicit actorSystem: ActorSystem, ec: ExecutionContext) extends Config {
  def relogin(reloginRequest: ReloginRequest): Future[Option[Token]] = {
    repository.addMethodToValidTokenByValue(reloginRequest.tokenValue, reloginRequest.authMethod)
  }

  def login(loginRequest: LoginRequest): Future[Token] = {
    val newToken = createFreshToken(loginRequest.identityId, loginRequest.authMethod)
    repository.insertToken(newToken).map(_ => newToken)
  }

  def findAndRefreshToken(tokenValue: String): Future[Option[Token]] = {
    repository.findValidTokenByValue(tokenValue).map { tokenOption =>
      tokenOption.map { token =>
        val newToken = refreshToken(token)
        if (newToken != token)
          repository.updateTokenByValue(token.value, newToken).onFailure { case t => logger.error(t, "Token refreshment failed") }
        newToken
      }
    }
  }

  def logout(tokenValue: String): Unit = {
    repository.deleteTokenByValue(tokenValue).onFailure { case t => logger.error(t, "Token deletion failed") }
  }

  private def createFreshToken(identityId: Long, authMethod: String): Token = {
    Token(generateToken, System.currentTimeMillis() + tokenTtl, identityId, Set(authMethod))
  }

  private def generateToken: String = new BigInteger(255, random).toString(32)

  private def refreshToken(token: Token): Token = token.copy(validTo = math.max(token.validTo, System.currentTimeMillis() + sessionTtl))

  private val random = new SecureRandom()

  private val logger = Logging(actorSystem, getClass)
} 
Example 15
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 16
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 17
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 18
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 19
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 20
Source File: IntervalConversion.scala    From spark-vector   with Apache License 2.0 5 votes vote down vote up
package com.actian.spark_vector.colbuffer.util

import java.math.BigInteger

object IntervalConversion {
  
  def convertIntervalYM(interval: String): Int = {
    var (y, m) = interval.splitAt(interval.lastIndexOf("-"))
    val years = y.toInt
    val months = m.substring(1).toInt
    if (years >= 0) (years * 12 + months)
    else (years * 12 - months)
  }
  
  def deconvertIntervalYM(source: Int): String = {
    val months = math.abs(source % 12)
    val years = source / 12
    years.toString() + "-" + months.toString()
  }
  
  def convertIntervalDS(interval: String, scale: Int): BigInteger = {
    val units = interval.split(" ")
    var totaltime = BigInteger.valueOf(units(0).toLong * SecondsInDay)
    if (scale > 0) {
      val subunits = units(1).split('.')
      val seconds: Long = subunits(0).split(":").map(_.toInt).reduceLeft((x,y) => x * 60 + y)
      val subseconds: Long = if (subunits.length > 1) subunits(1).padTo(scale, '0').toInt else  0
      totaltime = totaltime.multiply(BigInteger.valueOf(math.pow(10, scale).toLong))
      if (totaltime.compareTo(BigInteger.ZERO) < 0) 
        totaltime = totaltime.subtract(BigInteger.valueOf(seconds * math.pow(10, scale).toLong + subseconds))
      else 
        totaltime = totaltime.add(BigInteger.valueOf(seconds * math.pow(10, scale).toLong + subseconds))
    } else {
      val seconds = units(1).split(":").map(_.toInt).reduceLeft((x,y) => x * 60 + y)
      if (totaltime.compareTo(BigInteger.ZERO) < 0)
        totaltime = totaltime.subtract(BigInteger.valueOf(seconds))
      else
        totaltime = totaltime.add(BigInteger.valueOf(seconds))
    }
    totaltime
  }
  
  def deconvertIntervalDS(source: BigInteger, scale: Int): String = {
    var raw = source
    var interval = new StringBuilder()
    
    if (scale > 0) {
      val parts = raw.divideAndRemainder(BigInteger.valueOf(math.pow(10, scale).toLong))
      if (parts(1) != BigInteger.ZERO) {
        interval.append(parts(1).abs().toString().reverse.padTo(scale, '0'))
        interval.append('.')
      }
      raw = parts(0)
    }
    
    val parts = raw.divideAndRemainder(BigInteger.valueOf(SecondsInDay))
    var seconds = math.abs(parts(1).longValue())
    val multiples = Seq(SecondsInMinute, MinutesInHour, HoursInDay)
    val time = for (m <- multiples) yield {
      val r = seconds % m
      seconds /= m
      r.toString().reverse.padTo(2, '0')
    }
    interval.append(time.mkString(":")).append(" ").append(parts(0).toString().reverse)
    interval.reverse.toString()
  }
} 
Example 21
Source File: IPConversion.scala    From spark-vector   with Apache License 2.0 5 votes vote down vote up
package com.actian.spark_vector.colbuffer.util

import java.math.BigInteger
import java.net.InetAddress

object IPConversion {
  
  def ipv4IntegerToString(i: Int): String = {
    val p = i ^ (1 << 31)
    Array(((p >> 24) & 0xFF), ((p >> 16) & 0xFF), ((p >> 8) & 0xFF), ( p & 0xFF)).mkString(".")
  }
  
  def ipv4StringToInteger(ipstr: String): Int = {
    (ipstr.split("\\.").reverse.zipWithIndex.map(p => p._1.toInt * math.pow(256 , p._2).toLong).sum ^ (1 << 31)).toInt
  }
  
  def ipv6LongsToString(lower: Long, upper: Long): String = {
    var l1 = lower
    var l2 = upper ^ (1l << 63)
    val iparr = for (i <- 0 until 8) yield {
      if (i < 4) {
        val s = (l1 & 0xFFFF).toHexString
        l1 = l1 >> 16
        s
      } else {
        val s = (l2 & 0xFFFF).toHexString
        l2 = l2 >> 16
        s
      }
    }
    var ip = iparr.reverse.mkString(":")
    
    if (ip.contains("0:0")) {
      ip = ip.replaceAll("0(:0)+", ":")
      if (ip.length == 1)
        ip += ":"
    }
    ip
  }
  
  def ipv6StringToLongs(ipstr: String): (Long, Long) = {
    val addr = InetAddress.getByName(ipstr)
    val value = new BigInteger(1, addr.getAddress)
    val lower = value.longValue()
    val upper = (value.shiftRight(64)).longValue() ^ (1l << 63)
    (lower, upper)
  }
} 
Example 22
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 23
Source File: UserAuthService.scala    From akka-http-rest-api   with MIT License 5 votes vote down vote up
package authentication

import java.math.BigInteger
import java.security.SecureRandom

import authentication.UserAuthResult.{UserNotExists, UserExists, Success, InvalidData}
import core.authentication.Identity
import org.joda.time.DateTime
import org.mindrot.jbcrypt.BCrypt
import redis.RedisClient
import token.TokenRepository
import user.{UserRepository, User}
import core.authentication.UserSerializer._

import scala.concurrent.{Future, ExecutionContext}

class UserAuthService(tokenRepo: TokenRepository, userAuthRepo: UserAuthRepository, userRepo: UserRepository)
                     (implicit ec: ExecutionContext, redisClient: RedisClient) {

  def register(request: UserRegistrationRequest): Future[UserAuthResult] = {
    if (!RegistrationValidator.isDataValid(request.email, request.password)) {
      Future.successful(InvalidData("E-mail or password is invalid"))
    } else {
      userAuthRepo.findByUserEmail(request.email).flatMap {
        case Some(userAuth) => Future.successful(UserExists("E-mail already in use"))
        case None =>
          val now = DateTime.now().getMillis
          val token = generateToken
          for {
            user <- userRepo.insert(User(None, now, request.firstName, request.lastName, request.gender, None, None, None, request.role))
            _ <- userAuthRepo.insert(UserAuth(None, request.email, hashPassword(request.password), user.id.get))
            _ <- tokenRepo.insert(token, user.id.get)
            _ <- redisClient.setnx(token, Identity.User(user.id.get, user.role))
          } yield {
            Success(token)
          }
      }
    }
  }

  def login(request: UserLoginRequest): Future[UserAuthResult] = {
    userAuthRepo.findByUserEmail(request.email).flatMap {
      case Some(userAuth) if checkPassword(request.password, userAuth.password) =>
        val token = generateToken
        for {
          Some(user) <- userRepo.findByUserId(userAuth.userId)
          _ <- tokenRepo.insert(token, user.id.get)
          _ <- redisClient.setnx(token, Identity.User(user.id.get, user.role))
        } yield {
          Success(token)
        }
      case Some(_) => Future.successful(InvalidData("Password is invalid"))
      case None => Future.successful(UserNotExists("E-mail does not exist"))
    }
  }

  private lazy val random = new SecureRandom()

  private def generateToken: String = new BigInteger(255, random).toString(32)

  private def hashPassword(password: String): String = BCrypt.hashpw(password, BCrypt.gensalt(12))

  private def checkPassword(password: String, passwordHash: String): Boolean = BCrypt.checkpw(password, passwordHash)
}

trait UserAuthResult

object UserAuthResult {

  case class InvalidData(msg: String) extends UserAuthResult

  case class UserExists(msg: String) extends UserAuthResult

  case class UserNotExists(msg: String) extends UserAuthResult

  case class Success(token: String) extends UserAuthResult

} 
Example 24
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 25
Source File: UnionGen.scala    From msgpack4z-core   with MIT License 5 votes vote down vote up
package msgpack4z

import java.math.BigInteger
import scalaprops._
import scalaprops.ScalapropsScalaz._
import scalaz.syntax.functor._

object UnionGen {
  val booleanGen: Gen[MsgpackUnion] =
    Gen.elements(
      MsgpackUnion.True,
      MsgpackUnion.False
    )

  val longGen: Gen[MsgpackUnion] =
    Gen.from1(MsgpackLong)

  val ulongGen: Gen[MsgpackUnion] =
    Gen
      .chooseLong(0, Long.MaxValue)
      .flatMap(i =>
        Gen.elements(
          MsgpackULong(BigInteger.valueOf(i)),
          MsgpackULong(BigInteger.valueOf(i).add(BigInteger.valueOf(i - 1L)))
        )
      )

  val integerGen: Gen[MsgpackUnion] =
    Gen.oneOf(ulongGen, longGen)

  implicit val scalaDoubleGen: Gen[Double] =
    Gen[Long].map { n =>
      java.lang.Double.longBitsToDouble(n) match {
        case x if x.isNaN => n.toDouble
        case x => x
      }
    }

  implicit val scalaFloatGen: Gen[Float] =
    Gen[Int].map { n =>
      java.lang.Float.intBitsToFloat(n) match {
        case x if x.isNaN => n.toFloat
        case x => x
      }
    }

  val doubleGen: Gen[MsgpackUnion] =
    Gen.oneOf(
      Gen[Double].map(MsgpackUnion.double),
      Gen[Float].map(MsgpackUnion.float)
    )

  val numberGen: Gen[MsgpackUnion] =
    Gen.oneOf(integerGen, doubleGen)

  val stringGen: Gen[MsgpackUnion] =
    Gen.alphaNumString.map(MsgpackString)

  val binaryGen: Gen[MsgpackUnion] =
    Gen.from1(MsgpackBinary)

  val unionGen0: Gen[MsgpackUnion] =
    Gen.frequency(
      1 -> Gen.value(MsgpackUnion.nil),
      1 -> booleanGen,
      6 -> numberGen,
      6 -> stringGen,
      3 -> binaryGen
    )

  val arrayGen: Gen[MsgpackUnion] =
    Gen.list(unionGen0).map(MsgpackArray)

  val mapGen: Gen[MsgpackUnion] =
    Gen.mapGen(unionGen0, unionGen0).map(MsgpackMap)

  val extGen: Gen[MsgpackExt] =
    Gen.from2(MsgpackExt.apply)

  val unionWithoutExtGen: Gen[MsgpackUnion] =
    Gen.oneOf(
      unionGen0,
      arrayGen,
      mapGen,
      stringGen,
      binaryGen
    )

  implicit val unionGen: Gen[MsgpackUnion] =
    Gen.oneOf(
      unionGen0,
      arrayGen,
      mapGen,
      stringGen,
      extGen.widen,
      binaryGen
    )
} 
Example 26
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 27
Source File: CacheChecksum.scala    From coursier   with Apache License 2.0 5 votes vote down vote up
package coursier.cache

import java.math.BigInteger
import java.nio.charset.StandardCharsets
import java.util.regex.Pattern

object CacheChecksum {

  private val checksumLength = Set(
    32, // md5
    40, // sha-1
    64, // sha-256
    128 // sha-512
  )

  private def ifHexString(s: String) =
    s.forall(c => c.isDigit || c >= 'a' && c <= 'z')

  private def findChecksum(elems: Seq[String]): Option[BigInteger] =
    elems.collectFirst {
      case rawSum if ifHexString(rawSum) && checksumLength.contains(rawSum.length) =>
        new BigInteger(rawSum, 16)
    }

  private def parseChecksumLine(lines: Seq[String]): Option[BigInteger] =
    findChecksum(lines.map(_.toLowerCase.replaceAll("\\s", "")))

  private def parseChecksumAlternative(lines: Seq[String]): Option[BigInteger] =
    findChecksum(lines.flatMap(_.toLowerCase.split("\\s+"))).orElse {
      findChecksum(
        lines.map { line =>
          line
            .toLowerCase
            .split("\\s+")
            .filter(ifHexString)
            .mkString
        }
      )
    }

  def parseChecksum(content: String): Option[BigInteger] = {
    val lines = Predef.augmentString(content)
      .lines
      .toVector

    parseChecksumLine(lines).orElse(parseChecksumAlternative(lines))
  }

  def parseRawChecksum(content: Array[Byte]): Option[BigInteger] =
    if (content.length == 16 || content.length == 20)
      Some(new BigInteger(content))
    else {
      val s = new String(content, StandardCharsets.UTF_8)
      val lines = Predef.augmentString(s)
        .lines
        .toVector

      parseChecksumLine(lines) orElse parseChecksumAlternative(lines)
    }

} 
Example 28
Source File: package.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package special

import java.math.BigInteger

import org.bouncycastle.math.ec.ECPoint
import scalan.RType
import scalan.RType.GeneralType

import scala.reflect.{ClassTag, classTag}

package sigma {

  case class ArgType(override val name: String) extends RType[Any] {
    override def classTag: ClassTag[Any] = ClassTag.Any
    override def isConstantSize: Boolean = false  // pessimistic but safe default
  }
}

package object sigma {

  implicit val BigIntRType: RType[BigInt] = new GeneralType(classTag[BigInt]) {
    override def isConstantSize: Boolean = true
  }
  implicit val GroupElementRType: RType[GroupElement] = new GeneralType(classTag[GroupElement]) {
    override def isConstantSize: Boolean = true
  }
  implicit val SigmaPropRType: RType[SigmaProp] = new GeneralType(classTag[SigmaProp]) {
    override def isConstantSize: Boolean = true
  }
  implicit val AvlTreeRType:   RType[AvlTree]   = new GeneralType(classTag[AvlTree]) {
    override def isConstantSize: Boolean = true
  }

  implicit val BoxRType:       RType[Box]       = GeneralType(classTag[Box])
  implicit val ContextRType:   RType[Context]   = GeneralType(classTag[Context])

  // these are not wrapper types since they are used directly in ErgoTree values (e.g. Constants)
  // and no conversion is necessary
  implicit val HeaderRType: RType[Header] = new GeneralType(classTag[Header]) {
    override def isConstantSize: Boolean = true
  }
  implicit val PreHeaderRType: RType[PreHeader] = new GeneralType(classTag[PreHeader]) {
    override def isConstantSize: Boolean = true
  }

  implicit val AnyValueRType: RType[AnyValue] = RType.fromClassTag(classTag[AnyValue])
  implicit val CostModelRType: RType[CostModel] = RType.fromClassTag(classTag[CostModel])


  implicit val SigmaContractRType: RType[SigmaContract] = RType.fromClassTag(classTag[SigmaContract])
  implicit val SigmaDslBuilderRType: RType[SigmaDslBuilder] = RType.fromClassTag(classTag[SigmaDslBuilder])

  implicit val BigIntegerRType: RType[BigInteger] = new GeneralType(classTag[BigInteger]) {
    override def isConstantSize: Boolean = true
  }
  implicit val ECPointRType: RType[ECPoint] = new GeneralType(classTag[ECPoint]) {
    override def isConstantSize: Boolean = true
  }


  implicit val SizeAnyValueRType: RType[SizeAnyValue] = RType.fromClassTag(classTag[SizeAnyValue])
  implicit val SizeSigmaPropRType: RType[SizeSigmaProp] = RType.fromClassTag(classTag[SizeSigmaProp])
  implicit val SizeBoxRType: RType[SizeBox] = RType.fromClassTag(classTag[SizeBox])
  implicit val SizeContextRType: RType[SizeContext] = RType.fromClassTag(classTag[SizeContext])
  implicit val SizeBuilderRType: RType[SizeBuilder] = RType.fromClassTag(classTag[SizeBuilder])

  def argRType(name: String): RType[Any] = ArgType(name)
} 
Example 29
Source File: CrowdFundingKernelContract.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate.utxo.benchmarks

import java.math.BigInteger
import java.util

import org.ergoplatform.ErgoLikeContext
import sigmastate.basics.DLogProtocol.{DLogInteractiveProver, DLogProverInput, FirstDLogProverMessage, ProveDlog}
import sigmastate.basics.VerifierMessage.Challenge
import scorex.crypto.hash.Blake2b256
import sigmastate._
import sigmastate.lang.Terms._
import sigmastate.helpers.ContextEnrichingTestProvingInterpreter
import sigmastate.interpreter.{CryptoConstants, Interpreter}
import sigmastate.utils.Helpers

import scala.util.Try

class CrowdFundingKernelContract(
                                  timeout: Int,
                                  minToRaise: Long,
                                  override val backerProver: ContextEnrichingTestProvingInterpreter,
                                  override val projectProver: ContextEnrichingTestProvingInterpreter
) extends CrowdFundingContract(timeout, minToRaise, backerProver, projectProver) {

  def isProven(pubKey: ProveDlog, message: Array[Byte]): projectProver.ProofT = {
    import projectProver._
    var su = UnprovenSchnorr(pubKey, None, None, None, simulated = false)
    val secret = secrets.find {
      case in: DLogProverInput => in.publicImage == pubKey
      case _ => false
    }
    val secretKnown = secret.isDefined
    val simulated = !secretKnown
    val step4: UnprovenTree = if (simulated) {
      assert(su.challengeOpt.isDefined)
      DLogInteractiveProver.simulate(su.proposition,su.challengeOpt.get).asInstanceOf[UnprovenTree]
    } else {
      val (r, commitment) = DLogInteractiveProver.firstMessage(pubKey)
      UnprovenSchnorr(pubKey, Some(commitment), Some(r), None, simulated = false)
    }

    val commitments = step4 match {
      case ul: UnprovenLeaf => ul.commitmentOpt.toSeq
      case _ => ???
       // can't do this anymore because internal nodes no longer have commitments
    }

    val rootChallenge = Challenge @@ Blake2b256(Helpers.concatBytes(commitments.map(_.bytes) :+ message))

    su = step4.asInstanceOf[UnprovenSchnorr]
    val privKey = secret.get.asInstanceOf[DLogProverInput]
    val z = DLogInteractiveProver.secondMessage(privKey, su.randomnessOpt.get, rootChallenge)
    UncheckedSchnorr(su.proposition, None, rootChallenge, z)
  }

  def prove(ctx: ErgoLikeContext, message: Array[Byte]): Array[Byte] = {
    val c1 = ctx.preHeader.height >= timeout //&& isProven(backerPubKey, fakeMessage)
    val c2 = Array(
      ctx.preHeader.height < timeout,
      ctx.spendingTransaction.outputs.exists(out => {
        out.value >= minToRaise &&
          util.Arrays.equals(out.propositionBytes, projectPubKey.toSigmaProp.treeWithSegregation.bytes)
      })
    ).forall(identity)
    var proof: projectProver.ProofT = null
    c1 || (c2 && { proof = isProven(projectPubKey, message); true})
    SigSerializer.toBytes(proof)
  }

  def verify(proof: Array[Byte],
             ctx: ErgoLikeContext,
             message: Array[Byte]): Try[Interpreter.VerificationResult] = Try {
    val sn = proof.asInstanceOf[UncheckedSchnorr]
    val dlog = CryptoConstants.dlogGroup
    val g = dlog.generator
    val h = sn.proposition.h

    val a = dlog.multiplyGroupElements(
      dlog.exponentiate(g, sn.secondMessage.z.underlying()),
      dlog.getInverse(dlog.exponentiate(h, new BigInteger(1, sn.challenge))))

    val rootCommitment = FirstDLogProverMessage(a)

    val expectedChallenge = Blake2b256(Helpers.concatBytes(Seq(rootCommitment.bytes, message)))
    util.Arrays.equals(sn.challenge, expectedChallenge) -> 0
  }
} 
Example 30
Source File: LangTests.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate.lang

import sigmastate.lang.Terms.{MethodCallLike, Ident}
import sigmastate.Values.{LongConstant, SValue, Value, SigmaBoolean, GroupElementConstant, ConcreteCollection}
import sigmastate._
import java.math.BigInteger

import org.bouncycastle.math.ec.ECPoint
import org.scalatest.Matchers
import sigmastate.basics.DLogProtocol.ProveDlog
import sigmastate.SCollection.SByteArray
import sigmastate.basics.ProveDHTuple
import sigmastate.interpreter.CryptoConstants
import sigmastate.interpreter.Interpreter.ScriptEnv
import special.sigma._
import sigmastate.eval._
import special.collection.Coll

trait LangTests extends Matchers {

  def BoolIdent(name: String): Value[SBoolean.type] = Ident(name).asValue[SBoolean.type]
  def IntIdent(name: String): Value[SLong.type] = Ident(name).asValue[SLong.type]
  def ByteIdent(name: String): Value[SByte.type] = Ident(name).asValue[SByte.type]
  def ByteArrayIdent(name: String): Value[SByteArray] = Ident(name).asValue[SByteArray]
  def GEIdent(name: String): Value[SGroupElement.type] = Ident(name).asValue[SGroupElement.type]
  def SigmaPropIdent(name: String): Value[SSigmaProp.type] = Ident(name).asValue[SSigmaProp.type]
  def BigIntIdent(name: String): Value[SBigInt.type] = Ident(name).asValue[SBigInt.type]

  def plus(l: SValue, r: SValue, tpe: SType = NoType): MethodCallLike =
    MethodCallLike(l, "+", IndexedSeq(r), tpe)

  val EV: ScriptEnv = Map()

  val arr1 = Array[Byte](1, 2)
  val arr2 = Array[Byte](10, 20)
  val dlog = CryptoConstants.dlogGroup
  val ecp1 = dlog.generator
  val ecp2 = dlog.multiplyGroupElements(ecp1, ecp1)
  val ecp3 = dlog.multiplyGroupElements(ecp2, ecp2)
  val ecp4 = dlog.multiplyGroupElements(ecp3, ecp3)
  val g1 = CostingSigmaDslBuilder.GroupElement(ecp1.asInstanceOf[ECPoint])
  val g2 = CostingSigmaDslBuilder.GroupElement(ecp2.asInstanceOf[ECPoint])
  val g3 = CostingSigmaDslBuilder.GroupElement(ecp3.asInstanceOf[ECPoint])
  val g4 = CostingSigmaDslBuilder.GroupElement(ecp4.asInstanceOf[ECPoint])

  protected val n1: BigInt = BigInt(10).underlying()
  protected val n2: BigInt = BigInt(20).underlying()
  protected val bigIntegerArr1: Coll[BigInt] = Colls.fromItems(n1, n2)
  protected val big: BigInteger = BigInt(Long.MaxValue).underlying().pow(2)
  protected val p1: SigmaBoolean = ProveDlog(ecp1)
  protected val p2: SigmaBoolean = ProveDlog(ecp2)
  protected val dht1: SigmaBoolean = ProveDHTuple(ecp1, ecp2, ecp3, ecp4)

  val env = Map(
    "x" -> 10, "y" -> 11, "c1" -> true, "c2" -> false,
    "height1" -> 100L, "height2" -> 200L,
    "b1" -> 1.toByte,
    "b2" -> 2.toByte,
    "arr1" -> arr1,
    "arr2" -> arr2,
    "col1" -> ConcreteCollection.fromItems(LongConstant(1), LongConstant(2)),
    "col2" -> ConcreteCollection.fromItems(LongConstant(10), LongConstant(20)),
    "g1" -> g1,
    "g2" -> g2,
    "p1" -> p1,
    "p2" -> p2,
    "n1" -> n1,
    "n2" -> n2,
    "big" -> big,
    "bigIntArr1" -> bigIntegerArr1
  )

  
  def ty(s: String): SType = SigmaParser.parseType(s)

  def assertSrcCtxForAllNodes(tree: SValue): Unit = {
    import org.bitbucket.inkytonik.kiama.rewriting.Rewriter._
    rewrite(everywherebu(rule[SValue] {
      case node =>
        withClue(s"Missing sourceContext for $node") { node.sourceContext.isDefined shouldBe true }
        node
    }))(tree)
  }
} 
Example 31
Source File: GroupLawsSpecification.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate.crypto

import java.math.BigInteger

import sigmastate.helpers.SigmaTestingCommons
import sigmastate.interpreter.CryptoConstants

class GroupLawsSpecification extends SigmaTestingCommons {
  private val group = CryptoConstants.dlogGroup

  property("multiplication law is complete") {
    val identity = group.identity
    val ge = group.createRandomGenerator()

    group.multiplyGroupElements(ge, ge) shouldBe group.exponentiate(ge, new BigInteger("2"))
    group.multiplyGroupElements(ge, identity) shouldBe group.exponentiate(ge, BigInteger.ONE)
    group.multiplyGroupElements(ge, identity) shouldBe ge
    group.multiplyGroupElements(identity, identity) shouldBe identity

    val inverse = group.getInverse(ge)
    group.multiplyGroupElements(ge, inverse) shouldBe identity
  }

  property("exponentiation") {
    val identity = group.identity
    val ge = group.createRandomGenerator()

    group.exponentiate(ge, BigInteger.ZERO) shouldBe identity
    group.exponentiate(ge, BigInteger.ONE) shouldBe ge
    group.exponentiate(ge, group.order) shouldBe identity
    group.exponentiate(ge, group.order.add(BigInteger.ONE)) shouldBe ge
  }

} 
Example 32
Source File: BasicOpsTests.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate.eval

import java.math.BigInteger

import org.bouncycastle.crypto.ec.CustomNamedCurves
import org.scalatest.{FunSuite, Matchers}
import special.sigma.{Box, Context, ContractsTestkit, MockSigma, SigmaContract, SigmaDslBuilder, SigmaProp, TestSigmaDslBuilder}

import scala.language.implicitConversions

class BasicOpsTests extends FunSuite with ContractsTestkit with Matchers {
  override val SigmaDsl: SigmaDslBuilder = CostingSigmaDslBuilder

  implicit def boolToSigma(b: Boolean): SigmaProp = MockSigma(b)

  test("atLeast") {
    val props = Colls.fromArray(Array[SigmaProp](false, true, true, false))

    // border cases
    SigmaDsl.atLeast(0, props).isValid shouldBe true
    SigmaDsl.atLeast(5, props).isValid shouldBe false

    // normal cases
    SigmaDsl.atLeast(1, props).isValid shouldBe true
    SigmaDsl.atLeast(2, props).isValid shouldBe true
    SigmaDsl.atLeast(3, props).isValid shouldBe false
  }

  // TODO this is valid for BigIntModQ type (https://github.com/ScorexFoundation/sigmastate-interpreter/issues/554)
  ignore("ByteArrayToBigInt should always produce a positive big int") {
    SigmaDsl.byteArrayToBigInt(collection[Byte](-1)).signum shouldBe 1
  }

  // TODO this is valid for BigIntModQ type (https://github.com/ScorexFoundation/sigmastate-interpreter/issues/554)
  ignore("ByteArrayToBigInt should always produce big int less than dlog group order") {
    val groupOrder = CustomNamedCurves.getByName("secp256k1").getN

    SigmaDsl.byteArrayToBigInt(
      Colls.fromArray(groupOrder.subtract(BigInteger.ONE).toByteArray)
    ).compareTo(SigmaDsl.BigInt(BigInteger.ONE)) shouldBe 1

    SigmaDsl.byteArrayToBigInt(
      Colls.fromArray(groupOrder.toByteArray)
    ).compareTo(SigmaDsl.BigInt(BigInteger.ONE)) shouldBe 1

    an [RuntimeException] should be thrownBy
      SigmaDsl.byteArrayToBigInt(Colls.fromArray(groupOrder.add(BigInteger.ONE).toByteArray))

    an [RuntimeException] should be thrownBy
      SigmaDsl.byteArrayToBigInt(Colls.fromArray(Array.fill[Byte](500)(1)))
  }

  test("Coll.append")  {
    val c1 = collection[Byte](1, 2)
    val c2 = collection[Byte](3, 4)
    c1.append(c2).toArray shouldBe Array[Byte](1, 2, 3, 4)
  }

  test("box.creationInfo._1 is Int") {
    val box = newAliceBox(1, 100)
    box.creationInfo._1 shouldBe a [Integer]
  }

} 
Example 33
Source File: DataSerializerSpecification.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate.serialization

import java.math.BigInteger

import org.ergoplatform.ErgoBox
import org.scalacheck.Arbitrary._
import scalan.RType
import sigmastate.SCollection.SByteArray
import sigmastate.Values.SigmaBoolean
import sigmastate._
import sigmastate.eval.Evaluation
import sigmastate.eval._
import sigmastate.eval.Extensions._
import sigmastate.interpreter.CryptoConstants.EcPointType
import special.sigma.AvlTree
import SType.AnyOps

class DataSerializerSpecification extends SerializationSpecification {

  def roundtrip[T <: SType](obj: T#WrappedType, tpe: T) = {
    val w = SigmaSerializer.startWriter()
    DataSerializer.serialize(obj, tpe, w)
    val bytes = w.toBytes
    val r = SigmaSerializer.startReader(bytes, 0)
    val res = DataSerializer.deserialize(tpe, r)
    res shouldBe obj
    val randomPrefix = arrayGen[Byte].sample.get
    val r2 = SigmaSerializer.startReader(randomPrefix ++ bytes, randomPrefix.length)
    val res2 = DataSerializer.deserialize(tpe, r2)
    res2 shouldBe obj
  }

  def testCollection[T <: SType](tpe: T) = {
    implicit val wWrapped = wrappedTypeGen(tpe)
    implicit val tT = Evaluation.stypeToRType(tpe)
    implicit val tagT = tT.classTag
    implicit val tAny = RType.AnyType
    forAll { xs: Array[T#WrappedType] =>
      roundtrip[SCollection[T]](xs.toColl, SCollection(tpe))
      roundtrip[SType](xs.toColl.map(x => (x, x)).asWrappedType, SCollection(STuple(tpe, tpe)))

      val triples = xs.toColl.map(x => TupleColl(x, x, x)).asWrappedType
      roundtrip(triples, SCollection(STuple(tpe, tpe, tpe)))

      val quartets = xs.toColl.map(x => TupleColl(x, x, x, x)).asWrappedType
      roundtrip(quartets, SCollection(STuple(tpe, tpe, tpe, tpe)))

      val nested = xs.toColl.map(x => Colls.fromItems[T#WrappedType](x, x))
      roundtrip[SCollection[SCollection[T]]](nested, SCollection(SCollection(tpe)))

      roundtrip[SType](
        xs.toColl.map { x =>
          val arr = Colls.fromItems[T#WrappedType](x, x)
          (arr, arr)
        }.asWrappedType,
        SCollection(STuple(SCollection(tpe), SCollection(tpe)))
      )
    }
  }

  def testTuples[T <: SType](tpe: T) = {
    implicit val wWrapped = wrappedTypeGen(tpe)
    implicit val tag = tpe.classTag[T#WrappedType]
    implicit val tAny = RType.AnyType
    forAll { in: (T#WrappedType, T#WrappedType) =>
      val (x,y) = (in._1, in._2)
      roundtrip[SType]((x, y).asWrappedType, STuple(tpe, tpe))
      roundtrip[SType](TupleColl(x, y, x).asWrappedType, STuple(tpe, tpe, tpe))
      roundtrip[SType](TupleColl(x, y, x, y).asWrappedType, STuple(tpe, tpe, tpe, tpe))
      roundtrip[STuple](Colls.fromItems[Any](x, y, (x, y)), STuple(tpe, tpe, STuple(tpe, tpe)))
      roundtrip[STuple](Colls.fromItems[Any](x, y, TupleColl(x, y, x)), STuple(tpe, tpe, STuple(tpe, tpe, tpe)))
      roundtrip[STuple](Colls.fromItems[Any](x, y, TupleColl(x, y, (x, y))), STuple(tpe, tpe, STuple(tpe, tpe, STuple(tpe, tpe))))
    }
  }

  property("Data serialization round trip") {
    forAll { x: Byte => roundtrip[SByte.type](x, SByte) }
    forAll { x: Boolean => roundtrip[SBoolean.type](x, SBoolean) }
    forAll { x: Long => roundtrip[SLong.type](x, SLong) }
    forAll { x: String => roundtrip[SString.type](x, SString) }
    forAll { x: BigInteger => roundtrip[SBigInt.type](x, SBigInt) }
    forAll { x: EcPointType => roundtrip[SGroupElement.type](x, SGroupElement) }
    forAll { x: SigmaBoolean => roundtrip[SSigmaProp.type](x, SSigmaProp) }
    forAll { x: ErgoBox => roundtrip[SBox.type](x, SBox) }
    forAll { x: AvlTree => roundtrip[SAvlTree.type](x, SAvlTree) }
    forAll { x: Array[Byte] => roundtrip[SByteArray](x.toColl, SByteArray) }
    forAll { t: SPredefType => testCollection(t) }
    forAll { t: SPredefType => testTuples(t) }
  }

} 
Example 34
Source File: package.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate

import java.math.BigInteger

import org.ergoplatform.ErgoBox
import scalan.RType
import scorex.crypto.hash.Digest32
import sigmastate.Values.SigmaBoolean
import sigmastate.interpreter.CryptoConstants.EcPointType
import special.collection.{Coll, CollBuilder}
import special.sigma._
import supertagged.TaggedType

import scala.language.implicitConversions

package object eval {
  
  implicit def bigIntegerToBigInt(bi: BigInteger): BigInt = SigmaDsl.BigInt(bi)
  implicit def bigIntToBigInteger(bi: BigInt): BigInteger = SigmaDsl.toBigInteger(bi)

  implicit def ecPointToGroupElement(p: EcPointType): GroupElement = SigmaDsl.GroupElement(p)
  implicit def groupElementToECPoint(p: GroupElement): EcPointType = SigmaDsl.toECPoint(p).asInstanceOf[EcPointType]

  implicit def sigmaBooleanToSigmaProp(p: SigmaBoolean): SigmaProp = SigmaDsl.SigmaProp(p)
  implicit def sigmaPropToSigmaBoolean(p: SigmaProp): SigmaBoolean = SigmaDsl.toSigmaBoolean(p)

  implicit def avlTreeDataToAvlTree(p: AvlTreeData): AvlTree = SigmaDsl.avlTree(p)
  implicit def avlTreeToAvlTreeData(p: AvlTree): AvlTreeData = SigmaDsl.toAvlTreeData(p)

  implicit def ergoBoxToBox(p: ErgoBox): Box = SigmaDsl.Box(p)
  implicit def boxToErgoBox(p: Box): ErgoBox = SigmaDsl.toErgoBox(p)
} 
Example 35
Source File: OrderBookSideSnapshotCodecs.scala    From matcher   with MIT License 5 votes vote down vote up
package com.wavesplatform.dex.codecs

import java.math.BigInteger
import java.nio.ByteBuffer

import com.google.common.primitives.{Ints, Longs}
import com.wavesplatform.dex.codecs.ByteBufferCodecs.ByteBufferExt
import com.wavesplatform.dex.domain.model.Price
import com.wavesplatform.dex.domain.order.{Order, OrderType}
import com.wavesplatform.dex.model.{BuyLimitOrder, LimitOrder, OrderBookSideSnapshot, SellLimitOrder}

import scala.collection.mutable

object OrderBookSideSnapshotCodecs {

  def encode(dest: mutable.ArrayBuilder[Byte], snapshot: OrderBookSideSnapshot): Unit = {
    dest ++= Ints.toByteArray(snapshot.size)
    snapshot.foreach {
      case (price, xs) =>
        dest ++= Longs.toByteArray(price)
        dest ++= Ints.toByteArray(xs.size)
        xs.foreach(encodeLoV2(dest, _))
    }
  }

  def decode(bb: ByteBuffer): OrderBookSideSnapshot = {
    val snapshotSize = bb.getInt
    val r            = Map.newBuilder[Price, Seq[LimitOrder]]
    (1 to snapshotSize).foreach { _ =>
      val price       = bb.getLong
      val levelSize   = bb.getInt
      val limitOrders = (1 to levelSize).map(_ => decodeLo(bb))
      r += price -> limitOrders
    }
    r.result()
  }

  def encodeLoV1(dest: mutable.ArrayBuilder[Byte], lo: LimitOrder): Unit = {

    dest ++= lo.order.orderType.bytes
    dest ++= Longs.toByteArray(lo.amount)
    dest ++= Longs.toByteArray(lo.fee)
    dest += lo.order.version

    val orderBytes = lo.order.bytes()

    dest ++= Ints.toByteArray(orderBytes.length)
    dest ++= orderBytes
  }

  def encodeLoV2(dest: mutable.ArrayBuilder[Byte], lo: LimitOrder): Unit = {
    val avgWeighedPriceNominatorBytes = lo.avgWeighedPriceNominator.toByteArray

    dest += 2

    encodeLoV1(dest, lo)

    dest ++= Ints.toByteArray(avgWeighedPriceNominatorBytes.length)
    dest ++= avgWeighedPriceNominatorBytes
  }

  def decodeLo(bb: ByteBuffer): LimitOrder = {

    val header    = bb.get
    val version   = if (header == 2) 2 else 1
    val orderType = if (version == 1) header else bb.get

    val amount       = bb.getLong
    val fee          = bb.getLong
    val orderVersion = bb.get
    val order        = Order.fromBytes(orderVersion, bb.getBytes)

    val avgWeighedPriceNominator =
      if (version == 2) new BigInteger(bb.getBytes)
      else {
        val filledAmount = order.amount - amount
        (BigInt(order.price) * filledAmount).bigInteger
      }

    OrderType(orderType) match {
      case OrderType.SELL => SellLimitOrder(amount, fee, order, avgWeighedPriceNominator)
      case OrderType.BUY  => BuyLimitOrder(amount, fee, order, avgWeighedPriceNominator)
    }
  }
} 
Example 36
Source File: PossibleForgersSet.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.fixtures.sidechainblock.generation
import java.math.BigInteger
import java.util.{Random, ArrayList => JArrayList}

import com.horizen.consensus.{ConsensusSlotNumber, NonceConsensusEpochInfo, StakeConsensusEpochInfo, _}
import com.horizen.proof.VrfProof
import com.horizen.utils._
import com.horizen.vrf.VrfOutput

import scala.collection.immutable.TreeMap


class PossibleForgersSet(forgers: Set[PossibleForger]) {
  private val ordering: Ordering[SidechainForgingData] = Ordering[(Long, BigInteger)].on(x => (x.forgerBox.value(), new BigInteger(x.forgerId)))

  val forgingDataToPossibleForger: TreeMap[SidechainForgingData, PossibleForger] = TreeMap(forgers.map(pf => (pf.forgingData.copy(), pf.copy())).toArray:_*)(ordering.reverse)
  require(forgingDataToPossibleForger.size == forgers.size)

  def getRandomPossibleForger(rnd: Random): PossibleForger = forgers.toSeq(rnd.nextInt(forgers.size))

  def getAvailableSidechainForgingData: Set[SidechainForgingData] = forgingDataToPossibleForger.keys.to

  def getNotSpentSidechainForgingData: Set[SidechainForgingData] = forgingDataToPossibleForger.filter{case (forgingData, possibleForger) => possibleForger.isNotSpent}.keys.to

  def getEligibleForger(slotNumber: ConsensusSlotNumber, nonceConsensusEpochInfo: NonceConsensusEpochInfo, totalStake: Long, additionalCheck: Boolean => Boolean): Option[(PossibleForger, VrfProof, VrfOutput)] = {
    val vrfMessage = buildVrfMessage(slotNumber, nonceConsensusEpochInfo)
    forgingDataToPossibleForger
      .values
      .view
      .flatMap{forger => forger.canBeForger(vrfMessage, totalStake, additionalCheck).map{case (proof, vrfOutput) => (forger, proof, vrfOutput)}} //get eligible forgers
      .headOption
  }

  def finishCurrentEpoch(): (PossibleForgersSet, StakeConsensusEpochInfo) = {
    val possibleForgersForNextEpoch: Seq[PossibleForger] = getPossibleForgersForNextEpoch

    val totalStake = possibleForgersForNextEpoch.withFilter(_.isNotSpent).map(_.forgingData.forgerBox.value()).sum
    val merkleTreeForEndOfEpoch: MerkleTree = buildMerkleTree(possibleForgersForNextEpoch.map(_.forgingData.forgerBox.id()))
    val merkleTreeForEndOfEpochRootHash = merkleTreeForEndOfEpoch.rootHash()

    val forgingDataWithUpdatedMerkleTreePathAndMaturity: Seq[PossibleForger] =
      possibleForgersForNextEpoch
        .zipWithIndex.map{case (possibleForger, index) =>
        possibleForger.createPossibleForgerForTheNextEpoch(Some(merkleTreeForEndOfEpoch.getMerklePathForLeaf(index)))}

    val stakeEpochInfo: StakeConsensusEpochInfo = StakeConsensusEpochInfo(merkleTreeForEndOfEpochRootHash, totalStake)
    val newForgers = new PossibleForgersSet(Set(forgingDataWithUpdatedMerkleTreePathAndMaturity.toArray:_*))

    (newForgers, stakeEpochInfo)
  }

  private def getPossibleForgersForNextEpoch: Seq[PossibleForger] = {
    forgingDataToPossibleForger.values.filter(_.couldBePossibleForgerInNextEpoch).to
  }

  def createModified(generationRules: GenerationRules): PossibleForgersSet = {

    val forgingBoxToSpent = generationRules.forgingBoxesToSpent
    val forgingBoxToAdd = generationRules.forgingBoxesToAdd

    val withoutSpentKeys = forgingDataToPossibleForger.keys.toSet -- forgingBoxToSpent
    require((withoutSpentKeys.size + forgingBoxToSpent.size) == forgingDataToPossibleForger.size)

    val withoutSpentPossibleForgers: Set[PossibleForger] = withoutSpentKeys.collect(forgingDataToPossibleForger)
    val spentPossibleForgers: Set[PossibleForger] = forgingBoxToSpent.collect(forgingDataToPossibleForger).map(possibleForger => possibleForger.copy(spentInEpochsAgoOpt = Some(0)))
    val newPossibleForgers: Set[PossibleForger] = forgingBoxToAdd.map(PossibleForger(_, merklePathInPreviousEpochOpt = None, merklePathInPrePreviousEpochOpt = None, spentInEpochsAgoOpt = None))

    new PossibleForgersSet(withoutSpentPossibleForgers ++ spentPossibleForgers ++ newPossibleForgers)
  }

  private def buildMerkleTree(ids: Iterable[Array[Byte]]): MerkleTree = {
    val leavesForMerkleTree =
      ids.foldLeft(new JArrayList[Array[Byte]]()) {(acc, id) =>
        acc.add(id)
        acc
      }

    val filler = leavesForMerkleTree.get(leavesForMerkleTree.size() - 1)
    val additionalLeaves = SidechainBlocksGenerator.merkleTreeSize - leavesForMerkleTree.size()
    (1 to additionalLeaves).foreach(_ => leavesForMerkleTree.add(filler))

    val resultTree = MerkleTree.createMerkleTree(leavesForMerkleTree)
    resultTree
  }
} 
Example 37
Source File: package.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen

import java.math.{BigDecimal, BigInteger, MathContext}

import com.google.common.primitives.{Bytes, Ints}
import com.horizen.vrf.VrfOutput
import scorex.util.ModifierId
import supertagged.TaggedType

package object consensus {
  val merkleTreeHashLen: Int = 32
  val sha256HashLen: Int = 32

  val consensusHardcodedSaltString: Array[Byte] = "TEST".getBytes()
  val forgerStakePercentPrecision: BigDecimal = BigDecimal.valueOf(1000000) // where 1 / forgerStakePercentPrecision -- minimal possible forger stake percentage to be able to forge
  val stakeConsensusDivideMathContext: MathContext = MathContext.DECIMAL128 //shall be used during dividing, otherwise ArithmeticException is thrown in case of irrational number as division result

  object ConsensusEpochNumber extends TaggedType[Int]
  type ConsensusEpochNumber = ConsensusEpochNumber.Type
  def intToConsensusEpochNumber(consensusEpochNumber: Int): ConsensusEpochNumber = ConsensusEpochNumber @@ consensusEpochNumber

  
  object ConsensusEpochId extends TaggedType[String]
  type ConsensusEpochId = ConsensusEpochId.Type
  def blockIdToEpochId(blockId: ModifierId): ConsensusEpochId = ConsensusEpochId @@ blockId
  def lastBlockIdInEpochId(epochId: ConsensusEpochId): ModifierId = ModifierId @@ epochId.untag(ConsensusEpochId)

  object ConsensusSlotNumber extends TaggedType[Int]
  type ConsensusSlotNumber = ConsensusSlotNumber.Type
  def intToConsensusSlotNumber(consensusSlotNumber: Int): ConsensusSlotNumber = ConsensusSlotNumber @@ consensusSlotNumber

  //Slot number starting from genesis block
  object ConsensusAbsoluteSlotNumber extends TaggedType[Int]
  type ConsensusAbsoluteSlotNumber = ConsensusAbsoluteSlotNumber.Type
  def intToConsensusAbsoluteSlotNumber(consensusSlotNumber: Int): ConsensusAbsoluteSlotNumber = ConsensusAbsoluteSlotNumber @@ consensusSlotNumber


  object ConsensusNonce extends TaggedType[Array[Byte]]
  type ConsensusNonce = ConsensusNonce.Type
  def byteArrayToConsensusNonce(bytes: Array[Byte]): ConsensusNonce = ConsensusNonce @@ bytes

  object VrfMessage extends TaggedType[Array[Byte]]
  type VrfMessage = VrfMessage.Type

  def buildVrfMessage(slotNumber: ConsensusSlotNumber, nonce: NonceConsensusEpochInfo): VrfMessage = {
    val slotNumberBytes = Ints.toByteArray(slotNumber)
    val nonceBytes = nonce.consensusNonce

    val resBytes = Bytes.concat(slotNumberBytes, nonceBytes, consensusHardcodedSaltString)
    VrfMessage @@ resBytes
  }

  def vrfOutputToPositiveBigInteger(vrfOutput: VrfOutput): BigInteger = {
    new BigInteger(1, vrfOutput.bytes())
  }

  def vrfProofCheckAgainstStake(vrfOutput: VrfOutput, actualStake: Long, totalStake: Long): Boolean = {
    val requiredStakePercentage: BigDecimal = vrfOutputToRequiredStakePercentage(vrfOutput)
    val actualStakePercentage: BigDecimal = new BigDecimal(actualStake).divide(new BigDecimal(totalStake), stakeConsensusDivideMathContext)

    requiredStakePercentage.compareTo(actualStakePercentage) match {
      case -1 => true //required percentage is less than actual
      case  0 => true //required percentage is equal to actual
      case  _ => false //any other case
    }
  }

  // @TODO shall be changed by adding "active slots coefficient" according to Ouroboros Praos Whitepaper (page 10)
  def vrfOutputToRequiredStakePercentage(vrfOutput: VrfOutput): BigDecimal = {
    val hashAsBigDecimal: BigDecimal = new BigDecimal(vrfOutputToPositiveBigInteger(vrfOutput))

    hashAsBigDecimal
      .remainder(forgerStakePercentPrecision) //got random number from 0 to forgerStakePercentPrecision - 1
      .divide(forgerStakePercentPrecision, stakeConsensusDivideMathContext) //got random number from 0 to 0.(9)
  }
} 
Example 38
Source File: MainNetParams.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.params
import java.math.BigInteger

import com.horizen.proposition.SchnorrProposition
import scorex.core.block.Block
import scorex.util.ModifierId
import scorex.util.bytesToId

case class MainNetParams(
                          override val sidechainId: Array[Byte] = new Array[Byte](32),
                          override val sidechainGenesisBlockId: ModifierId = bytesToId(new Array[Byte](32)),
                          override val genesisMainchainBlockHash: Array[Byte] = new Array[Byte](32),
                          override val parentHashOfGenesisMainchainBlock: Array[Byte] = new Array[Byte](32),
                          override val genesisPoWData: Seq[(Int, Int)] = Seq(),
                          override val mainchainCreationBlockHeight: Int = 1,
                          override val withdrawalEpochLength: Int = 100,
                          override val sidechainGenesisBlockTimestamp: Block.Timestamp = 720 * 120,
                          override val consensusSecondsInSlot: Int = 120,
                          override val consensusSlotsInEpoch: Int = 720,
                          override val signersPublicKeys: Seq[SchnorrProposition] = Seq(),
                          override val signersThreshold: Int = 0,
                          override val provingKeyFilePath: String = "",
                          override val verificationKeyFilePath: String = "",
                          override val calculatedSysDataConstant: Array[Byte] = Array()
                        ) extends NetworkParams {
  override val EquihashN: Int = 200
  override val EquihashK: Int = 9
  override val EquihashVarIntLength: Int = 3
  override val EquihashSolutionLength: Int = 1344

  override val powLimit: BigInteger = new BigInteger("0007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
  override val nPowAveragingWindow: Int = 17
  override val nPowMaxAdjustDown: Int = 32 // 32% adjustment down
  override val nPowMaxAdjustUp: Int = 16 // 16% adjustment up
  override val nPowTargetSpacing: Int = 150 // 2.5 * 60
} 
Example 39
Source File: NetworkParams.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.params

import java.math.BigInteger

import com.horizen.proposition.SchnorrProposition
import scorex.core.block.Block
import scorex.util.{ModifierId, bytesToId}

trait NetworkParams {
  // Mainchain ProofOfWork parameters:
  val EquihashN: Int
  val EquihashK: Int
  val EquihashVarIntLength: Int // VarInt value length for Equihash solution bytes
  val EquihashSolutionLength: Int // solution bytes length

  val powLimit: BigInteger

  val nPowAveragingWindow: Int
  val nPowMaxAdjustDown: Int
  val nPowMaxAdjustUp: Int
  val nPowTargetSpacing: Int
  final def averagingWindowTimespan: Int = nPowAveragingWindow * nPowTargetSpacing
  final def MinActualTimespan: Int = (averagingWindowTimespan * (100 - nPowMaxAdjustUp  )) / 100
  final def MaxActualTimespan: Int = (averagingWindowTimespan * (100 + nPowMaxAdjustDown)) / 100
  final def nMedianTimeSpan: Int = 11


  // Sidechain params:
  val zeroHashBytes: Array[Byte] = new Array[Byte](32)
  val sidechainId: Array[Byte]
  val sidechainGenesisBlockId: ModifierId
  val sidechainGenesisBlockParentId: ModifierId = bytesToId(new Array[Byte](32))
  val signersPublicKeys: Seq[SchnorrProposition]
  val signersThreshold: Int
  val provingKeyFilePath: String
  val verificationKeyFilePath: String
  val calculatedSysDataConstant: Array[Byte]

  val maxHistoryRewritingLength: Int = 100


  // Sidechain genesis params:
  val genesisMainchainBlockHash: Array[Byte] // hash of the block which include SidechainCreationTx for current SC
  val parentHashOfGenesisMainchainBlock: Array[Byte] // hash of the block which are parent for genesis MainchainBlock
  val genesisPoWData: Seq[(Int, Int)] // Tuples with timestamps and bits values of <nPowAveragingWindow> blocks up-to <genesisMainchainBlockHash> block. From oldest MC block to genesis one.
  val mainchainCreationBlockHeight: Int // Height of the block which include SidechainCreationTx for current SC
  val sidechainGenesisBlockTimestamp: Block.Timestamp
  val withdrawalEpochLength: Int
  val consensusSecondsInSlot: Int
  val consensusSlotsInEpoch: Int
} 
Example 40
Source File: RegTestParams.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.params
import java.math.BigInteger

import com.horizen.proposition.SchnorrProposition
import scorex.core.block.Block
import scorex.util.ModifierId
import scorex.util.bytesToId

case class RegTestParams(
                          override val sidechainId: Array[Byte] = new Array[Byte](32),
                          override val sidechainGenesisBlockId: ModifierId = bytesToId(new Array[Byte](32)),
                          override val genesisMainchainBlockHash: Array[Byte] = new Array[Byte](32),
                          override val parentHashOfGenesisMainchainBlock: Array[Byte] = new Array[Byte](32),
                          override val genesisPoWData: Seq[(Int, Int)] = Seq(),
                          override val mainchainCreationBlockHeight: Int = 1,
                          override val withdrawalEpochLength: Int = 100,
                          override val sidechainGenesisBlockTimestamp: Block.Timestamp = 720 * 120,
                          override val consensusSecondsInSlot: Int = 120,
                          override val consensusSlotsInEpoch: Int = 720,
                          override val signersPublicKeys: Seq[SchnorrProposition] = Seq(),
                          override val signersThreshold: Int = 0,
                          override val provingKeyFilePath: String = "",
                          override val verificationKeyFilePath: String = "",
                          override val calculatedSysDataConstant: Array[Byte] = Array()
) extends NetworkParams {
  override val EquihashN: Int = 48
  override val EquihashK: Int = 5
  override val EquihashVarIntLength: Int = 1
  override val EquihashSolutionLength: Int = 36

  override val powLimit: BigInteger = new BigInteger("0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f", 16)
  override val nPowAveragingWindow: Int = 17
  override val nPowMaxAdjustDown: Int = 0 // Turn off adjustment down
  override val nPowMaxAdjustUp: Int = 0 // Turn off adjustment up
  override val nPowTargetSpacing: Int = 150 // 2.5 * 60
} 
Example 41
Source File: TestNetParams.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.params
import java.math.BigInteger

import com.horizen.proposition.SchnorrProposition
import scorex.core.block.Block
import scorex.util.ModifierId
import scorex.util.bytesToId

case class TestNetParams(
                          override val sidechainId: Array[Byte] = new Array[Byte](32),
                          override val sidechainGenesisBlockId: ModifierId = bytesToId(new Array[Byte](32)),
                          override val genesisMainchainBlockHash: Array[Byte] = new Array[Byte](32),
                          override val parentHashOfGenesisMainchainBlock: Array[Byte] = new Array[Byte](32),
                          override val genesisPoWData: Seq[(Int, Int)] = Seq(),
                          override val mainchainCreationBlockHeight: Int = 1,
                          override val withdrawalEpochLength: Int = 100,
                          override val sidechainGenesisBlockTimestamp: Block.Timestamp = 720 * 120,
                          override val consensusSecondsInSlot: Int = 120,
                          override val consensusSlotsInEpoch: Int = 720,
                          override val signersPublicKeys: Seq[SchnorrProposition] = Seq(),
                          override val signersThreshold: Int = 0,
                          override val provingKeyFilePath: String = "",
                          override val verificationKeyFilePath: String = "",
                          override val calculatedSysDataConstant: Array[Byte] = Array()
                        ) extends NetworkParams {
  override val EquihashN: Int = 200
  override val EquihashK: Int = 9
  override val EquihashVarIntLength: Int = 3
  override val EquihashSolutionLength: Int = 1344

  override val powLimit: BigInteger = new BigInteger("07ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
  override val nPowAveragingWindow: Int = 17
  override val nPowMaxAdjustDown: Int = 32 // 32% adjustment down
  override val nPowMaxAdjustUp: Int = 16 // 16% adjustment up
  override val nPowTargetSpacing: Int = 150 // 2.5 * 60
} 
Example 42
Source File: Security.scala    From keycloak-benchmark   with Apache License 2.0 5 votes vote down vote up
package org.jboss.perf

import java.math.BigInteger
import java.security.spec.{PKCS8EncodedKeySpec, X509EncodedKeySpec}
import java.security._
import java.security.cert.X509Certificate
import java.util.concurrent.TimeUnit

import org.keycloak.common.util.Base64
import sun.security.x509._

import scala.util.Random


object Security {
  // not really secure; gives deterministic numbers
  private val secureRandom = new SecureRandom(new SecureRandomSpi {
    val random = new Random(1234L)
    override def engineGenerateSeed(numBytes: Int): Array[Byte] = {
      val bytes = new Array[Byte](numBytes)
      random.nextBytes(bytes)
      bytes
    }

    override def engineSetSeed(seed: Array[Byte]) {}

    override def engineNextBytes(bytes: Array[Byte]) {
      random.nextBytes(bytes)
    }

  }, new SecureRandom().getProvider) {}
  private val algorithm = "RSA"
  private val signingAlgorithm = "SHA256WithRSA"
  private val keyPair: KeyPair = {
    val generator = KeyPairGenerator.getInstance(algorithm)
    generator.initialize(2048, secureRandom)
    generator.genKeyPair()
  }

  val PublicKey =  Base64.encodeBytes(KeyFactory.getInstance(algorithm).getKeySpec(keyPair.getPublic, classOf[X509EncodedKeySpec]).getEncoded)
  val PrivateKey = Base64.encodeBytes(KeyFactory.getInstance(algorithm).getKeySpec(keyPair.getPrivate, classOf[PKCS8EncodedKeySpec]).getEncoded)
  val Certificate = Base64.encodeBytes(generateCertificate("CN=Benchmark", keyPair).getEncoded)

  private def generateCertificate(dn: String, pair: KeyPair): X509Certificate = {
    val info = new X509CertInfo();
    val from = new java.util.Date();
    val to = new java.util.Date(from.getTime() + TimeUnit.DAYS.toMillis(365));
    val interval = new CertificateValidity(from, to);
    val sn = new BigInteger(64, secureRandom);
    val owner = new X500Name(dn);

    info.set(X509CertInfo.VALIDITY, interval);
    info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn));
    info.set(X509CertInfo.SUBJECT, owner);
    info.set(X509CertInfo.ISSUER, owner);
//    Use following for Java < 1.8:
//    info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(owner));
//    info.set(X509CertInfo.ISSUER, new CertificateIssuerName(owner));
    info.set(X509CertInfo.KEY, new CertificateX509Key(pair.getPublic()));
    info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
    var algo = new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid);
    info.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algo));

    // Sign the cert to identify the algorithm that's used.
    var cert = new X509CertImpl(info);
    cert.sign(pair.getPrivate, signingAlgorithm);

    // Update the algorith, and resign.
    algo = cert.get(X509CertImpl.SIG_ALG).asInstanceOf[AlgorithmId];
    info.set(CertificateAlgorithmId.NAME + "." + CertificateAlgorithmId.ALGORITHM, algo);
    cert = new X509CertImpl(info);
    cert.sign(pair.getPrivate, signingAlgorithm);
    return cert;
  }
} 
Example 43
Source File: RiakKeysPartitioner.scala    From spark-riak-connector   with Apache License 2.0 5 votes vote down vote up
package com.basho.riak.spark.rdd.partitioner

import com.basho.riak.client.core.util.HostAndPort
import com.basho.riak.spark.query.QueryData
import com.basho.riak.spark.rdd.{ RiakPartition, ReadConf }
import org.apache.spark.Partition
import java.math.BigInteger

case class RiakKeysPartition[K](
  index: Int,
  endpoints: Iterable[HostAndPort],
  keys: QueryData[K]) extends RiakPartition

object RiakKeysPartitioner {
  def partitions[K](endpoints: Iterable[HostAndPort], readConf: ReadConf, riakKeys: QueryData[K]): Array[Partition] = {
    riakKeys.keysOrRange match {
      case Some(Left(keys)) =>
        Array(new RiakKeysPartition[K](0, endpoints, riakKeys))

      case Some(Right(ranges: Seq[(K, Option[K])])) =>
        ranges match {
          case (from, to) +: Seq() => {
            val splitRanges = splitRangeIntoSubranges(from, to, readConf.getOrDefaultSplitCount())
            partitionPerRange(splitRanges, endpoints, riakKeys.index)
          }
          case _ => partitionPerRange(ranges, endpoints, riakKeys.index)
        }
    }
  }

  def partitionPerRange[K](ranges: Seq[(K, Option[K])], endpoints: Iterable[HostAndPort], index: Option[String]): Array[Partition] = {
    ranges.zipWithIndex.map {
      case (range, indx) => new RiakKeysPartition[K](indx, endpoints, new QueryData[K](Some(Right(Seq(range))), index))
    }.toArray
  }

  // TODO: move to PartitionUtils
  def calculateRanges[T: Integral](from: T, to: T, splitCount: Int)(implicit num: Integral[T]): Seq[(T, T)] = {
    import num._
    val diff = (to - from) / num.fromInt(splitCount - 1)
    val partitionsCount = if (diff == 0) 1 else splitCount
    val start = (0 to (partitionsCount - 1)).map(x => from + num.fromInt(x) * diff) 
    val end = start.tail.map(_ - num.fromInt(1)) :+ to
    start zip end
  }

  def splitRangeIntoSubranges[K](from: K, to: Option[K], splitCount: Int): Seq[(K, Option[K])] = {
    to match {
      case Some(rangeEnd: Int) => from match {
        case rangeStart: Int => {
          calculateRanges(rangeStart, rangeEnd, splitCount).map(r => (r._1.asInstanceOf[K], Some(r._2.asInstanceOf[K])))
        }
        case _ => throw new IllegalArgumentException
      }
      case Some(rangeEnd: Long) => from match {
        case rangeStart: Long => {
          calculateRanges(rangeStart, rangeEnd, splitCount).map(r => (r._1.asInstanceOf[K], Some(r._2.asInstanceOf[K])))
        }
        case _ => throw new IllegalArgumentException
      }
      case Some(rangeEnd: BigInt) => from match {
        case rangeStart: BigInt => {
          calculateRanges(rangeStart, rangeEnd, splitCount).map(r => (r._1.asInstanceOf[K], Some(r._2.asInstanceOf[K])))
        }
        case _ => throw new IllegalArgumentException
      }
      case _ => Seq((from, to))
    }
  }
} 
Example 44
Source File: Numerics.scala    From finagle-postgres   with Apache License 2.0 5 votes vote down vote up
package com.twitter.finagle.postgres.values

import java.math.BigInteger

import io.netty.buffer.{ByteBuf, Unpooled}

private object Numerics {
  private val NUMERIC_POS = 0x0000
  private val NUMERIC_NEG = 0x4000
  private val NUMERIC_NAN = 0xC000
  private val NUMERIC_NULL = 0xF000
  private val NumericDigitBaseExponent = 4
  val biBase = BigInteger.valueOf(10000)

  private def getUnsignedShort(buf: ByteBuf) = {
    val high = buf.readByte().toInt
    val low = buf.readByte()
    (high << 8) | low
  }

  private def base10exponent(in: java.math.BigDecimal) = {
     in.round(new java.math.MathContext(1)).scale() * -1
  }

  def readNumeric(buf: ByteBuf) = {
    val len = getUnsignedShort(buf)
    val weight = buf.readShort()
    val sign = getUnsignedShort(buf)
    val displayScale = getUnsignedShort(buf)

    //digits are actually unsigned base-10000 numbers (not straight up bytes)
    val digits = Array.fill(len)(buf.readShort())
    val bdDigits = digits.map(BigDecimal(_))

    if(bdDigits.length > 0) {
      val unscaled = bdDigits.foldLeft(BigDecimal(0)) {
        case (acc, n) => acc * 10000 + n
      }

      val scaleFactor = (weight  + 1 - len) * NumericDigitBaseExponent
      val unsigned = unscaled.bigDecimal.movePointRight(scaleFactor).setScale(displayScale)

      sign match {
        case NUMERIC_POS => BigDecimal(unsigned)
        case NUMERIC_NEG => BigDecimal(unsigned.negate())
        case NUMERIC_NAN => throw new NumberFormatException("Decimal is NaN")
        case NUMERIC_NULL => throw new NumberFormatException("Decimal is NUMERIC_NULL")
      }
    } else {
      BigDecimal(0)
    }
  }

  def writeNumeric(in: BigDecimal) = {
    val minimized = BigDecimal(in.bigDecimal.stripTrailingZeros())
    val unscaled = minimized.bigDecimal.unscaledValue().abs()
    val sign = minimized.signum

    def findDigits(i: BigInteger, current: List[Short] = Nil): List[Short] = if(i.signum() != 0) {
      val Array(q, r) = i.divideAndRemainder(biBase)
      findDigits(q, r.shortValue() :: current)
    } else current

    //the decimal point must align on a base-10000 digit
    val padZeroes = 4 - (minimized.scale % 4)
    val paddedUnscaled = Option(padZeroes)
      .filterNot(_ == 0)
      .map(a => BigInteger.valueOf(10).pow(a))
      .map(unscaled.multiply)
      .getOrElse(unscaled)

    val digits = findDigits(paddedUnscaled, Nil)

    val weight = {
      val powers10 = base10exponent(in.bigDecimal) - base10exponent(new java.math.BigDecimal(paddedUnscaled))
      val mod4 = if (powers10 % 4 >= 0) powers10 % 4 else 4 + powers10 % 4

      digits.length + (powers10 - mod4) / 4 - 1
    }
    val bufSize =
      2 + //digit length
      2 + //weight
      2 + //sign
      2 + //scale
      digits.length * 2 //a short for each digit

    val buf = Unpooled.wrappedBuffer(new Array[Byte](bufSize))
    val scale = if(in.scale < 0) 0 else in.scale
    buf.resetWriterIndex()
    buf.writeShort(digits.length)
    buf.writeShort(weight)
    buf.writeShort(if(sign < 0) NUMERIC_NEG else NUMERIC_POS)
    buf.writeShort(scale)
    digits foreach (d => buf.writeShort(d))
    buf
  }
} 
Example 45
Source File: ECIESCoderSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.crypto

import java.math.BigInteger

import io.iohk.ethereum.nodebuilder.SecureRandomBuilder
import org.scalatest.{FlatSpec, Matchers}
import org.spongycastle.crypto.generators.ECKeyPairGenerator
import org.spongycastle.crypto.params.{ECKeyGenerationParameters, ECPrivateKeyParameters, ECPublicKeyParameters}
import org.spongycastle.util.encoders.Hex

class ECIESCoderSpec extends FlatSpec with Matchers with SecureRandomBuilder {

  "ECIESCoder" should "decrypt encrypted message" in {

    val generator = new ECKeyPairGenerator
    generator.init(new ECKeyGenerationParameters(curve, secureRandom))

    val pub = curve.getCurve.decodePoint(Hex.decode("0395e5903e0e732f8db854d63314ef1926ccadda6cf77c5052638d5ccb00d74af5"))
    val prv = new BigInteger(1, Hex.decode("00bc9551eeccd2b1189e24e18ca1482ed69080ac69b67c2a17bc1e44e79a1390c3"))

    val plainText = Hex.decode("ea9f99ea8b9ee6ae8de3abb4e5948ee9b789e0aca1e9b3a4e5a2b7e38292e994b9e7a8aeec9180e6aea4e8809eea9181e595a4eba985e598a7e98d99eab8a6eb8496e394bde3b28ee1aea6e2b0bfec8cbbe1b493e5a094e794b5e1a0a9e79a89e7a895e69494e6be89e7b19fe6a081ebabb2e69481e9989be2a09fe7ae84e7aba2ed97b7e5acb6e2b996e7aabbeb95b3e5aa83ec91b3e986bbe39d81ed8c8be0aaaee89e85e597ace58b81e9bb9de2aea5ed8997ea88ace5adb9eb92a6e7b6aee1b9bbe0bf9cd8a1e1abb3e5a8a5cfb9ea9192e3a68bec98bce5978ae4a1b2e8a0aaeab98fed9881e3a089e7bf9cecba97e7afb6e0b69de29699ebb989e4b785e5b589ebb388ed8992e9a3b8eb8fa2e4bfa2e4a781e39abce7a896e1a687e48588e181b9eb8ea4ebb1b8e8b0a9e9bb86e7b38be9ab84e5b5a8e69783ec8cb9ebbcabe68ca5e180a0e990b5e799a4e499aaec958bec81ace4b58ced9d94e281bde292aceb8f86ed9cace188a7e594b4e883bfca93ebb3b5e99591e8a292e1958764e29cabeab5b5e2bab6c5a3e0b681e7bc91e78db7e884a4e1968cea8aa6e1a8a0e4a880e7b1a0e79eaae1a897e9bb8ce8b19beaabaae381bae4bfb5e5aeb9ed8189cbb0ec99b2e68b9be9a2aae384a1ed8dbde8aca0e3b1a0e5859de993a3e38b8ae5b7b1cc84e19190e284bfe18aa5e99db9e38f8aeaae8ee6adb7e9a093ecae8fe6beafeab0bbe7968de1b7a9e38296ecb99be8ac87e2abb0e4ada2ec9698e184a3ea9787e882bce2bda7e68587e5b6b0ea8d9be9a8a0eaa9bfe8ab8aeab1b7e29f8ce386a2e1a58bea8f9ae88ebee5839be1998ae3a585e584bfe89e8ee4b6b4e69caae5ad97e5859feb8dbbebb6ace497ace7ada8ed9da8e7a099eabda3e786a1e7a6a4ec8ca4e7968de38a8ce2a3a5e4b8a2ec90a4ea88aae4bd82e98bbaecb8b5e19397e384b9e5bbbceaa58de58090e783b9ea8aa5e0af8ae882b7e0bdade1a18beabd9ae883a5e3918be2b9a5e8b4b3ed91a8cca8ebb7bfeb81abeca8a6eab6b6e9b0bdecb2b6e48e80e6a8acec86a6e4afb5ec8d91e48f94e19483e8a2bde7829ce799b4eaa68be78dabe395b5c3bae19298e389a6e38dbbd59ee5b994e8b4ade1ae9ce88bbae8b487e3bcb4e2a19ee9a7adebbdafe78082e6999de2babfed83a2e4a6bce180a2d8a5ea9c98eab1a1ebb9abe8bcb1e880b3ec96bde49b80ecb38ee2a18eecbfaee58b95e9a88fe19c9eeabb89dfbde8b893e2a09fd88ee0a687e4acb9e5b0b1e5b2b1e48993ecb397e886bfe888b2e6b583e0bc84e18fbbed8a94e78981e3b194e2b890e590afebbab5e3a19beab796e8a693e98580e69eb7e2839fea8f87e891a3e5848aec809eeb9c90ea9abce7b8bee883a2e3b189e2ada0eaa486e592b8e2b5aee295b1ec9ea9e78dbfe5a384ec9ebce4978ce290b4eb9a8aeab4b6e6b08be6b1b3e0a0ace6a9a3e0a788e5bbb5e1b29ce585a1e8b895e499bcceb9eca491e293bde6a1bfea82a8e0a9bbeb8f92e0bf81c297e295bfe4bebce8beaeed9fb4e7adb9e1be99e7b98ae699a3e7a8a2e58188e6b09cc993de82ec9389ea87a0ec9d97e7aa8fe98c91e0afbce197b3e8a5b8e2a7afeb9abeeb9989e1bc93eb9ab4e888adeb968bec83b6ecadb2e5aaa5d383e58086eb918be28eabc5a1ecae9aeb9da7ec9da0e0a394e69d83e58488e59aaae3af97ec8f8beb82bde7b297e79c86e0b5bee7b1a7e398adeab69de4a2b8e383b8e0adb1c9b1e2ac89e5a0a8e6b8a7e2a19ae6a693e0b69ce4bab7e9b5b3e9a18ceaac8fe7bda8e6b290e196a9e9b695eabfb8ec9fa8e9bd89e78e93e0b586e29a92e39d83e6a7a5e7a1b9e38f8de5ae92e7a5a8e2aeb7c7afe6b58be19cbae4a7b8eba193ecb987e4aabee29781e487abe7a7ace292aae59690eb809ce5be96eb90a6e5bfafeba8b6e99984eb9d90e3b099e0ac86e3a19ce39c9eebab98e991a1ed92b5e5a8b4eb8face0a890e787a4ec8eb4e6958be298a5ed869ce587bbe981a2e0b9b5e89f8fe3b581ed8abfe0a680e38c81d09ae795a8e9aab1e0afa3e496b5e4af8ce7bcaa")
    val providedCryptogram = Hex.decode("0448341f4403b8780fff8fecaabd79b917c94230f9ab19f9d2921129b504568a707e6e1dc1e5c5ffa026a546db9a2591e55c4c15135c1de41e7dc1251604add380720d70fdf88f465f84c881a5500dbde47848683ddf6739b778d13d7f08e991c9aa9a4798d6081c5581781e851912b3113c182d5506b0a172fc8e20bb08fe385cd6cc065478ec4807438156068eb1c1629761079b4c12d9fa875a95014920054ce7c918a16af9b03136c513762f72a9c3ef4665ccf69032e0964c77dee159876e287712bf458216ff1c710672218cd5d027dbfaffcff60b871d616439c76e0371ec64fb7e6761140850721d85a04e82d50138f147c2b029bfa3f6f9323054f36714c6a012de92875ce367bcd781131181181c306f62c5e93209b4d555717126ec8ce5dcc7de1dcf4283cc43ab4e3f00d3791dec2f46f761f7ccc4536d8d7b5341cdbb46f88135b9a4e3367bfaa3cd3be5425585e61f0de1ec3079015817257551443f918076f446dd7cc8208fd2e7a9adfb77e55f9673df4335a8b5bea873cccccc2ca3c6869b41462edb069f1e9328bfa7cb8d77d96df8e42d6bd7a4a20a30ad047b07b03bda677893a74d55af5109642754a0c880d698006180723bbbdc7f088a42a27090df7e479eda4eca07a6bf1d22592194a5b982d49aed18024a9285fad2b650e7a10e24bd1da36539686862e4405ed91a288fe77629a0cb06f1cfe575950aff06b6bee07999eae200730d8ec873c148d3e620a1869ca59741ac16bf68c54e40edfd777492a7e72588aa56dd7ae436616581852c97f5af998b2e0e138d35585cb95dfc7cb0c1a028a5a897c009e8dd53709195c20103869bf4adb8e868c32ef3bbba5202bc78663fbc69307abe89a43ee6e222705bba342b2ecbf8ce7c2d01fc9c44a245224350172181ca38fa12720e84974b1fbf9f9c59d61b9dd13d55547850f6c952cc18b76fae40bfd65398ed4ce8b2eec49a86ea46355dbf937c04ccdd962fbc4a3ba1808533b4b585698ddb8892ebdd3e0c10c19831a5c9dc50b9579cd8bf6304babb08245727c1c2cde78fba6f52ba19a4f28988b95d0a513640a6b3ade92ff045259c4327937eb5d0a9e507321b2ecfdee7d3b1db82e090d2155aca9d57677e31adaea840389f74d205e896c2840f23c5fd588f5a1bc5be8482c564d287659b83d6f823c1303fcfdad9a24bfadcdb49592d41bca6ee14c822ba688152923981692c71137408500bd5ab8702312f5c09fb30d9c5540c0cd2a9284162c35d94d29607e8bb0835103691cb73e03f2f18fc0832486ce825d56fd8d8d9fd0d259809bb46ea4b1010ce45c85a197679447448c6aa4af731d28a959a2c09ea493911a6c54e3ad0d8c02dc27de903bdc0544815c13e8bc559860c83af9ecf33a40b00036e27b21fb4f3a11feadf1abd01be37e54494db9470b74724706d839969e33ed35e0e466c3e727bd50c1b4010ed9e4b439d5581dc1b37aee0fa049ae0fed2cda8ec7433bbcfc537d2ff201cb51cd5f939b7bce65077e928c4e106d78be3bff0b5fbcc549f5e1fe6a76595ea02a60ff18e3450515aadbbb2b48376d6ff0056e81373f20959105241ef3dc10d447a35614147935622012da31c260afd0226e93bf42d871ec59114c46cf25cdb1dc8d2320aaed63af937f128803fa6d8320e7ff80e0d68e92792d93c0563dea8e0ad66ecb6f1bbee8d3c1e42a593ad1d16584337214b212a16f49f2fdf42df5e37ee6c845fd08345671e8d7ecacb481838299b8307fe98ef3d6724d06e8d81bacc451a3e84c799db9fe2001beec81a25043ef26f069987ed14b8d5334212e85909a803043591e90662cb451cb0dc946f77a3ea9fb80ac7ba5800ddcee3cdfa5e814609891b9f2e966e6e356db09b198f7f4b636466ff8500d51ab8e7dc410bd2a081916bb63acc299d6d1b6e4fc8bb2d443f575a8140afc4c9a7c277ad0dfcb61fd0da4652b1fa87d4a92de2c9e218117114d75bb83819d5d426543a3fe11510e24b519ef2e899d5b87bfc9ef845581d4330cc84203348746a5cb34028fff9edfb9355bd395497378c8495570cf8d94f258944224626e0e5c9cd02dbda08461530b17010aac4768a6a5aaf03078383afa1d6574d40ae8050b590ff0f229df85029f751ab663f349bcbd0dfd1a54f3030d3ab4a4248787f865ae9aa8d8329c6c109610e3b03869adb98b3090fe40e8eed79971a71fa81eef27b524b92cba54fa92a76ee")

    val resultForProvidedCryptogram = ECIESCoder.decrypt(prv, providedCryptogram)

    val cryptogram = ECIESCoder.encrypt(pub, secureRandom, plainText)
    val result = ECIESCoder.decrypt(prv, cryptogram)

    plainText shouldBe resultForProvidedCryptogram
    plainText shouldBe result
  }

  "ECIESCoder" should "decryptSimple encryptSimple message" in {

    val generator = new ECKeyPairGenerator
    generator.init(new ECKeyGenerationParameters(curve, secureRandom))

    val keyPair = generator.generateKeyPair()
    val prv = keyPair.getPrivate.asInstanceOf[ECPrivateKeyParameters].getD
    val pub = keyPair.getPublic.asInstanceOf[ECPublicKeyParameters].getQ

    val charSet = "utf-8"
    val plainText = "some test message".getBytes(charSet)
    val cryptogram = ECIESCoder.encryptSimple(pub, secureRandom, plainText)
    val result = ECIESCoder.decryptSimple(prv, cryptogram)

    plainText shouldBe result
  }

  "ECIESCoder" should "pass test for simple encoding from ethereumJ" in {

    val cipherText1 = Hex.decode("0469e324b8ab4a8e2bf0440548498226c9864d1210248ebf76c3396dd1748f0b04d347728b683993e4061998390c2cc8d6d09611da6df9769ebec888295f9be99e86ddad866f994a494361a5658d2b48d1140d73f71a382a4dc7ee2b0b5487091b0c25a3f0e6")
    val priv = new BigInteger(1, Hex.decode("d0b043b4c5d657670778242d82d68a29d25d7d711127d17b8e299f156dad361a"))
    val pub = curve.getCurve.decodePoint(
      Hex.decode("04bd27a63c91fe3233c5777e6d3d7b39204d398c8f92655947eb5a373d46e1688f022a1632d264725cbc7dc43ee1cfebde42fa0a86d08b55d2acfbb5e9b3b48dc5"))
    val plain1 = ECIESCoder.decryptSimple(priv, cipherText1)
    val cipherText2 = ECIESCoder.encryptSimple(pub, secureRandom, plain1)
    val plain2 = ECIESCoder.decryptSimple(priv, cipherText2)

    val expected = Array[Byte](0, 91, 34, 48, 120, 52, 56, 54, 53, 54, 99, 54, 99, 54, 102, 34, 93)

    expected shouldBe plain1
    expected shouldBe plain2
  }

  "ECIESCoder" should "past tests from ethereumJ - test1" in {

    val privKey = new BigInteger("5e173f6ac3c669587538e7727cf19b782a4f2fda07c1eaa662c593e5e85e3051", 16)
    val cipher = Hex.decode("049934a7b2d7f9af8fd9db941d9da281ac9381b5740e1f64f7092f3588d4f87f5ce55191a6653e5e80c1c5dd538169aa123e70dc6ffc5af1827e546c0e958e42dad355bcc1fcb9cdf2cf47ff524d2ad98cbf275e661bf4cf00960e74b5956b799771334f426df007350b46049adb21a6e78ab1408d5e6ccde6fb5e69f0f4c92bb9c725c02f99fa72b9cdc8dd53cff089e0e73317f61cc5abf6152513cb7d833f09d2851603919bf0fbe44d79a09245c6e8338eb502083dc84b846f2fee1cc310d2cc8b1b9334728f97220bb799376233e113")
    val payload = ECIESCoder.decrypt(privKey, cipher)

    val expectedPayload = "802b052f8b066640bba94a4fc39d63815c377fced6fcb84d27f791c9921ddf3e9bf0108e298f490812847109cbd778fae393e80323fd643209841a3b7f110397f37ec61d84cea03dcc5e8385db93248584e8af4b4d1c832d8c7453c0089687a700"
    expectedPayload shouldBe Hex.toHexString(payload)
  }

  "ECIESCoder" should "past tests from ethereumJ - test2" in {

    val privKey = new BigInteger("5e173f6ac3c669587538e7727cf19b782a4f2fda07c1eaa662c593e5e85e3051", 16)
    val payload = Hex.decode("1122334455")
    val pubKeyPoint = curve.getG.multiply(privKey)
    val cipher = ECIESCoder.encrypt(pubKeyPoint, secureRandom, payload)
    val decryptedPayload = ECIESCoder.decrypt(privKey, cipher)

    decryptedPayload shouldBe payload
  }
} 
Example 46
Source File: AuthHandshakerSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.network

import java.math.BigInteger
import java.net.URI

import akka.util.ByteString
import io.iohk.ethereum.crypto._
import io.iohk.ethereum.network.rlpx.{AuthHandshakeSuccess, AuthHandshaker, AuthResponseMessage, Secrets}
import io.iohk.ethereum.nodebuilder.SecureRandomBuilder
import org.scalatest.{FlatSpec, Matchers}
import org.spongycastle.crypto.params.{ECPrivateKeyParameters, ECPublicKeyParameters}
import org.spongycastle.crypto.AsymmetricCipherKeyPair
import org.spongycastle.util.encoders.Hex

class AuthHandshakerSpec extends FlatSpec with Matchers with SecureRandomBuilder {

  val remoteNodeKey = new AsymmetricCipherKeyPair(
    new ECPublicKeyParameters(curve.getCurve.decodePoint(
      Hex.decode("0491376c89ba75cc51fd6b63af01083e6cc11f5635620527e254a03374738e1eb344b2221470a4638e670a97a06f3b91c4f517ccc325561148b106407671d5c46d")), curve),
    new ECPrivateKeyParameters(new BigInteger("105751695959748236927330749967459856049816015502376529986938855740081063876828"), curve))

  val remoteEphemeralKey = new AsymmetricCipherKeyPair(
    new ECPublicKeyParameters(curve.getCurve.decodePoint(
      Hex.decode("0404753378699da7678151e9a0605aa0b07ba4f31764b624c90d497d0c78b56f8fe47cd78e1eef35022d1241c7d2ee42eac74a9036f3ed0b8027ce484b556e789e")), curve),
    new ECPrivateKeyParameters(new BigInteger("92053546780651665949308997856114509339625788837073204328320628931366416758609"), curve))

  val remoteNonce = ByteString(Array.fill[Byte](AuthHandshaker.NonceSize)(9.toByte))

  val remoteNodeId: Array[Byte] = remoteNodeKey.getPublic.asInstanceOf[ECPublicKeyParameters].toNodeId
  val remoteUri = new URI(s"enode://${Hex.toHexString(remoteNodeId)}@127.0.0.1:30303")

  val nodeKey = new AsymmetricCipherKeyPair(
    new ECPublicKeyParameters(curve.getCurve.decodePoint(
      Hex.decode("045a57761ca5e81288f32b4136e5a8f8d816a0b992b6dfea75312d2dd7618ee8f7e113aaa732dd77f901a7af43275280b985b9f539615733cdf7fbe06636813d4b")), curve),
    new ECPrivateKeyParameters(new BigInteger("51209471710014748445103304012335548896378228839026325233666834803269084805514"), curve))

  val ephemeralKey = new AsymmetricCipherKeyPair(
    new ECPublicKeyParameters(curve.getCurve.decodePoint(
      Hex.decode("04ead31caeaf59d1299991c16910f68cd61216a67e397111429d2800f58e849940fc0bf8c8f1df05c7de40cd21a2b0bed9d0c3c184034f9d5fd54c4476ddd8d6ed")), curve),
    new ECPrivateKeyParameters(new BigInteger("47209959662887443680833530073996538660770112643177512357678065781331682025297"), curve))

  val nonce = ByteString(Array.fill[Byte](AuthHandshaker.NonceSize)(1.toByte))

  "AuthHandshaker" should "handle init response" in {
    val (_, authHandshaker) = AuthHandshaker(nodeKey, nonce, ephemeralKey, secureRandom).initiate(remoteUri)

    val response = AuthResponseMessage(
      ephemeralPublicKey = remoteEphemeralKey.getPublic.asInstanceOf[ECPublicKeyParameters].getQ,
      nonce = remoteNonce,
      knownPeer = false)

    val encodedResponse = response.encoded
    val encryptedResponse = ECIESCoder.encrypt(nodeKey.getPublic.asInstanceOf[ECPublicKeyParameters].getQ, secureRandom, encodedResponse.toArray)

    val AuthHandshakeSuccess(secrets: Secrets, _) = authHandshaker.handleResponseMessage(ByteString(encryptedResponse))

    val expectedMacSecret = Hex.decode("50a782c6fedf88b829a6e5798da721dcbf5b46c117704e2ada985d5235ac192c")
    val expectedSharedToken = Hex.decode("b1960fa5d529ee89f8032c8aeb0e4fda2bbf4d7eff0c5695173e27f382d8f5bb")
    val expectedAesSecret = Hex.decode("55e7896a728e74650b3da1e4011823983551d1b5a5bfaf166627da9bea25a562")

    secrets.mac shouldBe expectedMacSecret
    secrets.token shouldBe expectedSharedToken
    secrets.aes shouldBe expectedAesSecret
  }

  it should "handle both incoming packet and a response" in {
    val thisHandshaker = AuthHandshaker(nodeKey, nonce, ephemeralKey, secureRandom)
    val remoteHandshaker = AuthHandshaker(remoteNodeKey, remoteNonce, remoteEphemeralKey, secureRandom)

    val (initPacket, thisHandshakerInitiated) = thisHandshaker.initiate(remoteUri)
    val (responsePacket, AuthHandshakeSuccess(remoteSecrets: Secrets, _)) = remoteHandshaker.handleInitialMessageV4(initPacket)
    val AuthHandshakeSuccess(thisSecrets: Secrets, _) = thisHandshakerInitiated.handleResponseMessageV4(responsePacket)

    remoteSecrets.token shouldBe thisSecrets.token
    remoteSecrets.aes shouldBe thisSecrets.aes
    remoteSecrets.mac shouldBe thisSecrets.mac
  }

} 
Example 47
Source File: ECIESCoder.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.crypto

import java.io.{ByteArrayInputStream, IOException}
import java.math.BigInteger
import java.security.SecureRandom

import org.spongycastle.crypto.digests.{SHA1Digest, SHA256Digest}
import org.spongycastle.crypto.engines.AESEngine
import org.spongycastle.crypto.generators.ECKeyPairGenerator
import org.spongycastle.crypto.macs.HMac
import org.spongycastle.crypto.modes.SICBlockCipher
import org.spongycastle.crypto.params._
import org.spongycastle.crypto.parsers.ECIESPublicKeyParser
import org.spongycastle.crypto.{BufferedBlockCipher, InvalidCipherTextException}
import org.spongycastle.math.ec.ECPoint

object ECIESCoder {

  val KeySize = 128
  val PublicKeyOverheadSize = 65
  val MacOverheadSize = 32
  val OverheadSize = PublicKeyOverheadSize + KeySize / 8 + MacOverheadSize

  @throws[IOException]
  @throws[InvalidCipherTextException]
  def decrypt(privKey: BigInteger, cipher: Array[Byte], macData: Option[Array[Byte]] = None): Array[Byte] = {
    val is = new ByteArrayInputStream(cipher)
    val ephemBytes = new Array[Byte](2 * ((curve.getCurve.getFieldSize + 7) / 8) + 1)
    is.read(ephemBytes)
    val ephem = curve.getCurve.decodePoint(ephemBytes)
    val IV = new Array[Byte](KeySize / 8)
    is.read(IV)
    val cipherBody = new Array[Byte](is.available)
    is.read(cipherBody)
    decrypt(ephem, privKey, Some(IV), cipherBody, macData)
  }

  @throws[InvalidCipherTextException]
  def decrypt(ephem: ECPoint, prv: BigInteger, IV: Option[Array[Byte]], cipher: Array[Byte], macData: Option[Array[Byte]]): Array[Byte] = {
    val aesEngine = new AESEngine

    val iesEngine = new EthereumIESEngine(
      kdf = Left(new ConcatKDFBytesGenerator(new SHA256Digest)),
      mac = new HMac(new SHA256Digest),
      hash = new SHA256Digest,
      cipher = Some(new BufferedBlockCipher(new SICBlockCipher(aesEngine))),
      IV = IV,
      prvSrc = Left(new ECPrivateKeyParameters(prv, curve)),
      pubSrc = Left(new ECPublicKeyParameters(ephem, curve)))


    iesEngine.processBlock(cipher, 0, cipher.length, forEncryption = false, macData)
  }

  
  @throws[IOException]
  @throws[InvalidCipherTextException]
  def encryptSimple(pub: ECPoint, secureRandom: SecureRandom, plaintext: Array[Byte]): Array[Byte] = {

    val eGen = new ECKeyPairGenerator
    val gParam = new ECKeyGenerationParameters(curve, secureRandom)
    eGen.init(gParam)

    val iesEngine = new EthereumIESEngine(
      kdf = Right(new MGF1BytesGeneratorExt(new SHA1Digest)),
      mac = new HMac(new SHA1Digest),
      hash = new SHA1Digest,
      cipher = None,
      IV = Some(new Array[Byte](0)),
      prvSrc = Right(eGen),
      pubSrc = Left(new ECPublicKeyParameters(pub, curve)),
      hashMacKey = false)

    iesEngine.processBlock(plaintext, 0, plaintext.length, forEncryption = true)
  }

  private def makeIESEngine(pub: ECPoint, prv: BigInteger, IV: Option[Array[Byte]]) = {
    val aesEngine = new AESEngine

    val iesEngine = new EthereumIESEngine(
      kdf = Left(new ConcatKDFBytesGenerator(new SHA256Digest)),
      mac = new HMac(new SHA256Digest),
      hash = new SHA256Digest,
      cipher = Some(new BufferedBlockCipher(new SICBlockCipher(aesEngine))),
      IV = IV,
      prvSrc = Left(new ECPrivateKeyParameters(prv, curve)),
      pubSrc = Left(new ECPublicKeyParameters(pub, curve)))

    iesEngine
  }

} 
Example 48
Source File: ByteUtils.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.utils

import java.math.BigInteger
import java.nio.{ByteBuffer, ByteOrder}

import akka.util.ByteString

import scala.util.Random

object ByteUtils {

  def bigIntegerToBytes(b: BigInteger, numBytes: Int): Array[Byte] = {
    val bytes = new Array[Byte](numBytes)
    val biBytes = b.toByteArray
    val start = if (biBytes.length == numBytes + 1) 1 else 0
    val length = Math.min(biBytes.length, numBytes)
    System.arraycopy(biBytes, start, bytes, numBytes - length, length)
    bytes
  }

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

  def or(arrays: Array[Byte]*): Array[Byte] = {
    require(arrays.map(_.length).distinct.length <= 1, "All the arrays should have the same length")
    require(arrays.nonEmpty, "There should be one or more arrays")

    val zeroes = Array.fill(arrays.head.length)(0.toByte)
    arrays.foldLeft[Array[Byte]](zeroes){
      case (prevOr, array) => prevOr.zip(array).map{ case (b1, b2) => (b1 | b2).toByte }
    }
  }

  def and(arrays: Array[Byte]*): Array[Byte] = {
    require(arrays.map(_.length).distinct.length <= 1, "All the arrays should have the same length")
    require(arrays.nonEmpty, "There should be one or more arrays")

    val ones = Array.fill(arrays.head.length)(0xFF.toByte)
    arrays.foldLeft[Array[Byte]](ones){
      case (prevOr, array) => prevOr.zip(array).map{ case (b1, b2) => (b1 & b2).toByte }
    }
  }

  def randomBytes(len: Int): Array[Byte] = {
    val arr = new Array[Byte](len)
    new Random().nextBytes(arr)
    arr
  }

  def bigEndianToShort(bs: Array[Byte]): Short = {
    val n = bs(0) << 8
    (n | bs(1) & 0xFF).toShort
  }

  def padLeft(bytes: ByteString, length: Int, byte: Byte = 0): ByteString = {
    val l = math.max(0, length - bytes.length)
    val fill = Seq.fill[Byte](l)(byte)
    fill ++: bytes
  }

  def compactPickledBytes(buffer: ByteBuffer): ByteString = {
    val data = Array.ofDim[Byte](buffer.limit)
    buffer.rewind()
    buffer.get(data)
    ByteString(data)
  }


  def bytesToInts(bytes: Array[Byte]): Array[Int] =
    bytes.grouped(4).map(getIntFromWord).toArray

  def intsToBytes(input: Array[Int]): Array[Byte] = {
    input.flatMap { i =>
      Array(
        (i & 0xFF).toByte,
        ((i >> 8) & 0xFF).toByte,
        ((i >> 16) & 0xFF).toByte,
        ((i >> 24) & 0xFF).toByte)
    }
  }

  def getIntFromWord(arr: Array[Byte]): Int = {
    ByteBuffer.wrap(arr, 0, 4).order(ByteOrder.LITTLE_ENDIAN).getInt
  }

} 
Example 49
Source File: SignaturePlatform.scala    From iotchain   with MIT License 5 votes vote down vote up
package jbok.crypto.signature

import java.math.BigInteger
import java.util.Random

import cats.effect.Sync
import jbok.crypto.facade.{BN, EC, SignatureEC}

import scala.scalajs.js.JSConverters._
import scala.scalajs.js.typedarray.Uint8Array

trait SignaturePlatform {
  val ecdsa: Signature[ECDSA] = ECDSAPlatform
}

private object ECDSAPlatform extends Signature[ECDSA] {
  import ECDSACommon._
  val secp256k1 = new EC("secp256k1")

  override def generateKeyPair[F[_]](random: Option[Random])(implicit F: Sync[F]): F[KeyPair] = F.delay {
    val keyPair = secp256k1.genKeyPair()
    val secret  = KeyPair.Secret(keyPair.getPrivate("hex"))
    // drop uncompressed indicator, make it 64-bytes
    val pubkey = KeyPair.Public(keyPair.getPublic(false, "hex").drop(2))
    KeyPair(pubkey, secret)
  }

  override def generatePublicKey[F[_]](secret: KeyPair.Secret)(implicit F: Sync[F]): F[KeyPair.Public] = F.delay {
    val keyPair = secp256k1.keyFromPrivate(secret.bytes.toHex, "hex")
    // drop uncompressed indicator, make it 64-bytes
    KeyPair.Public(keyPair.getPublic(false, "hex").drop(2))
  }

  override def sign[F[_]](hash: Array[Byte], keyPair: KeyPair, chainId: BigInt)(implicit F: Sync[F]): F[CryptoSignature] = F.delay {
    val kp  = secp256k1.keyFromPrivate(keyPair.secret.bytes.toHex, "hex")
    val sig = secp256k1.sign(new Uint8Array(hash.toJSArray), kp)
    val r   = new BigInteger(sig.r.toString)
    val s   = new BigInteger(sig.s.toString)
    val pointSign = calculatePointSign(r, toCanonicalS(s), keyPair, hash, chainId) match {
      case Some(recId) => recId
      case None        => throw new Exception("unexpected error")
    }
    val rid: BigInt = getRecoveryId(chainId, pointSign).getOrElse(pointSign)
    CryptoSignature(r, toCanonicalS(s), rid)
  }

  override def verify[F[_]](hash: Array[Byte], sig: CryptoSignature, public: KeyPair.Public, chainId: BigInt)(implicit F: Sync[F]): F[Boolean] = F.delay {
    getPointSign(chainId, sig.v).exists { bigInt =>
      val signatureEC = convert(sig.copy(v = bigInt))
      val key         = secp256k1.keyFromPublic(UNCOMPRESSED_INDICATOR_STRING + public.bytes.toHex, "hex")
      secp256k1.verify(new Uint8Array(hash.toJSArray), signatureEC, key)
    }
  }

  override def recoverPublic(hash: Array[Byte], sig: CryptoSignature, chainId: BigInt): Option[KeyPair.Public] =
    getPointSign(chainId, sig.v).map { bigInt =>
      val signatureEC = convert(sig.copy(v = bigInt))
      val msg         = new Uint8Array(hash.toJSArray)
      val recId       = secp256k1.getKeyRecoveryParam(msg, signatureEC)
      val point       = secp256k1.recoverPubKey(new Uint8Array(hash.toJSArray), signatureEC, recId)
      KeyPair.Public(point.encode("hex", false).drop(2))
    }

  private def convert(sig: CryptoSignature) = {
    val r = new BN(sig.r.toString(16), 16)
    val s = new BN(sig.s.toString(16), 16)
    SignatureEC(r, s, recoveryParam = (sig.v - NEGATIVE_POINT_SIGN).toInt)
  }

  private def calculatePointSign(r: BigInt, s: BigInt, keyPair: KeyPair, hash: Array[Byte], chainId: BigInt): Option[BigInt] =
    allowedPointSigns.find(
      v =>
        recoverPublic(hash, CryptoSignature(r, s, getRecoveryId(chainId, v).getOrElse(v)), chainId)
          .contains(keyPair.public))
} 
Example 50
Source File: KeyPair.scala    From iotchain   with MIT License 5 votes vote down vote up
package jbok.crypto.signature

import java.math.BigInteger

import cats.effect.IO
import jbok.codec.json.implicits._
import io.circe.generic.extras.ConfiguredJsonCodec
import scodec.bits.ByteVector

@ConfiguredJsonCodec
final case class KeyPair(public: KeyPair.Public, secret: KeyPair.Secret)

object KeyPair {
  @ConfiguredJsonCodec
  final case class Public(bytes: ByteVector) extends AnyVal

  object Public {
    def apply(hex: String): Public        = Public(ByteVector.fromValidHex(hex))
    def apply(bytes: Array[Byte]): Public = Public(ByteVector(bytes))
  }

  @ConfiguredJsonCodec
  final case class Secret(bytes: ByteVector) extends AnyVal {
    def d: BigInteger = new BigInteger(1, bytes.toArray)
  }

  object Secret {
    def apply(d: BigInteger): Secret      = apply(ByteVector(d.toByteArray))
    def apply(hex: String): Secret        = apply(ByteVector.fromValidHex(hex))
    def apply(bytes: Array[Byte]): Secret = apply(ByteVector(bytes))
    def apply(bv: ByteVector): Secret     = new Secret(bv.takeRight(32).padLeft(32))
  }

  def fromSecret(secret: ByteVector): KeyPair = {
    val sec = KeyPair.Secret(secret)
    val pub = Signature[ECDSA].generatePublicKey[IO](sec).unsafeRunSync()
    KeyPair(pub, sec)
  }
} 
Example 51
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 52
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 53
Source File: FileUtils.scala    From spark-distcp   with Apache License 2.0 5 votes vote down vote up
package com.coxautodata.utils

import java.math.{BigDecimal, BigInteger}

// Adapted from: https://jira.apache.org/jira/secure/attachment/12542305/roundedByteCountToDisplaySize.patch
object FileUtils {

  val ONE_KB = 1024
  val ONE_KB_BI: BigInteger = BigInteger.valueOf(ONE_KB)
  val ONE_MB: Long = ONE_KB * ONE_KB
  val ONE_MB_BI: BigInteger = ONE_KB_BI.multiply(ONE_KB_BI)
  val ONE_GB: Long = ONE_KB * ONE_MB
  val ONE_GB_BI: BigInteger = ONE_KB_BI.multiply(ONE_MB_BI)
  val ONE_TB: Long = ONE_KB * ONE_GB
  val ONE_TB_BI: BigInteger = ONE_KB_BI.multiply(ONE_GB_BI)
  val ONE_PB: Long = ONE_KB * ONE_TB
  val ONE_PB_BI: BigInteger = ONE_KB_BI.multiply(ONE_TB_BI)
  val ONE_EB: Long = ONE_KB * ONE_PB
  val ONE_EB_BI: BigInteger = ONE_KB_BI.multiply(ONE_PB_BI)
  val ONE_ZB: BigInteger = BigInteger.valueOf(ONE_KB).multiply(BigInteger.valueOf(ONE_EB))
  val ONE_YB: BigInteger = ONE_KB_BI.multiply(ONE_ZB)

  def byteCountToDisplaySize(size: BigInteger): String = {
    val sizeBD = new BigDecimal(size)
    if (size.divide(ONE_YB).compareTo(BigInteger.ZERO) > 0) getThreeSigFigs(sizeBD.divide(new BigDecimal(ONE_YB))) + s" YB (${String.valueOf(size)} bytes)"
    else if (size.divide(ONE_ZB).compareTo(BigInteger.ZERO) > 0) getThreeSigFigs(sizeBD.divide(new BigDecimal(ONE_ZB))) + s" ZB (${String.valueOf(size)} bytes)"
    else if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) getThreeSigFigs(sizeBD.divide(new BigDecimal(ONE_EB_BI))) + s" EB (${String.valueOf(size)} bytes)"
    else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) getThreeSigFigs(sizeBD.divide(new BigDecimal(ONE_PB_BI))) + s" PB (${String.valueOf(size)} bytes)"
    else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0) getThreeSigFigs(sizeBD.divide(new BigDecimal(ONE_TB_BI))) + s" TB (${String.valueOf(size)} bytes)"
    else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) getThreeSigFigs(sizeBD.divide(new BigDecimal(ONE_GB_BI))) + s" GB (${String.valueOf(size)} bytes)"
    else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) getThreeSigFigs(sizeBD.divide(new BigDecimal(ONE_MB_BI))) + s" MB (${String.valueOf(size)} bytes)"
    else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) getThreeSigFigs(sizeBD.divide(new BigDecimal(ONE_KB_BI))) + s" KB (${String.valueOf(size)} bytes)"
    else String.valueOf(size) + " bytes"
  }

  def byteCountToDisplaySize(size: Long): String = byteCountToDisplaySize(BigInteger.valueOf(size))

  private def getThreeSigFigs(size: BigDecimal): String = {
    val (isDecimal, _, sizeS) = size.toString.foldLeft((false, 0, "")) {
      case ((decimal, count, agg), c) =>
        if (c == '.' && !decimal) (true, count, agg + c)
        else if (count < 3 || !decimal) (decimal, count + 1, agg + c)
        else (decimal, count + 1, agg)
    }

    if (isDecimal) sizeS.reverse.dropWhile(c => c == '0').reverse.stripSuffix(".")
    else sizeS

  }

} 
Example 54
Source File: TestFileUtils.scala    From spark-distcp   with Apache License 2.0 5 votes vote down vote up
package com.coxautodata.utils

import org.scalatest.{FunSpec, Matchers}

class TestFileUtils extends FunSpec with Matchers {

  it("byteCountToDisplaySize") {

    import java.math.BigInteger
    val b1023 = BigInteger.valueOf(1023)
    val b1025 = BigInteger.valueOf(1025)
    val KB1 = BigInteger.valueOf(1024)
    val MB1 = KB1.multiply(KB1)
    val GB1 = MB1.multiply(KB1)
    val GB2 = GB1.add(GB1)
    val TB1 = GB1.multiply(KB1)
    val PB1 = TB1.multiply(KB1)
    val EB1 = PB1.multiply(KB1)
    FileUtils.byteCountToDisplaySize(BigInteger.ZERO) should be("0 bytes")
    FileUtils.byteCountToDisplaySize(BigInteger.ONE) should be("1 bytes")
    FileUtils.byteCountToDisplaySize(b1023) should be("1023 bytes")
    FileUtils.byteCountToDisplaySize(KB1) should be("1 KB (1024 bytes)")
    FileUtils.byteCountToDisplaySize(b1025) should be("1 KB (1025 bytes)")
    FileUtils.byteCountToDisplaySize(MB1.subtract(BigInteger.ONE)) should be("1023 KB (1048575 bytes)")
    FileUtils.byteCountToDisplaySize(MB1) should be("1 MB (1048576 bytes)")
    FileUtils.byteCountToDisplaySize(MB1.add(BigInteger.ONE)) should be("1 MB (1048577 bytes)")
    FileUtils.byteCountToDisplaySize(GB1.subtract(BigInteger.ONE)) should be("1023 MB (1073741823 bytes)")
    FileUtils.byteCountToDisplaySize(GB1) should be("1 GB (1073741824 bytes)")
    FileUtils.byteCountToDisplaySize(GB1.add(BigInteger.ONE)) should be("1 GB (1073741825 bytes)")
    FileUtils.byteCountToDisplaySize(GB2) should be("2 GB (2147483648 bytes)")
    FileUtils.byteCountToDisplaySize(GB2.subtract(BigInteger.ONE)) should be("1.99 GB (2147483647 bytes)")
    FileUtils.byteCountToDisplaySize(TB1) should be("1 TB (1099511627776 bytes)")
    FileUtils.byteCountToDisplaySize(PB1) should be("1 PB (1125899906842624 bytes)")
    FileUtils.byteCountToDisplaySize(EB1) should be("1 EB (1152921504606846976 bytes)")
    FileUtils.byteCountToDisplaySize(java.lang.Long.MAX_VALUE) should be("7.99 EB (9223372036854775807 bytes)")
    // Other MAX_VALUEs
    FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Character.MAX_VALUE)) should be("63.9 KB (65535 bytes)")
    FileUtils.byteCountToDisplaySize(BigInteger.valueOf(java.lang.Short.MAX_VALUE)) should be("31.9 KB (32767 bytes)")
    FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Integer.MAX_VALUE)) should be("1.99 GB (2147483647 bytes)")
    // Other Values
    FileUtils.byteCountToDisplaySize(105013122725L) should be("97.8 GB (105013122725 bytes)")
    FileUtils.byteCountToDisplaySize(644353293312L) should be("600 GB (644353293312 bytes)")

  }

} 
Example 55
Source File: Unpacker.scala    From airframe   with Apache License 2.0 5 votes vote down vote up
package wvlet.airframe.msgpack.spi

import java.math.BigInteger
import java.time.Instant


  def tryUnpackNil: Boolean
  def unpackBoolean: Boolean
  def unpackByte: Byte
  def unpackShort: Short
  def unpackInt: Int
  def unpackLong: Long
  def unpackBigInteger: BigInteger
  def unpackFloat: Float
  def unpackDouble: Double
  def unpackString: String
  def unpackTimestamp: Instant
  def unpackTimestamp(extTypeHeader: ExtTypeHeader): Instant

  def unpackArrayHeader: Int
  def unpackMapHeader: Int
  def unpackExtTypeHeader: ExtTypeHeader
  def unpackExtValue(extTypeHeader: ExtTypeHeader): Value
  def unpackRawStringHeader: Int
  def unpackBinaryHeader: Int

  def unpackValue: Value

  //def skipPayload(numBytes: Int): Unit
  def readPayload(dst: Array[Byte]): Unit
  def readPayload(dst: Array[Byte], offset: Int, length: Int): Unit
  def readPayload(length: Int): Array[Byte]
}

case class ExtTypeHeader(extType: Byte, byteLength: Int) 
Example 56
Source File: Packer.scala    From airframe   with Apache License 2.0 5 votes vote down vote up
package wvlet.airframe.msgpack.spi

import java.math.BigInteger
import java.time.Instant


  def packTimestamp(epochSecond: Long, nanoAdjustment: Int = 0): this.type
  def packArrayHeader(arraySize: Int): this.type
  def packMapHeader(mapSize: Int): this.type

  def packExtensionTypeHeader(extType: Byte, payloadLen: Int): this.type
  def packExtensionTypeHeader(extensionTypeHeader: ExtTypeHeader): this.type =
    packExtensionTypeHeader(extensionTypeHeader.extType, extensionTypeHeader.byteLength)
  def packBinaryHeader(len: Int): this.type
  def packRawStringHeader(len: Int): this.type

  def packValue(v: Value): this.type

  def writePayload(src: Array[Byte]): this.type
  def writePayload(src: Array[Byte], offset: Int, length: Int): this.type
  def addPayload(src: Array[Byte]): this.type
  def addPayload(src: Array[Byte], offset: Int, length: Int): this.type
}

trait BufferPacker extends Packer {
  def toByteArray: Array[Byte]
  def clear: Unit
} 
Example 57
Source File: MessageException.scala    From airframe   with Apache License 2.0 5 votes vote down vote up
package wvlet.airframe.msgpack.spi

import java.math.BigInteger


case class IntegerOverflowException(bigInteger: BigInteger)
    extends MessageException(ErrorCode.INTEGER_OVERFLOW, s"Too large integer: ${bigInteger}") {
  def getBigInteger = bigInteger
}
case class TooLargeMessageException(size: Long)
    extends MessageException(ErrorCode.TOO_LARGE_MESSAGE, s"Too large message size: ${size}")

object MessageException {
  def overflowU8(u8: Byte)    = new IntegerOverflowException(BigInteger.valueOf((u8 & 0xff).toLong))
  def overflowU16(u16: Short) = new IntegerOverflowException(BigInteger.valueOf((u16 & 0xffff).toLong))
  def overflowU32(u32: Int)   = new IntegerOverflowException(BigInteger.valueOf((u32 & 0xffffffff).toLong))
  def overflowU64(u64: Long)  = new IntegerOverflowException(BigInteger.valueOf(u64 + Long.MaxValue + 1L).setBit(63))

  def overflowI16(i16: Short) = new IntegerOverflowException(BigInteger.valueOf(i16.toLong))
  def overflowI32(i32: Int)   = new IntegerOverflowException(BigInteger.valueOf(i32.toLong))
  def overflowI64(i64: Long)  = new IntegerOverflowException(BigInteger.valueOf(i64))
  def overflow(b: BigInteger) = new IntegerOverflowException(b)

  def overflowU32Size(u32: Int) = new TooLargeMessageException(((u32 & 0x7fffffff) + 0x80000000L).toLong)
} 
Example 58
Source File: PureScalaBufferUnpacker.scala    From airframe   with Apache License 2.0 5 votes vote down vote up
package wvlet.airframe.msgpack.impl

import java.math.BigInteger
import java.time.Instant

import wvlet.airframe.msgpack.spi._


class PureScalaBufferUnpacker(buf: ReadBuffer) extends Unpacker {
  private var cursor = ReadCursor(buf, 0)

  override def hasNext: Boolean = {
    cursor.lastReadLength < buf.size
  }

  override def getNextFormat: MessageFormat = {
    OffsetUnpacker.peekNextFormat(cursor)
  }

  override def getNextValueType: ValueType = {
    getNextFormat.valueType
  }
  override def skipValue: Unit = {
    OffsetUnpacker.skipValue(cursor)
  }
  override def skipValue(count: Int): Unit = {
    OffsetUnpacker.skipPayload(cursor, count)
  }
  override def unpackNil: Unit = {
    OffsetUnpacker.unpackNil(cursor)
  }
  override def tryUnpackNil: Boolean = {
    OffsetUnpacker.tryUnpackNil(cursor)
  }
  override def unpackBoolean: Boolean = {
    OffsetUnpacker.unpackBoolean(cursor)
  }
  override def unpackByte: Byte = {
    OffsetUnpacker.unpackByte(cursor)
  }
  override def unpackShort: Short = {
    OffsetUnpacker.unpackShort(cursor)
  }
  override def unpackInt: Int = {
    OffsetUnpacker.unpackInt(cursor)
  }
  override def unpackLong: Long = {
    OffsetUnpacker.unpackLong(cursor)
  }
  override def unpackBigInteger: BigInteger = {
    OffsetUnpacker.unpackBigInteger(cursor)
  }
  override def unpackFloat: Float = {
    OffsetUnpacker.unpackFloat(cursor)
  }
  override def unpackDouble: Double = {
    OffsetUnpacker.unpackDouble(cursor)
  }
  override def unpackString: String = {
    OffsetUnpacker.unpackString(cursor)
  }
  override def unpackTimestamp: Instant = {
    OffsetUnpacker.unpackTimestamp(cursor)
  }
  override def unpackTimestamp(extTypeHeader: ExtTypeHeader): Instant = {
    OffsetUnpacker.unpackTimestamp(extTypeHeader, cursor)
  }
  override def unpackArrayHeader: Int = {
    OffsetUnpacker.unpackArrayHeader(cursor)
  }
  override def unpackMapHeader: Int = {
    OffsetUnpacker.unpackMapHeader(cursor)
  }
  override def unpackExtTypeHeader: ExtTypeHeader = {
    OffsetUnpacker.unpackExtTypeHeader(cursor)
  }
  override def unpackExtValue(extTypeHeader: ExtTypeHeader): Value = {
    OffsetUnpacker.unpackExt(extTypeHeader, cursor)
  }
  override def unpackRawStringHeader: Int = {
    OffsetUnpacker.unpackRawStringHeader(cursor)
  }
  override def unpackBinaryHeader: Int = {
    OffsetUnpacker.unpackBinaryHeader(cursor)
  }
  override def unpackValue: Value = {
    OffsetUnpacker.unpackValue(cursor)
  }
  override def readPayload(dst: Array[Byte]): Unit = {
    OffsetUnpacker.readPayload(cursor, dst.length, dst, 0)
  }
  override def readPayload(dst: Array[Byte], offset: Int, length: Int): Unit = {
    OffsetUnpacker.readPayload(cursor, length, dst, offset)
  }
  override def readPayload(length: Int): Array[Byte] = {
    OffsetUnpacker.readPayload(cursor, length)
  }
  override def close(): Unit = {
    // buf.close
  }
} 
Example 59
Source File: GcsPathTest.scala    From fs2-blobstore   with Apache License 2.0 5 votes vote down vote up
package blobstore.gcs

import java.math.BigInteger

import blobstore.AbstractPathTest
import com.google.api.client.util.DateTime
import com.google.api.services.storage.model.StorageObject
import com.google.cloud.storage.BlobInfo

class GcsPathTest extends AbstractPathTest[GcsPath] {
  @SuppressWarnings(Array("scalafix:DisableSyntax.asInstanceOf"))
  val blobInfo: BlobInfo = {
    val storageObject = new StorageObject()
    storageObject.setBucket(root)
    storageObject.setName(dir ++ "/" ++ fileName)
    storageObject.setSize(new BigInteger(fileSize.toString))
    storageObject.setUpdated(new DateTime(lastModified.toEpochMilli))
    val fromPb = classOf[BlobInfo].getDeclaredMethod("fromPb", classOf[StorageObject])
    fromPb.setAccessible(true)
    fromPb.invoke(classOf[BlobInfo], storageObject).asInstanceOf[BlobInfo]
  }

  val concretePath = new GcsPath(blobInfo)
} 
Example 60
Source File: ByteArraysSpec.scala    From scala-stellar-sdk   with Apache License 2.0 5 votes vote down vote up
package stellar.sdk.util

import java.math.BigInteger

import org.scalacheck.Gen
import org.specs2.mutable.Specification
import stellar.sdk.ArbitraryInput
import stellar.sdk.util.ByteArrays._

import scala.util.Try

class ByteArraysSpec extends Specification with ArbitraryInput {

  "padding a byte array" should {
    "do nothing when required length is the array length" >> prop { bs: Array[Byte] =>
      paddedByteArray(bs, bs.length).toSeq mustEqual bs.toSeq
    }

    "do nothing when required length is less than the array length" >> prop { bs: Array[Byte] =>
      paddedByteArray(bs, bs.length - 1).toSeq mustEqual bs.toSeq
    }.setGen(Gen.nonEmptyListOf(Gen.posNum[Byte]).map(_.toArray))

    "pad with zeros when required length is greater than the array length" >> prop { (bs: Array[Byte], plus: Byte) =>
      paddedByteArray(bs, bs.length + plus.toInt).toSeq mustEqual bs.toSeq ++ (1 to plus).map(_ => 0)
    }.setGen2(Gen.posNum[Byte])
  }

  "trimming a byte array" should {
    "remove trailing zeros" >> {
      trimmedByteArray(LazyList()) must beEmpty
      trimmedByteArray("hello".getBytes("UTF-8")) mustEqual "hello".getBytes("UTF-8").toSeq
      trimmedByteArray("hello\u0000\u0000".getBytes("UTF-8")) mustEqual "hello".getBytes("UTF-8").toSeq
      trimmedByteArray("hello\u0000there".getBytes("UTF-8")) mustEqual "hello\u0000there".getBytes("UTF-8").toSeq
    }
  }

  "sha256" should {
    "hash correctly" >> {
      val hash = sha256("今日は世界".getBytes("UTF-8"))
      new BigInteger(1, hash).toString(16).toUpperCase mustEqual
        "72C2CC3C678D77939435E5AE0A0EF2B83D6A42AFB221EA15CD736CB122B23989"
    }
    "hash anything" >> prop { bs: Array[Byte] =>
      Try(sha256(bs)) must beSuccessfulTry[Array[Byte]]
    }
  }

  "base64" should {
    "perform round trip to string" >> prop { bs: Array[Byte] =>
      base64(base64(bs)).toSeq mustEqual bs.toSeq
    }
  }

} 
Example 61
Source File: LnRoute.scala    From bitcoin-s   with MIT License 5 votes vote down vote up
package org.bitcoins.core.protocol.ln.routing

import java.math.BigInteger

import org.bitcoins.core.number.UInt32
import org.bitcoins.core.protocol.ln.ShortChannelId
import org.bitcoins.core.protocol.ln.currency.MilliSatoshis
import org.bitcoins.core.protocol.ln.fee.{
  FeeBaseMSat,
  FeeProportionalMillionths
}
import org.bitcoins.core.util.BytesUtil
import org.bitcoins.crypto.{ECPublicKey, NetworkElement}
import scodec.bits.ByteVector


case class LnRoute(
    pubkey: ECPublicKey,
    shortChannelID: ShortChannelId,
    feeBaseMsat: FeeBaseMSat,
    feePropMilli: FeeProportionalMillionths,
    cltvExpiryDelta: Short)
    extends NetworkElement {

  require(pubkey.isCompressed,
          s"Can only use a compressed public key in routing")

  override def bytes: ByteVector = {

    val cltvExpiryDeltaHex = BytesUtil.encodeHex(cltvExpiryDelta)

    pubkey.bytes ++
      shortChannelID.bytes ++
      feeBaseMsat.bytes ++
      feePropMilli.bytes ++
      ByteVector.fromValidHex(cltvExpiryDeltaHex)
  }
}

object LnRoute {

  def fromBytes(bytes: ByteVector): LnRoute = {
    val PUBKEY_LEN = 33
    val SHORT_CHANNEL_ID_LEN = 8
    val FEE_BASE_U32_LEN = 4
    val FEE_PROPORTIONAL_LEN = 4
    val CLTV_EXPIRTY_DELTA_LEN = 2

    val TOTAL_LEN = PUBKEY_LEN +
      SHORT_CHANNEL_ID_LEN +
      FEE_BASE_U32_LEN +
      FEE_PROPORTIONAL_LEN +
      CLTV_EXPIRTY_DELTA_LEN

    require(
      bytes.length >= TOTAL_LEN,
      s"ByteVector must at least of length $TOTAL_LEN, got ${bytes.length}")

    val (pubKeyBytes, rest0) = bytes.splitAt(PUBKEY_LEN)
    val pubKey = ECPublicKey.fromBytes(pubKeyBytes)

    val (shortChannelIdBytes, rest1) = rest0.splitAt(SHORT_CHANNEL_ID_LEN)

    val shortChannelId = ShortChannelId.fromBytes(shortChannelIdBytes)

    val (feeBaseU32Bytes, rest2) = rest1.splitAt(FEE_BASE_U32_LEN)

    val feeBaseU32 = UInt32.fromBytes(feeBaseU32Bytes)
    val feeBase = feeBaseU32.toLong
    val feeBaseMSat = FeeBaseMSat(MilliSatoshis(feeBase))

    val (u32Bytes, rest3) = rest2.splitAt(FEE_PROPORTIONAL_LEN)

    val u32 = UInt32.fromBytes(u32Bytes)
    val feeProportionalMillionths = FeeProportionalMillionths(u32)

    val (cltvExpiryDeltaBytes, _) = rest3.splitAt(CLTV_EXPIRTY_DELTA_LEN)

    val cltvExpiryDelta = new BigInteger(
      cltvExpiryDeltaBytes.toArray).shortValueExact

    LnRoute(pubKey,
            shortChannelId,
            feeBaseMSat,
            feeProportionalMillionths,
            cltvExpiryDelta)
  }
} 
Example 62
Source File: LnInvoiceSignature.scala    From bitcoin-s   with MIT License 5 votes vote down vote up
package org.bitcoins.core.protocol.ln

import java.math.BigInteger

import org.bitcoins.core.number.{UInt5, UInt8}
import org.bitcoins.core.util.Bech32
import org.bitcoins.crypto.{ECDigitalSignature, Factory, NetworkElement}
import scodec.bits.ByteVector


sealed abstract class LnInvoiceSignature extends NetworkElement {
  require(recoverId.toInt >= 0 && recoverId.toInt <= 3,
          s"signature recovery byte must be 0,1,2,3, got ${recoverId.toInt}")

  require(
    bytes.length == 65,
    s"LnInvoiceSignatures MUST be 65 bytes in length, got ${bytes.length}")
  def signature: ECDigitalSignature

  def recoverId: UInt8

  def data: Vector[UInt5] = {
    val bytes = signature.toRawRS ++ recoverId.bytes
    Bech32.from8bitTo5bit(bytes)
  }

  override def bytes: ByteVector = {
    signature.toRawRS ++ recoverId.bytes
  }
}

object LnInvoiceSignature extends Factory[LnInvoiceSignature] {

  private case class LnInvoiceSignatureImpl(
      recoverId: UInt8,
      signature: ECDigitalSignature)
      extends LnInvoiceSignature

  def apply(
      recoverId: UInt8,
      signature: ECDigitalSignature): LnInvoiceSignature = {
    LnInvoiceSignatureImpl(recoverId, signature)
  }

  def fromBytes(bytes: ByteVector): LnInvoiceSignature = {
    val sigBytes = bytes.take(64)
    val recoverId = UInt8(bytes(64))

    val signature = ECDigitalSignature.fromRS(sigBytes)

    LnInvoiceSignature.apply(recoverId = recoverId, signature = signature)
  }

  def fromRS(
      r: BigInteger,
      s: BigInteger,
      recovId: UInt8): LnInvoiceSignature = {
    val sig = ECDigitalSignature.fromRS(r, s)
    LnInvoiceSignature(recovId, sig)
  }

  def fromU5s(u5s: Vector[UInt5]): LnInvoiceSignature = {
    val u8s = Bech32.from5bitTo8bit(u5s)
    val bytes = UInt8.toBytes(u8s)
    fromBytes(bytes)
  }
} 
Example 63
Source File: SessionUtil.scala    From akka-http-session   with Apache License 2.0 5 votes vote down vote up
package com.softwaremill.session

import java.math.BigInteger
import java.util.Base64
import java.util.concurrent.ThreadLocalRandom

object SessionUtil {
  def randomString(length: Int): String = {
    // http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string
    val random = ThreadLocalRandom.current()
    new BigInteger(length * 5, random).toString(32) // because 2^5 = 32
  }

  
  def randomServerSecret(): String = randomString(128)

  // Do not change this unless you understand the security issues behind timing attacks.
  // This method intentionally runs in constant time if the two strings have the same length.
  // If it didn't, it would be vulnerable to a timing attack.
  def constantTimeEquals(a: String, b: String): Boolean = {
    if (a.length != b.length) {
      false
    } else {
      var equal = 0
      for (i <- Array.range(0, a.length)) {
        equal |= a(i) ^ b(i)
      }
      equal == 0
    }
  }

  private val HexArray = "0123456789ABCDEF".toCharArray

  def toHexString(bytes: Array[Byte]): String = {
    // from https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java
    val hexChars = new Array[Char](bytes.length * 2)
    var j = 0
    while (j < bytes.length) {
      val v = bytes(j) & 0xFF
      hexChars(j * 2) = HexArray(v >>> 4)
      hexChars(j * 2 + 1) = HexArray(v & 0x0F)
      j += 1
    }
    new String(hexChars)
  }

  def hexStringToByte(hexString: String): Array[Byte] = {
    // https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java
    val len = hexString.length
    val data = new Array[Byte](len / 2)
    var i = 0
    while (i < len) {
      data(i / 2) = ((Character.digit(hexString.charAt(i), 16) << 4) +
        Character.digit(hexString.charAt(i + 1), 16)).toByte
      i += 2
    }

    data
  }

  def toBase64_v0_5_2(bytes: Array[Byte]): String = {
    Base64.getUrlEncoder.encodeToString(bytes)
  }

  def parseBase64_v0_5_2(s: String): Array[Byte] = {
    Base64.getUrlDecoder.decode(s)
  }
} 
Example 64
Source File: Lit.scala    From bosatsu   with Apache License 2.0 5 votes vote down vote up
package org.bykn.bosatsu

import org.typelevel.paiges.{Document, Doc}
import java.math.BigInteger
import fastparse.all._

import Parser.escape

sealed abstract class Lit {
  def repr: String =
    this match {
      case Lit.Integer(i) => i.toString
      case Lit.Str(s) => "\"" + escape('"', s) + "\""
    }
}
object Lit {
  case class Integer(toBigInteger: BigInteger) extends Lit
  case class Str(toStr: String) extends Lit

  val EmptyStr: Str = Str("")

  def fromInt(i: Int): Lit = Integer(BigInteger.valueOf(i.toLong))
  def apply(i: Long): Lit = apply(BigInteger.valueOf(i))
  def apply(bi: BigInteger): Lit = Integer(bi)
  def apply(str: String): Lit = Str(str)

  val integerParser: P[Integer] =
    Parser.integerString.map { str => Integer(new BigInteger(str.filterNot(_ == '_'))) }

  val stringParser: P[Str] = {
    val q1 = '\''
    val q2 = '"'
    def str(q: Char): P[Str] =
      Parser.escapedString(q).map(Str(_))

    str(q1) | str(q2)
  }

  implicit val litOrdering: Ordering[Lit] =
    new Ordering[Lit] {
      def compare(a: Lit, b: Lit): Int =
        (a, b) match {
          case (Integer(a), Integer(b)) => a.compareTo(b)
          case (Integer(_), Str(_)) => -1
          case (Str(_), Integer(_)) => 1
          case (Str(a), Str(b)) => a.compareTo(b)
        }
    }

  val parser: P[Lit] = integerParser | stringParser

  implicit val document: Document[Lit] =
    Document.instance[Lit] {
      case Integer(i) =>
        Doc.text(i.toString)
      case Str(str) =>
        val q = if (str.contains('\'') && !str.contains('"')) '"' else '\''
        Doc.char(q) + Doc.text(escape(q, str)) + Doc.char(q)
    }
} 
Example 65
Source File: JsonFormatSpec.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package io.fintrospect.formats

import java.math.BigInteger

import io.fintrospect.parameters.ParameterSpec
import org.scalatest.{FunSpec, Matchers}

import scala.util.{Success, Try}

abstract class JsonFormatSpec[X <: Y, Y](val jsonLib: JsonLibrary[X, Y]) extends FunSpec with Matchers {

  val format = jsonLib.JsonFormat

  val expectedJson = """{"string":"hello","object":{"field1":"aString"},"int":10,"long":2,"double":1.2,"decimal":1.2,"bigInt":12344,"bool":true,"null":null,"array":["world",true]}"""

  val expected = format.obj("field" -> format.string("value"))

  describe(format.getClass.getSimpleName) {
    it("creates JSON objects as expected") {
      format.compact(format.objSym(
        'string -> format.string("hello"),
        'object -> format.obj("field1" -> format.string("aString")),
        'int -> format.number(10),
        'long -> format.number(2L),
        'double -> format.number(1.2),
        'decimal -> format.number(BigDecimal(1.2)),
        'bigInt -> format.number(new BigInteger("12344")),
        'bool -> format.boolean(true),
        'null -> format.nullNode(),
        'array -> format.array(format.string("world"), format.boolean(true))
      )) shouldEqual expectedJson
    }

    describe("Parse blows up when invalid") {
      it("parse blows up when invalid") {
        Try(format.parse("<12312>")).isFailure shouldBe true
      }
    }

    val paramName = "name"

    describe("ParameterSpec json") {
      describe(getClass.getName + " Json") {
        it("retrieves a valid value") {
          Try(ParameterSpec.json(jsonLib).deserialize(format.compact(expected))) shouldBe Success(expected)
        }

        it("does not retrieve an invalid value") {
          Try(ParameterSpec.json(jsonLib).deserialize("notJson")).isFailure shouldBe true
        }

        it("does not retrieve an null value") {
          Try(ParameterSpec.json(jsonLib).deserialize(null)).isFailure shouldBe true
        }

        it("serializes correctly") {
          ParameterSpec.json(jsonLib).serialize(expected) shouldBe """{"field":"value"}"""
        }
      }
    }
  }
} 
Example 66
Source File: BigQuerySource.scala    From spark-bigquery   with Apache License 2.0 5 votes vote down vote up
package com.samelamin.spark.bigquery.streaming

import java.math.BigInteger
import com.google.cloud.hadoop.io.bigquery.BigQueryStrings
import com.samelamin.spark.bigquery.BigQueryClient
import org.apache.spark.sql.{DataFrame, SQLContext}
import org.apache.spark.sql.execution.streaming.{Offset, _}
import org.apache.spark.sql.types.{BinaryType, StringType, StructField, StructType}
import com.samelamin.spark.bigquery._
import com.samelamin.spark.bigquery.converters.SchemaConverters
import org.joda.time.DateTime
import org.slf4j.LoggerFactory


  override def getBatch(start: Option[Offset], end: Offset): DataFrame = {
    val startIndex = start.getOrElse(LongOffset(0L)).asInstanceOf[LongOffset].offset.toLong
    val endIndex = end.asInstanceOf[LongOffset].offset.toLong
    val startPartitionTime = new DateTime(startIndex).toLocalDate
    val endPartitionTime = new DateTime(endIndex).toLocalDate.toString
    logger.info(s"Fetching data between $startIndex and $endIndex")
    val query =
      s"""
         |SELECT
         |  *
         |FROM
         |  `${fullyQualifiedOutputTableId.replace(':','.')}`
         |WHERE
         |  $timestampColumn BETWEEN TIMESTAMP_MILLIS($startIndex) AND TIMESTAMP_MILLIS($endIndex)
         |  AND _PARTITIONTIME BETWEEN TIMESTAMP('$startPartitionTime') AND TIMESTAMP('$endPartitionTime')
         |  """.stripMargin
    val bigQuerySQLContext = new BigQuerySQLContext(sqlContext)
    val df = bigQuerySQLContext.bigQuerySelect(query)
    df
  }

  override def stop(): Unit = {}
  def getConvertedSchema(sqlContext: SQLContext): StructType = {
    val bigqueryClient = BigQueryClient.getInstance(sqlContext)
    val tableReference = BigQueryStrings.parseTableReference(fullyQualifiedOutputTableId)
    SchemaConverters.BQToSQLSchema(bigqueryClient.getTableSchema(tableReference))
  }
}

object BigQuerySource {
  val DEFAULT_SCHEMA = StructType(
    StructField("Sample Column", StringType) ::
      StructField("value", BinaryType) :: Nil
  )
} 
Example 67
Source File: ChecksumType.scala    From coursier   with Apache License 2.0 5 votes vote down vote up
package coursier.publish.checksum

import java.math.BigInteger
import java.util.Locale


sealed abstract class ChecksumType(
  val name: String,
  val extension: String,
  val size: Int
) extends Product with Serializable {
  private val firstInvalidValue = BigInteger.valueOf(16L).pow(size)
  def validValue(value: BigInteger): Boolean =
    value.compareTo(BigInteger.ZERO) >= 0 &&
      value.compareTo(firstInvalidValue) < 0
}

object ChecksumType {
  case object SHA1 extends ChecksumType("sha-1", "sha1", 40)
  case object MD5 extends ChecksumType("md5", "md5", 32)

  val all = Seq(SHA1, MD5)
  val map = all.map(c => c.name -> c).toMap

  def parse(s: String): Either[String, ChecksumType] =
    map
      .get(s.toLowerCase(Locale.ROOT))
      .toRight(s"Unrecognized checksum: $s")
}