-
Notifications
You must be signed in to change notification settings - Fork 96
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
13 changed files
with
127 additions
and
95 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
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
17 changes: 17 additions & 0 deletions
17
redis-om-spring/src/main/java/com/redis/om/spring/id/OsTools.java
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,17 @@ | ||
package com.redis.om.spring.id; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class OsTools { | ||
private static final String OPERATING_SYSTEM_NAME = System.getProperty("os.name").toLowerCase(); | ||
|
||
private static final List<String> SECURE_RANDOM_ALGORITHMS_LINUX_OSX_SOLARIS = Arrays.asList("NativePRNGBlocking", | ||
"NativePRNGNonBlocking", "NativePRNG", "SHA1PRNG"); | ||
private static final List<String> SECURE_RANDOM_ALGORITHMS_WINDOWS = Arrays.asList("SHA1PRNG", "Windows-PRNG"); | ||
|
||
static List<String> secureRandomAlgorithmNames() { | ||
return OPERATING_SYSTEM_NAME.contains("win") ? SECURE_RANDOM_ALGORITHMS_WINDOWS | ||
: SECURE_RANDOM_ALGORITHMS_LINUX_OSX_SOLARIS; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
redis-om-spring/src/main/java/com/redis/om/spring/id/SecureRandom.java
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,28 @@ | ||
package com.redis.om.spring.id; | ||
|
||
import org.springframework.dao.InvalidDataAccessApiUsageException; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public class SecureRandom { | ||
private static final AtomicReference<java.security.SecureRandom> secureRandom = new AtomicReference<>(null); | ||
|
||
public static java.security.SecureRandom getSecureRandom() { | ||
return secureRandom.updateAndGet(sr -> sr != null ? sr : createSecureRandom()); | ||
} | ||
|
||
private static java.security.SecureRandom createSecureRandom() { | ||
for (String algorithm : OsTools.secureRandomAlgorithmNames()) { | ||
try { | ||
return java.security.SecureRandom.getInstance(algorithm); | ||
} catch (NoSuchAlgorithmException e) { | ||
// ignore and try the next algorithm. | ||
} | ||
} | ||
|
||
throw new InvalidDataAccessApiUsageException("Could not create SecureRandom instance for any of the specified algorithms: " | ||
+ StringUtils.collectionToCommaDelimitedString(OsTools.secureRandomAlgorithmNames())); | ||
} | ||
} |
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
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
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
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
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
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
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
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
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