当前位置: 首页>>代码示例>>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;未经允许,请勿转载。