https://github.com/catap/swrng-provider
A SecureRandomSPI that makes SwiftRNG devices available to SecureRandom
https://github.com/catap/swrng-provider
clojure java jvm scala securerandom swiftrng
Last synced: about 2 months ago
JSON representation
A SecureRandomSPI that makes SwiftRNG devices available to SecureRandom
- Host: GitHub
- URL: https://github.com/catap/swrng-provider
- Owner: catap
- License: unlicense
- Created: 2020-06-12T12:32:54.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-13T22:45:05.000Z (over 5 years ago)
- Last Synced: 2025-07-20T05:02:55.225Z (11 months ago)
- Topics: clojure, java, jvm, scala, securerandom, swiftrng
- Language: Java
- Size: 45.9 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# 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
```
ky.korins
swrng-provider
1.0.0
```
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'}
```