⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 securerandom.java

📁 进行与数字证书相关开发必须的java源码
💻 JAVA
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   SecureRandom.java

package jit.security;

import java.util.Random;
import jit.crypto.digests.SHA1Digest;

public class SecureRandom extends Random
{

    private static SecureRandom rand = new SecureRandom();
    private byte seed[];
    private long counter;
    private SHA1Digest digest;
    private byte state[];
    private byte intBytes[];
    private byte longBytes[];

    public SecureRandom()
    {
        super(0L);
        counter = 1L;
        digest = new SHA1Digest();
        state = new byte[digest.getDigestSize()];
        intBytes = new byte[4];
        longBytes = new byte[8];
        setSeed(System.currentTimeMillis());
    }

    public SecureRandom(byte inSeed[])
    {
        counter = 1L;
        digest = new SHA1Digest();
        state = new byte[digest.getDigestSize()];
        intBytes = new byte[4];
        longBytes = new byte[8];
        setSeed(inSeed);
    }

    public static SecureRandom getInstance(String algorithm)
    {
        return new SecureRandom();
    }

    public static SecureRandom getInstance(String algorithm, String provider)
    {
        return new SecureRandom();
    }

    public static byte[] getSeed(int numBytes)
    {
        byte rv[] = new byte[numBytes];
        rand.setSeed(System.currentTimeMillis());
        rand.nextBytes(rv);
        return rv;
    }

    public byte[] generateSeed(int numBytes)
    {
        byte rv[] = new byte[numBytes];
        nextBytes(rv);
        return rv;
    }

    public void setSeed(byte inSeed[])
    {
        digest.update(inSeed, 0, inSeed.length);
    }

    public void nextBytes(byte bytes[])
    {
        int stateOff = 0;
        digest.doFinal(state, 0);
        for(int i = 0; i != bytes.length; i++)
        {
            if(stateOff == state.length)
            {
                byte b[] = longToBytes(counter++);
                digest.update(b, 0, b.length);
                digest.update(state, 0, state.length);
                digest.doFinal(state, 0);
                stateOff = 0;
            }
            bytes[i] = state[stateOff++];
        }

        byte b[] = longToBytes(counter++);
        digest.update(b, 0, b.length);
        digest.update(state, 0, state.length);
    }

    public void setSeed(long rSeed)
    {
        if(rSeed != (long)0)
            setSeed(longToBytes(rSeed));
    }

    public int nextInt()
    {
        nextBytes(intBytes);
        int result = 0;
        for(int i = 0; i < 4; i++)
            result = (result << 8) + (intBytes[i] & 0xff);

        return result;
    }

    protected final int next(int numBits)
    {
        int size = (numBits + 7) / 8;
        byte bytes[] = new byte[size];
        nextBytes(bytes);
        int result = 0;
        for(int i = 0; i < size; i++)
            result = (result << 8) + (bytes[i] & 0xff);

        return result & (1 << numBits) - 1;
    }

    private byte[] longToBytes(long val)
    {
        for(int i = 0; i != 8; i++)
        {
            longBytes[i] = (byte)(int)val;
            val >>>= 8;
        }

        return longBytes;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -