當前位置: 首頁>>代碼示例>>Java>>正文


Java RandomUtils類代碼示例

本文整理匯總了Java中craterdog.utils.RandomUtils的典型用法代碼示例。如果您正苦於以下問題:Java RandomUtils類的具體用法?Java RandomUtils怎麽用?Java RandomUtils使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


RandomUtils類屬於craterdog.utils包,在下文中一共展示了RandomUtils類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: generateSharedKey

import craterdog.utils.RandomUtils; //導入依賴的package包/類
@Override
public SecretKey generateSharedKey() {
    try {
        logger.entry();
        KeyGenerator keyGenerator = KeyGenerator.getInstance(SYMMETRIC_KEY_TYPE);
        keyGenerator.init(SYMMETRIC_KEY_SIZE, RandomUtils.generator);
        SecretKey sharedKey = keyGenerator.generateKey();
        logger.exit();
        return sharedKey;

    } catch (GeneralSecurityException e) {
        String message = "An exception occured while trying to generate a shared key.";
        RuntimeException exception = new RuntimeException(message, e);
        logger.error(message, exception);
        throw exception;

    }
}
 
開發者ID:craterdog,項目名稱:java-security-framework,代碼行數:19,代碼來源:RsaAesMessageCryptex.java

示例2: generateKeyPair

import craterdog.utils.RandomUtils; //導入依賴的package包/類
@Override
public KeyPair generateKeyPair() {
    try {
        logger.entry();
        KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(ASYMMETRIC_KEY_TYPE);
        keyGenerator.initialize(ASYMMETRIC_KEY_SIZE, RandomUtils.generator);
        KeyPair keyPair = keyGenerator.generateKeyPair();
        logger.exit();
        return keyPair;

    } catch (GeneralSecurityException e) {
        String message = "An unexpected exception occurred while attempting to generate a new key pair.";
        RuntimeException exception = new RuntimeException(message, e);
        logger.error(message, exception);
        throw exception;

    }
}
 
開發者ID:craterdog,項目名稱:java-security-framework,代碼行數:19,代碼來源:RsaAesMessageCryptex.java

示例3: Tag

import craterdog.utils.RandomUtils; //導入依賴的package包/類
/**
 * This default constructor creates an instance of a tag with a new random value.
 */
public Tag() {
    tagSize = DEFAULT_TAG_SIZE;
    byte[] bytes = RandomUtils.generateRandomBytes(tagSize);
    hash = ByteUtils.bytesToInt(bytes);
    string = Base32Utils.encode(bytes);
}
 
開發者ID:craterdog,項目名稱:java-primitive-types,代碼行數:10,代碼來源:Tag.java

示例4: randomizeCollection

import craterdog.utils.RandomUtils; //導入依賴的package包/類
private void randomizeCollection(List<E> indexedCollection, int size) {
    for (int index = size; index > 1; index--) {
        int randomIndex = RandomUtils.pickRandomIndex(index) + 1;  // use ordinal based indexing
        E swap = indexedCollection.getElement(index);
        swap = indexedCollection.replaceElement(swap, randomIndex);
        indexedCollection.replaceElement(swap, index);
    }
}
 
開發者ID:craterdog,項目名稱:java-collection-framework,代碼行數:9,代碼來源:RandomSorter.java

示例5: randomizeArray

import craterdog.utils.RandomUtils; //導入依賴的package包/類
private void randomizeArray(E[] array) {
    int size = array.length;
    for (int index = size; index > 1; index--) {
        int randomIndex = RandomUtils.pickRandomIndex(index);  // use zero based indexing
        E swap = array[index - 1];
        array[index - 1] = array[randomIndex];
        array[randomIndex] = swap;
    }
}
 
開發者ID:craterdog,項目名稱:java-collection-framework,代碼行數:10,代碼來源:RandomSorter.java

示例6: main

import craterdog.utils.RandomUtils; //導入依賴的package包/類
/**
 * The main method for this application.  It expects the following arguments:
 * <ol>
 * <li>The name of the target environment (e.g. Sandbox, PreProd, Production, etc.).</li>
 * <li>The name of the client.</li>
 * <li>The path to the directory that contains the private certificate authorities and passwords.</li>
 * <li>The subject string containing the CN, O, OU, C, etc. values.</li>
 * </ol>
 *
 * @param args The arguments that were passed into this program.
 */
static public void main(String[] args) {
    String environment = args[0];
    String clientKeyStorePrefix = args[1] + "-" + environment;
    String caKeyStorePrefix = args[2] + File.separator + environment + "-CA";
    String subject = args[3];

    try (
            FileReader pwReader = new FileReader(caKeyStorePrefix + ".pw");
            FileInputStream caInput = new FileInputStream(caKeyStorePrefix + ".p12");
            FileWriter pwWriter = new FileWriter(clientKeyStorePrefix + ".pw");
            FileOutputStream clientOutput = new FileOutputStream(clientKeyStorePrefix + ".p12")
            ) {
        logger.info("Loading the private certificate authority keys...");
        int size = new Tag(16).toString().length();
        char[] caPassword = new char[size];
        pwReader.read(caPassword);
        MessageCryptex cryptex = new RsaAesMessageCryptex();
        RsaCertificateManager manager = new RsaCertificateManager();
        KeyStore caKeyStore = manager.retrieveKeyStore(caInput, caPassword);
        PrivateKey caPrivateKey = manager.retrievePrivateKey(caKeyStore, CA_ALIAS, caPassword);
        X509Certificate caCertificate = manager.retrieveCertificate(caKeyStore, CA_ALIAS);

        logger.info("Generating a new key pair for the client certificate...");
        KeyPair clientKeyPair = cryptex.generateKeyPair();
        PublicKey clientPublicKey = clientKeyPair.getPublic();
        PrivateKey clientPrivateKey = clientKeyPair.getPrivate();

        logger.info("Generating and signing a new client certificate...");
        long lifetime = 30L /*years*/ * 365L /*days*/ * 24L /*hours*/ * 60L /*minutes*/
                * 60L /*seconds*/ * 1000L /*milliseconds*/;
        BigInteger serialNumber = new BigInteger(RandomUtils.generateRandomBytes(16));
        X509Certificate clientCertificate = manager.createCertificate(caPrivateKey,
                caCertificate, clientPublicKey, subject, serialNumber, lifetime);
        clientCertificate.verify(caCertificate.getPublicKey());

        logger.info("Storing the new client certificate and private key in a key store...");
        char[] clientPassword = new Tag(16).toString().toCharArray();
        List<X509Certificate> certificates = new ArrayList<>();
        certificates.add(clientCertificate);
        certificates.add(caCertificate);
        KeyStore clientKeyStore = manager.createPkcs12KeyStore(CLIENT_ALIAS, clientPassword,
                clientPrivateKey, certificates);

        logger.info("Writing out the key store and password to files...");
        manager.saveKeyStore(clientOutput, clientKeyStore, clientPassword);
        pwWriter.write(clientPassword);

    } catch (CertificateException | NoSuchAlgorithmException | InvalidKeyException |
            NoSuchProviderException | SignatureException | IOException e) {
        logger.error("An error occurred while attempting to generate the client certificate:", e);
        System.exit(1);
    }
    System.exit(0);
}
 
開發者ID:craterdog,項目名稱:java-security-framework,代碼行數:66,代碼來源:ClientCertificateGenerator.java

示例7: main

import craterdog.utils.RandomUtils; //導入依賴的package包/類
/**
 * The main method for this application.  It expects the following arguments:
 * <ol>
 * <li>The name of the target environment (e.g. Sandbox, PreProd, Production, etc.).</li>
 * <li>The name of the client.</li>
 * <li>The path to the directory that contains the private certificate authorities and passwords.</li>
 * </ol>
 *
 * @param args The arguments that were passed into this program.
 */
static public void main(String[] args) {
    String environment = args[0];
    String clientCertificatePrefix = args[1] + "-" + environment;
    String caKeyStorePrefix = args[2] + File.separator + environment + "-CA";

    try (
            FileReader pwReader = new FileReader(caKeyStorePrefix + ".pw");
            FileInputStream caInput = new FileInputStream(caKeyStorePrefix + ".p12");
            PemReader csrReader = new PemReader(new FileReader(clientCertificatePrefix + ".csr"));
            PemWriter pemWriter = new PemWriter(new FileWriter(clientCertificatePrefix + ".pem"))
            ) {
        logger.info("Loading the private certificate authority keys...");
        int size = new Tag(16).toString().length();
        char[] caPassword = new char[size];
        pwReader.read(caPassword);
        RsaCertificateManager manager = new RsaCertificateManager();
        KeyStore caKeyStore = manager.retrieveKeyStore(caInput, caPassword);
        PrivateKey caPrivateKey = manager.retrievePrivateKey(caKeyStore, CA_ALIAS, caPassword);
        X509Certificate caCertificate = manager.retrieveCertificate(caKeyStore, CA_ALIAS);

        logger.info("Reading in the certificate signing request...");
        byte[] requestBytes = csrReader.readPemObject().getContent();
        PKCS10CertificationRequest csr = new PKCS10CertificationRequest(requestBytes);

        logger.info("Generating and signing a new client certificate...");
        long lifetime = 30L /*years*/ * 365L /*days*/ * 24L /*hours*/ * 60L /*minutes*/
                * 60L /*seconds*/ * 1000L /*milliseconds*/;
        BigInteger serialNumber = new BigInteger(RandomUtils.generateRandomBytes(16));
        X509Certificate clientCertificate = manager.signCertificateRequest(caPrivateKey, caCertificate, csr, serialNumber, lifetime);
        clientCertificate.verify(caCertificate.getPublicKey());

        logger.info("Writing out the certificates to a file...");
        pemWriter.writeObject(new PemObject("CERTIFICATE", clientCertificate.getEncoded()));
        pemWriter.writeObject(new PemObject("CERTIFICATE", caCertificate.getEncoded()));

    } catch (CertificateException | NoSuchAlgorithmException | InvalidKeyException |
            NoSuchProviderException | SignatureException | IOException e) {
        logger.error("An error occurred while attempting to generate the client certificate:", e);
        System.exit(1);
    }
    System.exit(0);
}
 
開發者ID:craterdog,項目名稱:java-security-framework,代碼行數:53,代碼來源:ClientCertificateSigner.java

示例8: Probability

import craterdog.utils.RandomUtils; //導入依賴的package包/類
/**
 * This constructor creates a new instance of a probability with a random value between [0.0..1.0).
 * Notice that the random value can never be 1.0.
 */
public Probability() {
    this.value = RandomUtils.pickRandomProbability();  // returns [0.0..1.0) so will never be 1.0
}
 
開發者ID:craterdog,項目名稱:java-primitive-types,代碼行數:8,代碼來源:Probability.java

示例9: coinToss

import craterdog.utils.RandomUtils; //導入依賴的package包/類
/**
 * This function returns the result of a coin toss that is weighted with the
 * specified probability.  A probability of zero will always return false and
 * a probability of one will always return true.
 *
 * @param probability The probability of the weighted coin.
 * @return The result of the coin toss.
 */
static public boolean coinToss(Probability probability) {
    double p = probability.value;
    double toss = RandomUtils.pickRandomProbability();  // returns [0.0..1.0) so will never be 1.0
    return p > toss;
}
 
開發者ID:craterdog,項目名稱:java-primitive-types,代碼行數:14,代碼來源:Probability.java


注:本文中的craterdog.utils.RandomUtils類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。