Example usage for java.security SecureRandom generateSeed

List of usage examples for java.security SecureRandom generateSeed

Introduction

In this page you can find the example usage for java.security SecureRandom generateSeed.

Prototype

public byte[] generateSeed(int numBytes) 

Source Link

Document

Returns the given number of seed bytes, computed using the seed generation algorithm that this class uses to seed itself.

Usage

From source file:org.globusonline.nexus.GlobusOnlineRestClient.java

private long generateNonce() {
    SecureRandom sr = null;
    try {//w  w w . ja  v  a  2 s.c o m
        sr = SecureRandom.getInstance("SHA1PRNG");
        byte[] bytes = new byte[1024 / 8];
        sr.nextBytes(bytes);
        int seedByteCount = 10;
        byte[] seed = sr.generateSeed(seedByteCount);
        sr = SecureRandom.getInstance("SHA1PRNG");
        sr.setSeed(seed);
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return sr.nextLong();
}

From source file:de.rrze.idmone.utils.jidgen.random.RandomFactory.java

/**
 * Create a two pseudo random generator by utilizing the
 * <em>SecureRandom</em> class provided by SUN. Uses a two step procedure
 * for feeding the generator seed with two separate SecureRandom instances.
 * /*  w w w  .j  av  a  2 s.  com*/
 * @see http://java.sun.com/j2se/1.4.2/docs/api/java/security/SecureRandom.html
 * 
 * @param algorithm
 *            The algorithm used for creating the pseudo random generator
 * @param provider
 *            the provider identifier
 * @return a seeded <em>SecureRandom</em>
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 */
private SecureRandom initSecureRandom(String algorithm, String provider)
        throws NoSuchAlgorithmException, NoSuchProviderException {
    logger.debug(Messages.getString("RandomFactory.INIT") + algorithm + " : " + provider);
    if (provider == null)
        provider = PROVIDER_DEFAULT;

    // Create a secure random number generator
    SecureRandom sr = SecureRandom.getInstance(algorithm, provider);

    // Get 1024 random bits
    byte[] bytes = new byte[1024 / 8];
    sr.nextBytes(bytes);

    // Create two secure number generators with the same seed
    int seedByteCount = 10;
    byte[] seed = sr.generateSeed(seedByteCount);

    sr = SecureRandom.getInstance(algorithm, provider);
    sr.setSeed(seed);

    SecureRandom sr2 = SecureRandom.getInstance(algorithm, provider);
    sr2.setSeed(seed);
    return sr2;
}

From source file:de.rrze.idmone.utils.jpwgen.RandomFactory.java

/**
 * Create a two pseudo random generator by utilizing the
 * <em>SecureRandom</em> class provided by SUN. Uses a two step procedure
 * for feeding the generator seed with two separate SecureRandom instances.
 * //from www  .  java2 s .  c o m
 * @see http 
 *      ://java.sun.com/j2se/1.4.2/docs/api/java/security/SecureRandom.html
 * 
 * @param algorithm
 *            The algorithm used for creating the pseudo random generator
 * @param provider
 *            the provider identifier
 * @return a seeded <em>SecureRandom</em>
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 */
private SecureRandom initSecureRandom(String algorithm, String provider)
        throws NoSuchAlgorithmException, NoSuchProviderException {
    logger.debug("Initializing random with: " + algorithm + " : " + provider);
    if (provider == null)
        provider = PROVIDER_DEFAULT;

    // Create a secure random number generator
    SecureRandom sr = SecureRandom.getInstance(algorithm, provider);

    // Get 1024 random bits
    byte[] bytes = new byte[1024 / 8];
    sr.nextBytes(bytes);

    // Create two secure number generators with the same seed
    int seedByteCount = 10;
    byte[] seed = sr.generateSeed(seedByteCount);

    sr = SecureRandom.getInstance(algorithm, provider);
    sr.setSeed(seed);

    SecureRandom sr2 = SecureRandom.getInstance(algorithm, provider);
    sr2.setSeed(seed);
    return sr2;
}

From source file:org.exfio.weave.storage.StorageContext.java

public String generateWeaveID() {
    SecureRandom rnd = new SecureRandom();
    byte[] weaveID = rnd.generateSeed(9);
    return Base64.encodeToString(weaveID, Base64.NO_PADDING | Base64.NO_WRAP | Base64.URL_SAFE);
}

From source file:org.exfio.weave.account.exfiopeer.ExfioPeerV1.java

private String generateAuthCode(int chars) {
    SecureRandom rnd = new SecureRandom();
    Base32 b32codec = new Base32();
    int bytes = (int) Math.ceil((double) chars * 5 / 8);
    String authCode = b32codec.encodeToString(rnd.generateSeed(bytes));

    // Convert to uppercase, translate L and O to 8 and 9
    authCode = authCode.toUpperCase().replace('L', '8').replace('O', '9').replaceAll("=", "");

    //Return the specified number of chars only
    return authCode.substring(0, chars - 1);
}

From source file:org.jmangos.realm.network.packet.auth.client.CMD_AUTH_LOGON_CHALLENGE.java

@Override
protected void readImpl() throws BufferUnderflowException, RuntimeException {

    readC();
    if (readC() == WoWAuthResponse.WOW_SUCCESS.getMessageId()) {
        final SecureRandom random = new SecureRandom();
        MessageDigest sha = null;
        try {//from  ww w  .j  a v a 2  s. co m
            sha = MessageDigest.getInstance("SHA-1");
        } catch (final NoSuchAlgorithmException e) {
            e.printStackTrace();
            return;
        }
        final BigInteger k = new BigInteger("3");
        final byte[] Bb = readB(32);
        final BigInteger g = new BigInteger(readB(readC()));
        final byte[] Nb = readB(readC());
        final byte[] saltb = readB(32);
        /* byte[] unk3 = */readB(16);
        readC();
        ArrayUtils.reverse(Bb);
        final BigInteger B = new BigInteger(1, Bb);
        ArrayUtils.reverse(Bb);
        ArrayUtils.reverse(Nb);
        final BigInteger N = new BigInteger(1, Nb);
        ArrayUtils.reverse(Nb);
        final BigInteger a = new BigInteger(1, random.generateSeed(19));

        final byte[] passhash = sha.digest(this.config.AUTH_LOGIN.toUpperCase().concat(":")
                .concat(this.config.AUTH_PASSWORD.toUpperCase()).getBytes(Charset.forName("UTF-8")));
        sha.update(saltb);
        sha.update(passhash);

        final byte[] xhash = sha.digest();
        ArrayUtils.reverse(xhash);
        final BigInteger x = new BigInteger(1, xhash);
        logger.debug("x:" + x.toString(16).toUpperCase());
        final BigInteger v = g.modPow(x, N);
        logger.debug("v:" + v.toString(16).toUpperCase());
        final BigInteger A = g.modPow(a, N);
        logger.debug("A:" + A.toString(16).toUpperCase());
        logger.debug("B:" + B.toString(16).toUpperCase());
        this.ahash = A.toByteArray();
        ArrayUtils.reverse(this.ahash);
        sha.update(this.ahash);
        sha.update(Bb);
        final byte[] hashu = sha.digest();
        ArrayUtils.reverse(hashu);
        final BigInteger u = new BigInteger(1, hashu);
        logger.debug("u:" + u.toString(16).toUpperCase());
        final BigInteger S = (B.subtract(k.multiply(g.modPow(x, N)))).modPow(a.add(u.multiply(x)), N);

        final byte[] full_S = S.toByteArray();
        ArrayUtils.reverse(full_S);
        logger.debug("t:" + StringUtils.toHexString(full_S));
        final byte[] s1_hash = new byte[16];
        final byte[] s2_hash = new byte[16];
        for (int i = 0; i < 16; i++) {
            s1_hash[i] = full_S[i * 2];
            s2_hash[i] = full_S[(i * 2) + 1];
        }
        final byte[] t1 = sha.digest(s1_hash);
        final byte[] t2 = sha.digest(s2_hash);
        final byte[] vK = new byte[40];
        for (int i = 0; i < 20; i++) {
            vK[i * 2] = t1[i];
            vK[(i * 2) + 1] = t2[i];
        }

        byte[] hash = new byte[20];
        logger.debug("N:" + N.toString(16).toUpperCase());
        hash = sha.digest(Nb);

        logger.debug("hash:" + new BigInteger(1, hash).toString(16).toUpperCase());

        byte[] gH = new byte[20];
        sha.update(g.toByteArray());
        gH = sha.digest();
        for (int i = 0; i < 20; ++i) {
            hash[i] ^= gH[i];
        }

        byte[] t4 = new byte[20];
        t4 = sha.digest(this.config.AUTH_LOGIN.toUpperCase().getBytes(Charset.forName("UTF-8")));

        sha.update(hash);
        logger.debug("hash:" + StringUtils.toHexString(hash));
        sha.update(t4);
        logger.debug("t4:" + StringUtils.toHexString(t4));
        sha.update(saltb);
        logger.debug("saltb:" + StringUtils.toHexString(saltb));
        sha.update(this.ahash);
        logger.debug("ahash:" + StringUtils.toHexString(this.ahash));
        sha.update(Bb);
        logger.debug("Bb:" + StringUtils.toHexString(Bb));
        sha.update(vK);
        logger.debug("vK:" + StringUtils.toHexString(vK));
        this.m1 = sha.digest();

        sha.update(this.ahash);
        sha.update(this.m1);
        sha.update(vK);
        logger.debug("m1 value" + StringUtils.toHexString(this.m1));
        @SuppressWarnings("unused")
        final byte[] m2 = sha.digest();

        final ChannelPipeline pipeline = getClient().getChannel().getPipeline();
        ((RealmToAuthChannelHandler) pipeline.getLast()).setSeed(vK);

    } else {
        getChannel().getPipeline().remove("handler");
        getChannel().getPipeline().remove("eventlog");
        getChannel().getPipeline().remove("executor");
        getChannel().close();
        getChannel().getFactory().releaseExternalResources();
    }
}