Secure Random Number generation in scala

2,710 views
Skip to first unread message

jitendra shelar

unread,
Jun 25, 2014, 2:14:35 PM6/25/14
to scala...@googlegroups.com
Hi,

I am newbie to scala. I am working on secure random key generation.

Eg: In Java, we have following

import java.security.SecureRandom;
..
Random ranGen = new SecureRandom();
byte[] aesKey = new byte[8]; // 8 bytes = 64 bits
ranGen.nextBytes(aesKey);

Similarly, Can somebody please tell me how to generate the secure random number  in scala and convert it into alpha-numeric format?

Thanks in advance,
Jitendra





Rex Kerr

unread,
Jun 25, 2014, 2:34:58 PM6/25/14
to jitendra shelar, scala-user
Scala does not provide a secure random number facility.  Just do it the same way that you would in Java, except using Scala syntax.

  --Rex


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oliver Ruebenacker

unread,
Jun 25, 2014, 4:08:03 PM6/25/14
to jitendra shelar, scala-user

     Hello,

scala> import java.security.SecureRandom
import java.security.SecureRandom

scala> val ranGen = new SecureRandom()
ranGen: java.security.SecureRandom = java.security.SecureRandom@4dbed645

scala> val aesKey = new Array[Byte](8)
aesKey: Array[Byte] = Array(0, 0, 0, 0, 0, 0, 0, 0)

scala> ranGen.nextBytes(aesKey)

scala> aesKey
res1: Array[Byte] = Array(-69, 111, 1, 12, -88, -108, -99, 87)

scala>

     Best,
     Oliver



--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Be always grateful, but never satisfied.

Erik Osheim

unread,
Jun 25, 2014, 4:37:26 PM6/25/14
to jitendra shelar, scala...@googlegroups.com
On Wed, Jun 25, 2014 at 11:14:35AM -0700, jitendra shelar wrote:
> Similarly, Can somebody please tell me how to generate the secure
> random number in scala and convert it into alpha-numeric format?

Spire [1] has a wrapper around Java's SecureRandom class [2] [3]:

import spire.random.mutable.SecureJava

SecureJava().generateBytes(16).map("%02x" format _).mkString

-- Erik

[1] https://github.com/non/spire

[2] You may want to seed the RNG yourself. Also, this is the code
you'd use with Spire's most recent release. In the master branch the
class is found in spire.random.rng instead.

[3] This is probably not the fastest method to generate a very large
string, but it is quite terse.

Rex Kerr

unread,
Jun 25, 2014, 7:47:39 PM6/25/14
to Erik Osheim, jitendra shelar, scala-user
On Wed, Jun 25, 2014 at 1:37 PM, Erik Osheim <er...@plastic-idolatry.com> wrote:
On Wed, Jun 25, 2014 at 11:14:35AM -0700, jitendra shelar wrote:
> Similarly, Can somebody please tell me how to generate the secure
> random number in scala and convert it into alpha-numeric format?

Spire [1] has a wrapper around Java's SecureRandom class [2] [3]:

import spire.random.mutable.SecureJava

SecureJava().generateBytes(16).map("%02x" format _).mkString

-- Erik

[1] https://github.com/non/spire

[2] You may want to seed the RNG yourself. Also, this is the code
you'd use with Spire's most recent release. In the master branch the
class is found in spire.random.rng instead.

I wouldn't advise this unless you really know what you're doing.  Secure RNGs are still deterministic, so if you choose your seed predictably, your number stream will be vulnerable.

Better to let SecureRandom do the seeding for you (usually by deferring to an OS routine), or use its seed generation method if you must store the seed for later use.

  --Rex
 


jitendra shelar

unread,
Jun 26, 2014, 2:48:42 AM6/26/14
to scala...@googlegroups.com

Thanks All for your excellent help and suggestions. 
It was really helpful.

Thanks & Regards,
Jitendra
Reply all
Reply to author
Forward
0 new messages