ReseedingSecureRandom.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2014, 2015, Enrico Maria Crisostomo
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * * Neither the name of the author nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package com.warrenstrange.googleauth;
  31. import java.security.NoSuchAlgorithmException;
  32. import java.security.NoSuchProviderException;
  33. import java.security.SecureRandom;
  34. import java.util.concurrent.atomic.AtomicInteger;
  35. /**
  36. * Date: 08/04/14
  37. * Time: 15:21
  38. *
  39. * @author Enrico M. Crisostomo
  40. */
  41. class ReseedingSecureRandom {
  42. private static final int MAX_OPERATIONS = 1000000;
  43. private final String provider;
  44. private final String algorithm;
  45. private final AtomicInteger count = new AtomicInteger(0);
  46. private SecureRandom secureRandom;
  47. @SuppressWarnings("UnusedDeclaration")
  48. ReseedingSecureRandom() {
  49. this.algorithm = null;
  50. this.provider = null;
  51. buildSecureRandom();
  52. }
  53. @SuppressWarnings("UnusedDeclaration")
  54. ReseedingSecureRandom(String algorithm) {
  55. if (algorithm == null) {
  56. throw new IllegalArgumentException("Algorithm cannot be null.");
  57. }
  58. this.algorithm = algorithm;
  59. this.provider = null;
  60. buildSecureRandom();
  61. }
  62. ReseedingSecureRandom(String algorithm, String provider) {
  63. if (algorithm == null) {
  64. throw new IllegalArgumentException("Algorithm cannot be null.");
  65. }
  66. if (provider == null) {
  67. throw new IllegalArgumentException("Provider cannot be null.");
  68. }
  69. this.algorithm = algorithm;
  70. this.provider = provider;
  71. buildSecureRandom();
  72. }
  73. private void buildSecureRandom() {
  74. try {
  75. if (this.algorithm == null && this.provider == null) {
  76. this.secureRandom = new SecureRandom();
  77. } else if (this.provider == null) {
  78. this.secureRandom = SecureRandom.getInstance(this.algorithm);
  79. } else {
  80. this.secureRandom = SecureRandom.getInstance(this.algorithm, this.provider);
  81. }
  82. } catch (NoSuchAlgorithmException e) {
  83. throw new GoogleAuthenticatorException(
  84. String.format(
  85. "Could not initialise SecureRandom " +
  86. "with the specified algorithm: %s",
  87. this.algorithm
  88. ), e
  89. );
  90. } catch (NoSuchProviderException e) {
  91. throw new GoogleAuthenticatorException(
  92. String.format(
  93. "Could not initialise SecureRandom " +
  94. "with the specified provider: %s",
  95. this.provider
  96. ), e
  97. );
  98. }
  99. }
  100. void nextBytes(byte[] bytes) {
  101. if (count.incrementAndGet() > MAX_OPERATIONS) {
  102. synchronized (this) {
  103. if (count.get() > MAX_OPERATIONS) {
  104. buildSecureRandom();
  105. count.set(0);
  106. }
  107. }
  108. }
  109. this.secureRandom.nextBytes(bytes);
  110. }
  111. }