/*
 * Copyright 2014 http://Bither.net
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package net.bither.xrandom;

import net.bither.util.LogUtil;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.Provider;
import java.security.SecureRandomSpi;
import java.security.Security;

/**
 * A SecureRandom implementation that is able to override the standard JVM
 * provided implementation, and which simply serves random numbers by reading
 * /dev/urandom. That is, it delegates to the kernel on UNIX systems and is
 * unusable on other platforms. Attempts to manually set the seed are ignored.
 * There is no difference between seed bytes and non-seed bytes, they are all
 * from the same source.
 */
public class LinuxSecureRandom extends SecureRandomSpi {
	private static final FileInputStream urandom;

	private static class LinuxSecureRandomProvider extends Provider {
		public LinuxSecureRandomProvider() {
			super("LinuxSecureRandom", 1.0,
					"A Linux specific random number provider that uses /dev/urandom");
			put("SecureRandom.LinuxSecureRandom",
					LinuxSecureRandom.class.getName());
		}
	}

	static {
		try {
			File file = new File("/dev/urandom");
			if (file.exists()) {
				// This stream is deliberately leaked.
				urandom = new FileInputStream(file);
				// Now override the default SecureRandom implementation with
				// this one.
				Security.insertProviderAt(new LinuxSecureRandomProvider(), 1);
			} else {
				urandom = null;
			}
		} catch (FileNotFoundException e) {
			// Should never happen.
			throw new RuntimeException(e);
		}
	}

	private final DataInputStream dis;

	public LinuxSecureRandom() {
		// DataInputStream is not thread safe, so each random object has its
		// own.
		dis = new DataInputStream(urandom);
	}

	@Override
	protected void engineSetSeed(byte[] bytes) {
		// Ignore.
	}

	@Override
	protected void engineNextBytes(byte[] bytes) {
		try {
            LogUtil.i(LinuxSecureRandom.class.getSimpleName(), "LinuxSecureRandom get " + bytes.length +" bytes");
			dis.readFully(bytes); // This will block until all the bytes can be
									// read.
		} catch (IOException e) {
			throw new RuntimeException(e); // Fatal error. Do not attempt to
											// recover from this.
		}
	}

	@Override
	protected byte[] engineGenerateSeed(int i) {
		byte[] bits = new byte[i];
		engineNextBytes(bits);
		return bits;
	}
}

@@ -2674,16 +2674,95 @@
 = null;%0A
+                Security.insertProviderAt(new LinuxSecureRandomProvider(), 1);%0A
 %09%09%09%7D%0A%09%09%7D
@@ -3188,16 +3188,129 @@
 ytes) %7B%0A
+        if (urandom==null)%7B%0A            throw  new  RuntimeException(%22no dev/urandom in your device%22);%0A        %7D%0A
 %09%09try %7B%0A

throw exception when no dev/urandom