Package com.subgraph.orchid.crypto

Source Code of com.subgraph.orchid.crypto.TorRandom

package com.subgraph.orchid.crypto;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import com.subgraph.orchid.TorException;

public class TorRandom {

  private final SecureRandom random;
 
  public TorRandom() {
    random = createRandom();
  }
 
  private static SecureRandom createRandom() {
    try {
      return SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
      throw new TorException(e);
    }
  }

  public byte[] getBytes(int n) {
    final byte[] bs = new byte[n];
    random.nextBytes(bs);
    return bs;
  }

  public long nextLong(long n) {
    long bits, val;
    do {
      bits = nextLong();
      val = bits % n;
    } while(bits - val + (n - 1) < 0);
    return val;
  }

  public int nextInt(int n) {
    return random.nextInt(n);
  }
 
  public int nextInt() {
    return random.nextInt() & Integer.MAX_VALUE;
  }
 
  /**
   * Return a uniformly distributed positive random value between 0 and Long.MAX_VALUE
   *
   * @return A positive random value between 0 and Long.MAX_VALUE.
   */
  public long nextLong() {
    return random.nextLong() & Long.MAX_VALUE;
  }

}
TOP

Related Classes of com.subgraph.orchid.crypto.TorRandom

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.