Esempio n. 1
0
  public void initialize() {
    e = BigInteger.valueOf(65537);
    SecureRandom sr = new SecureRandom();

    BigInteger diff = BigInteger.valueOf(2).pow(256);

    /*
    generate 2 prime numbers greater than 512 bits and that have difference of 2^256
    */
    do {
      sr.generateSeed(size);
      p = new BigInteger(size, 256, sr);
      sr.generateSeed(size);
      q = new BigInteger(size, 256, sr);

    } while (p.bitLength() < size
        || q.bitLength() < size
        || !p.isProbablePrime(256)
        || !q.isProbablePrime(256)
        || p.subtract(q).abs().compareTo(diff) != 1);

    n = p.multiply(q);
    phi_n = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
    d = e.modInverse(phi_n);
  }
Esempio n. 2
0
 /**
  * Changes the internal state to a new, fully-random version that should have no relation to the
  * previous state. May be somewhat slow; you shouldn't need to call this often.
  */
 public void randomize() {
   byte[] bytes = sec.generateSeed(128);
   for (int i = sec.nextInt() & 127, c = 0; c < 128; c++, i = i + 1 & 127) {
     state[i & 15] |= bytes[c] << ((i >> 4) << 3);
   }
   choice = sec.nextInt(16);
 }
 // ---------------------------------------------------------------
 private byte[] getSalt(int saltSize) throws NoSuchAlgorithmException, NoSuchProviderException {
   // SecureRandom sr = SecureRandom.getInstance( SECURE_RANDOM_ALGORITHM,
   // SECURITY_PROVIDER );
   SecureRandom sr = new SecureRandom();
   sr.setSeed(System.currentTimeMillis());
   return sr.generateSeed(saltSize);
 }
Esempio n. 4
0
 /*
  * Generate random salt
  */
 private byte[] getSalt() {
   // Generate a random salt.
   byte[] salt = new byte[SALT_LEN];
   if (random == null) {
     random = new SecureRandom();
   }
   salt = random.generateSeed(SALT_LEN);
   return salt;
 }
Esempio n. 5
0
  static {
    try {
      rnd = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
      rnd = new SecureRandom(); // Use default if prefered provider is unavailable
    }

    byte[] seed = rnd.generateSeed(64);
    rnd.setSeed(seed);
  }
  /** Fall sem framkallar nýtt salt. */
  public byte[] getNewSalt() throws Exception {
    String TAG = "PasswordHandler getNewSalt";

    try {
      SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
      return random.generateSeed(saltLen);
    } catch (Exception e) {
      // Villa
      throw new Exception("Something went wrong, " + TAG + ": " + e.getMessage());
    }
  }
 public static byte[] randomKey(final int size) {
   try {
     final KeyGenerator gen = KeyGenerator.getInstance(ALGORITHM);
     final SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
     sr.generateSeed(128);
     gen.init(128, sr);
     return gen.generateKey().getEncoded();
   } catch (final Exception e) {
     throw new RuntimeException(e);
   }
 }
 private SecureRandom createSecureRandom(String algorithm, String provider, int keyLength) {
   try {
     SecureRandom random = SecureRandom.getInstance(algorithm, provider);
     random.setSeed(random.generateSeed(keyLength));
     return random;
   } catch (NoSuchAlgorithmException e) {
     throw new IllegalArgumentException(
         "Not a supported SecureRandom key generation algorithm", e);
   } catch (NoSuchProviderException e) {
     throw new IllegalArgumentException("Not a supported SecureRandom key provider", e);
   }
 }
 public String generateToken(String userName) throws PhrescoException {
   String token = "";
   try {
     SecureRandom random = SecureRandom.getInstance(ALGORITHM_NAME);
     byte[] seed = random.generateSeed(132);
     random.setSeed(seed);
     token = new BigInteger(132, random).toString(32);
     tokenCache.put(token, userName);
   } catch (NoSuchAlgorithmException e) {
     throw new PhrescoException(e);
   }
   return token;
 }
  public Client createClient(
      PackageInfoOAuth packageInfo,
      ClientType clientType,
      String[] redirectURIs,
      Map<String, Object> properties) {
    if (properties == null) {
      properties = new HashMap<String, Object>();
    }

    Object flows = properties.get(Client.PROPERTY_SUPPORTED_FLOWS);
    if (flows == null) {
      flows = defaultSupportedFlow.get(clientType);
      properties.put(Client.PROPERTY_SUPPORTED_FLOWS, flows);
    }

    /*
     * The authorization server MUST require the following clients to
     * register their redirection endpoint: o Public clients. o Confidential
     * clients utilizing the implicit grant type. (3.1.2.2. Registration
     * Requirements)
     */
    if (clientType == ClientType.PUBLIC
        || (clientType == ClientType.CONFIDENTIAL
            && Arrays.asList((Object[]) flows).contains(ResponseType.token))) {
      if (redirectURIs == null || redirectURIs.length == 0) {
        throw new IllegalArgumentException("RedirectionURI(s) required.");
      }
    }

    String clientId = UUID.randomUUID().toString();
    char[] clientSecret = null;
    if (clientType == ClientType.CONFIDENTIAL
        || (clientType == ClientType.PUBLIC && isIssueClientSecretToPublicClients())) {
      // Issue a client secret to the confidential client.
      if (count++ > RESEED_CLIENTS) {
        count = 0;
        random.setSeed(random.generateSeed(20));
      }
      byte[] secret = new byte[20];
      random.nextBytes(secret);
      clientSecret = Base64.encode(secret, false).toCharArray();
    }

    return createClient(packageInfo, clientId, clientSecret, clientType, redirectURIs, properties);
  }
Esempio n. 11
0
  public static void generateRandomSeed() {

    // create a 160-bit seed
    SecureRandom sr0 = new SecureRandom();
    byte[] seed = sr0.generateSeed(SEED_BYTES);

    // make a string representation
    StringBuffer buffer = new StringBuffer(SEED_BYTES * 3);
    for (int i = 0; i < seed.length; i++) {
      buffer.append(',');
      int seedByte = seed[i] & 0xFF;
      if (seedByte < 16) {
        buffer.append('0');
      }
      buffer.append(Integer.toHexString(seedByte));
    }

    // save it in local.properties (Note: remove leading comma)
    LocalProperties.setProperty("random_seed", buffer.substring(1));
  }
Esempio n. 12
0
 public synchronized byte[] generateSeed(int numBytes) {
   checkActive();
   return secureRandom.generateSeed(numBytes);
 }
Esempio n. 13
0
  private void seedFromExternalStuff(boolean canBlock) {
    byte[] buf = new byte[32];
    if (File.separatorChar == '/') {
      DataInputStream dis = null;
      FileInputStream fis = null;
      File hwrng = new File("/dev/hwrng");
      if (hwrng.exists() && hwrng.canRead())
        try {
          fis = new FileInputStream(hwrng);
          dis = new DataInputStream(fis);
          dis.readFully(buf);
          consumeBytes(buf);
          dis.readFully(buf);
          consumeBytes(buf);
          dis.close();
        } catch (Throwable t) {
          Logger.normal(this, "Can't read /dev/hwrng even though exists and is readable: " + t, t);
        } finally {
          Closer.close(dis);
          Closer.close(fis);
        }

      boolean isSystemEntropyAvailable = true;
      // Read some bits from /dev/urandom
      try {
        fis = new FileInputStream("/dev/urandom");
        dis = new DataInputStream(fis);
        dis.readFully(buf);
        consumeBytes(buf);
        dis.readFully(buf);
        consumeBytes(buf);
      } catch (Throwable t) {
        Logger.normal(this, "Can't read /dev/urandom: " + t, t);
        // We can't read it; let's skip /dev/random and seed from SecureRandom.generateSeed()
        canBlock = true;
        isSystemEntropyAvailable = false;
      } finally {
        Closer.close(dis);
        Closer.close(fis);
      }
      if (canBlock && isSystemEntropyAvailable)
        // Read some bits from /dev/random
        try {
          fis = new FileInputStream("/dev/random");
          dis = new DataInputStream(fis);
          dis.readFully(buf);
          consumeBytes(buf);
          dis.readFully(buf);
          consumeBytes(buf);
        } catch (Throwable t) {
          Logger.normal(this, "Can't read /dev/random: " + t, t);
        } finally {
          Closer.close(dis);
          Closer.close(fis);
        }
      fis = null;
    } else
      // Force generateSeed(), since we can't read random data from anywhere else.
      // Anyway, Windows's CAPI won't block.
      canBlock = true;
    if (canBlock) {
      // SecureRandom hopefully acts as a proxy for CAPI on Windows
      buf = sr.generateSeed(32);
      consumeBytes(buf);
      buf = sr.generateSeed(32);
      consumeBytes(buf);
    }
    // A few more bits
    consumeString(Long.toHexString(Runtime.getRuntime().freeMemory()));
    consumeString(Long.toHexString(Runtime.getRuntime().totalMemory()));
  }
Esempio n. 14
0
 static {
   random.setSeed(
       OLongSerializer.INSTANCE.deserialize(
           secureRandom.generateSeed(OLongSerializer.LONG_SIZE), 0));
 }
Esempio n. 15
0
 /**
  * 盐初始化 长度8字节
  *
  * @return
  */
 public static byte[] initSalt() {
   SecureRandom random = new SecureRandom();
   return random.generateSeed(8);
 }
 public static void main(String... arguments) throws Exception {
   byte[] bytes;
   SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
   bytes = prng.generateSeed(1);
 }
Esempio n. 17
0
 /** Helper to reseed the shared secure random number generator. */
 public static void reseedSharedRandom() {
   sharedSecureRandom.setSeed(sharedSecureRandom.generateSeed(8));
 }