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


Java RandomUtils.generateRandomBytes方法代碼示例

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


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

示例1: 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

示例2: 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

示例3: 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


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