Example usage for java.security SecureRandom nextBytes

List of usage examples for java.security SecureRandom nextBytes

Introduction

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

Prototype

@Override
public void nextBytes(byte[] bytes) 

Source Link

Document

Generates a user-specified number of random bytes.

Usage

From source file:jenkins.model.Jenkins.java

/**
 * @param pluginManager//from w w w.ja  v  a 2 s . c  o m
 *      If non-null, use existing plugin manager.  create a new one.
 */
@edu.umd.cs.findbugs.annotations.SuppressWarnings({ "SC_START_IN_CTOR", // bug in FindBugs. It flags UDPBroadcastThread.start() call but that's for another class
        "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD" // Trigger.timer
})
protected Jenkins(File root, ServletContext context, PluginManager pluginManager)
        throws IOException, InterruptedException, ReactorException {
    long start = System.currentTimeMillis();

    // As Jenkins is starting, grant this process full control
    ACL.impersonate(ACL.SYSTEM);
    try {
        this.root = root;
        this.servletContext = context;
        computeVersion(context);
        if (theInstance != null)
            throw new IllegalStateException("second instance");
        theInstance = this;

        if (!new File(root, "jobs").exists()) {
            // if this is a fresh install, use more modern default layout that's consistent with slaves
            workspaceDir = "${JENKINS_HOME}/workspace/${ITEM_FULLNAME}";
        }

        // doing this early allows InitStrategy to set environment upfront
        final InitStrategy is = InitStrategy.get(Thread.currentThread().getContextClassLoader());

        Trigger.timer = new java.util.Timer("Jenkins cron thread");
        queue = new Queue(LoadBalancer.CONSISTENT_HASH);

        try {
            dependencyGraph = DependencyGraph.EMPTY;
        } catch (InternalError e) {
            if (e.getMessage().contains("window server")) {
                throw new Error(
                        "Looks like the server runs without X. Please specify -Djava.awt.headless=true as JVM option",
                        e);
            }
            throw e;
        }

        // get or create the secret
        TextFile secretFile = new TextFile(new File(getRootDir(), "secret.key"));
        if (secretFile.exists()) {
            secretKey = secretFile.readTrim();
        } else {
            SecureRandom sr = new SecureRandom();
            byte[] random = new byte[32];
            sr.nextBytes(random);
            secretKey = Util.toHexString(random);
            secretFile.write(secretKey);

            // this marker indicates that the secret.key is generated by the version of Jenkins post SECURITY-49.
            // this indicates that there's no need to rewrite secrets on disk
            new FileBoolean(new File(root, "secret.key.not-so-secret")).on();
        }

        try {
            proxy = ProxyConfiguration.load();
        } catch (IOException e) {
            LOGGER.log(SEVERE, "Failed to load proxy configuration", e);
        }

        if (pluginManager == null)
            pluginManager = new LocalPluginManager(this);
        this.pluginManager = pluginManager;
        // JSON binding needs to be able to see all the classes from all the plugins
        WebApp.get(servletContext).setClassLoader(pluginManager.uberClassLoader);

        adjuncts = new AdjunctManager(servletContext, pluginManager.uberClassLoader, "adjuncts/" + SESSION_HASH,
                TimeUnit2.DAYS.toMillis(365));

        // initialization consists of ...
        executeReactor(is, pluginManager.initTasks(is), // loading and preparing plugins
                loadTasks(), // load jobs
                InitMilestone.ordering() // forced ordering among key milestones
        );

        if (KILL_AFTER_LOAD)
            System.exit(0);

        launchTcpSlaveAgentListener();

        if (UDPBroadcastThread.PORT != -1) {
            try {
                udpBroadcastThread = new UDPBroadcastThread(this);
                udpBroadcastThread.start();
            } catch (IOException e) {
                LOGGER.log(Level.WARNING, "Failed to broadcast over UDP (use -Dhudson.udp=-1 to disable)", e);
            }
        }
        dnsMultiCast = new DNSMultiCast(this);

        Timer.get().scheduleAtFixedRate(new SafeTimerTask() {
            @Override
            protected void doRun() throws Exception {
                trimLabels();
            }
        }, TimeUnit2.MINUTES.toMillis(5), TimeUnit2.MINUTES.toMillis(5), TimeUnit.MILLISECONDS);

        updateComputerList();

        {// master is online now
            Computer c = toComputer();
            if (c != null)
                for (ComputerListener cl : ComputerListener.all())
                    cl.onOnline(c, new LogTaskListener(LOGGER, INFO));
        }

        for (ItemListener l : ItemListener.all()) {
            long itemListenerStart = System.currentTimeMillis();
            try {
                l.onLoaded();
            } catch (RuntimeException x) {
                LOGGER.log(Level.WARNING, null, x);
            }
            if (LOG_STARTUP_PERFORMANCE)
                LOGGER.info(String.format("Took %dms for item listener %s startup",
                        System.currentTimeMillis() - itemListenerStart, l.getClass().getName()));
        }

        // All plugins are loaded. Now we can figure out who depends on who.
        resolveDependantPlugins();

        if (LOG_STARTUP_PERFORMANCE)
            LOGGER.info(String.format("Took %dms for complete Jenkins startup",
                    System.currentTimeMillis() - start));
    } finally {
        SecurityContextHolder.clearContext();
    }
}

From source file:com.truledger.client.Client.java

/**
 * Generate a new sessionID/*from  www.j av a  2 s.  co m*/
 * @return a 40-digit random hex string
 */
public static String newSessionid() {
    SecureRandom random = Crypto.getRandom();
    byte[] bytes = new byte[20];
    random.nextBytes(bytes);
    return Utility.bin2hex(bytes);
}

From source file:com.zimbra.cs.account.ldap.LdapProvisioning.java

private Account createDummyAccount(Map<String, Object> attrs, String password, Domain domain)
        throws ServiceException {
    SecureRandom random = new SecureRandom();
    byte[] keyBytes = new byte[10];
    random.nextBytes(keyBytes);
    String dummyEmailAddr = String.valueOf(Hex.encodeHex(keyBytes)) + "@" + domain.getName();
    return createAccount(dummyEmailAddr, password, attrs);
}