Skip to content

Latest commit

 

History

History
51 lines (44 loc) · 2 KB

README.md

File metadata and controls

51 lines (44 loc) · 2 KB

SwiftRNG SecureRandomSPI

A SecureRandomSPI that makes SwiftRNG devices available to SecureRandom.

This code automatically detect attached devices on Linux and macOS, and when a machine has more of one SwiftRNG it uses round-robin algorithm to balance getRandomBytes calls between devices.

To use it you should add this provider to dependency like

<dependency>
  <groupId>ky.korins</groupId>
  <artifactId>swrng-provider</artifactId>
  <version>1.0.0</version>
</dependency>

and use via specified instance:

  SecureRandom random = SecureRandom.getInstance("SwiftRNG");

But you should configure it. The easy way is adding this security provider by hand:

  Security.addProvider(new SwiftRNGProvider());

or you can add it to java.security as

security.provider.N=SwiftRNG

where N should be the value of the last provider incremented by 1.

You can also select one or more devices that this code should use. For example we would like to use SwiftRNG that attached as /dev/cu.usbmodemSWRNGP000A0061 we can do:

SecureRandom random = SecureRandom.getInstance("SwiftRNG", new SwiftRNGParameters(Collections.singletonList("/dev/cu.usbmodemSWRNGP000A0061")));

or you can specified at java.security comma separated list of used devices such as:

securerandom.swiftrng.devices=/dev/cu.usbmodemSWRNGP000A0061,/dev/cu.usbmodemSWRNGP000A0062

and this is the only way to use this provider at Windows where you can get path to the device by mode.

Thus, this code is using locking to prevent parallel using the device and this add limitation that only one SecureRandom instance can be created per device that is thread safe.

You also can test your configuration by running a main method from ky.korins.swrng.SwiftRNGDevices that returns all found devices like that

SwiftRNGDevice{path=/dev/cu.usbmodemSWRNGP000A0061, model='SWRNGPRO', version='V2.1', serialNumber='XXX'}
SwiftRNGDevice{path=/dev/cu.usbmodemSWRNGP000A0062, model='SWRNGPRO', version='V2.1', serialNumber='XXX'}