This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
48 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,27 @@ | ||
package tsec.common | ||
|
||
import java.security.SecureRandom | ||
import java.util.concurrent.atomic.LongAdder | ||
|
||
/** A trait that manages a secureRandom instance. | ||
*/ | ||
trait ManagedRandom { | ||
|
||
private val SecureRandomAlgorithm = "NativePRNGNonBlocking" | ||
|
||
/** Cache our random, and seed it properly as per | ||
* [[https://tersesystems.com/2015/12/17/the-right-way-to-use-securerandom/]] | ||
*/ | ||
private[tsec] var cachedRand: SecureRandom = { | ||
val r = SecureRandom.getInstance(SecureRandomAlgorithm) | ||
r.nextBytes(new Array[Byte](20)) | ||
private[tsec] val cachedRand: SecureRandom = { | ||
val r = { | ||
if (OSUtil.isWindows) new SecureRandom() | ||
else SecureRandom.getInstance(ManagedRandom.UnixURandom) | ||
} | ||
r.nextBytes(new Array[Byte](20)) //Force reseed | ||
r | ||
} | ||
|
||
/** We will keep a reference to how many times our random is utilized | ||
* After a sensible Integer.MaxValue/5 times, we should reseed | ||
*/ | ||
private val adder: LongAdder = new LongAdder | ||
private val MaxBeforeReseed = (Integer.MAX_VALUE / 5).toLong | ||
|
||
private def reSeed(): Unit = { | ||
adder.reset() | ||
val tmpRand = SecureRandom.getInstance(SecureRandomAlgorithm) | ||
tmpRand.nextBytes(new Array[Byte](20)) | ||
cachedRand = tmpRand | ||
} | ||
|
||
private[tsec] def forceIncrement: Unit = { | ||
adder.increment() | ||
if (adder.sum() == MaxBeforeReseed) | ||
reSeed() | ||
} | ||
|
||
def nextBytes(bytes: Array[Byte]): Unit = { | ||
adder.increment() | ||
if (adder.sum() == MaxBeforeReseed) | ||
reSeed() | ||
def nextBytes(bytes: Array[Byte]): Unit = | ||
cachedRand.nextBytes(bytes) | ||
} | ||
} | ||
|
||
object ManagedRandom { | ||
private[ManagedRandom] val UnixURandom = "NativePRNGNonBlocking" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tsec.common | ||
|
||
import java.security.{AccessController, PrivilegedAction} | ||
import java.util.Locale | ||
|
||
object OSUtil { | ||
|
||
lazy val isWindows: Boolean = { | ||
OSUtil.getSystemProperty("os.name", "").toLowerCase(Locale.US).contains("win") | ||
} | ||
|
||
lazy val isOsx: Boolean = { | ||
val osName = OSUtil | ||
.getSystemProperty("os.name", "") | ||
.toLowerCase(Locale.US) | ||
.replaceAll("[^a-z0-9]+", "") | ||
|
||
osName.startsWith("macosx") || osName.startsWith("osx") | ||
} | ||
|
||
def getSystemProperty(key: String, default: String): String = { | ||
if (key == null) throw new NullPointerException("key") | ||
if (key.isEmpty) throw new IllegalArgumentException("key must not be empty.") | ||
try if (System.getSecurityManager == null) System.getProperty(key) | ||
else | ||
AccessController.doPrivileged(new PrivilegedAction[String]() { | ||
override def run: String = System.getProperty(key) | ||
}) | ||
catch { | ||
case e: SecurityException => | ||
default | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters