package org.torproject.jtor.crypto;

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

import org.torproject.jtor.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) {
		// XXX not uniformly distributed
		return random.nextLong() % n;
	}

	public int nextInt(int n) {
		return random.nextInt(n);
	}
	
	public int nextInt() {
		return random.nextInt();
	}
	
	public long nextLong() {
		return random.nextLong();
	}

}

@@ -180,17 +180,16 @@
 andom %7B%0A
-%09
 %0A%09privat
@@ -629,39 +629,32 @@
 ibuted%0A%09%09return 
-random.
 nextLong() %25 n;%0A
@@ -827,16 +827,33 @@
 xtLong()
+ & Long.MAX_VALUE
 ;%0A%09%7D%0A%0A%7D%0A

Don't return negative values