Random.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch.jce;
00031 
00032 import java.security.SecureRandom;
00033 
00034 public class Random implements com.jcraft.jsch.Random{
00035   private byte[] tmp=new byte[16];
00036   private SecureRandom random=null;
00037   public Random(){
00038 
00039     // We hope that 'new SecureRandom()' will use NativePRNG algorithm
00040     // on Sun's Java5 for GNU/Linux and Solaris.
00041     // It seems NativePRNG refers to /dev/urandom and it must not be blocked,
00042     // but NativePRNG is slower than SHA1PRNG ;-<
00043     // TIPS: By adding option '-Djava.security.egd=file:/dev/./urandom'
00044     //       SHA1PRNG will be used instead of NativePRNG.
00045     // On MacOSX, 'new SecureRandom()' will use NativePRNG algorithm and
00046     // it is also slower than SHA1PRNG.
00047     // On Windows, 'new SecureRandom()' will use SHA1PRNG algorithm.
00048     random=new SecureRandom();
00049 
00050     /*
00051     try{ 
00052       random=SecureRandom.getInstance("SHA1PRNG"); 
00053       return;
00054     }
00055     catch(java.security.NoSuchAlgorithmException e){ 
00056       // System.err.println(e); 
00057     }
00058 
00059     // The following code is for IBM's JCE
00060     try{ 
00061       random=SecureRandom.getInstance("IBMSecureRandom"); 
00062       return;
00063     }
00064     catch(java.security.NoSuchAlgorithmException ee){ 
00065       //System.err.println(ee); 
00066     }
00067     */
00068   }
00069   public void fill(byte[] foo, int start, int len){
00070     /*
00071     // This case will not become true in our usage.
00072     if(start==0 && foo.length==len){
00073       random.nextBytes(foo);
00074       return;
00075     }
00076     */
00077     if(len>tmp.length){ tmp=new byte[len]; }
00078     random.nextBytes(tmp);
00079     System.arraycopy(tmp, 0, foo, start, len);
00080   }
00081 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1