本文整理汇总了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);
}
示例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);
}
示例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);
}