Example usage for org.apache.commons.crypto.random CryptoRandomFactory CLASSES_KEY

List of usage examples for org.apache.commons.crypto.random CryptoRandomFactory CLASSES_KEY

Introduction

In this page you can find the example usage for org.apache.commons.crypto.random CryptoRandomFactory CLASSES_KEY.

Prototype

String CLASSES_KEY

To view the source code for org.apache.commons.crypto.random CryptoRandomFactory CLASSES_KEY.

Click Source Link

Document

The configuration key of the CryptoRandom implementation class.

Usage

From source file:org.apache.commons.crypto.examples.RandomExample.java

public static void main(String[] args) throws GeneralSecurityException, IOException {
    // Constructs a byte array to store random data.
    byte[] key = new byte[16];
    byte[] iv = new byte[32];

    Properties properties = new Properties();
    properties.put(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.OPENSSL.getClassName());

    // Gets the 'CryptoRandom' instance.
    try (CryptoRandom random = CryptoRandomFactory.getCryptoRandom(properties)) {

        // Show the actual class (may be different from the one requested)
        System.out.println(random.getClass().getCanonicalName());

        // Generate random bytes and places them into the byte arrays.
        random.nextBytes(key);/*from  w  w w  .jav  a2 s.  c o m*/
        random.nextBytes(iv);

    }

    // Show the generated output
    System.out.println(Arrays.toString(key));
    System.out.println(Arrays.toString(iv));
}

From source file:org.apache.hadoop.hbase.ipc.ServerRpcConnection.java

/**
 * Set up cipher for rpc encryption with Apache Commons Crypto
 *
 * @throws FatalConnectionException/*from   w w  w  .  j  a va  2  s  .  com*/
 */
private void setupCryptoCipher(final ConnectionHeader header,
        RPCProtos.ConnectionHeaderResponse.Builder chrBuilder) throws FatalConnectionException {
    // If simple auth, return
    if (saslServer == null)
        return;
    // check if rpc encryption with Crypto AES
    String qop = saslServer.getNegotiatedQop();
    boolean isEncryption = SaslUtil.QualityOfProtection.PRIVACY.getSaslQop().equalsIgnoreCase(qop);
    boolean isCryptoAesEncryption = isEncryption
            && this.rpcServer.conf.getBoolean("hbase.rpc.crypto.encryption.aes.enabled", false);
    if (!isCryptoAesEncryption)
        return;
    if (!header.hasRpcCryptoCipherTransformation())
        return;
    String transformation = header.getRpcCryptoCipherTransformation();
    if (transformation == null || transformation.length() == 0)
        return;
    // Negotiates AES based on complete saslServer.
    // The Crypto metadata need to be encrypted and send to client.
    Properties properties = new Properties();
    // the property for SecureRandomFactory
    properties.setProperty(CryptoRandomFactory.CLASSES_KEY,
            this.rpcServer.conf.get("hbase.crypto.sasl.encryption.aes.crypto.random",
                    "org.apache.commons.crypto.random.JavaCryptoRandom"));
    // the property for cipher class
    properties.setProperty(CryptoCipherFactory.CLASSES_KEY, this.rpcServer.conf
            .get("hbase.rpc.crypto.encryption.aes.cipher.class", "org.apache.commons.crypto.cipher.JceCipher"));

    int cipherKeyBits = this.rpcServer.conf.getInt("hbase.rpc.crypto.encryption.aes.cipher.keySizeBits", 128);
    // generate key and iv
    if (cipherKeyBits % 8 != 0) {
        throw new IllegalArgumentException("The AES cipher key size in bits" + " should be a multiple of byte");
    }
    int len = cipherKeyBits / 8;
    byte[] inKey = new byte[len];
    byte[] outKey = new byte[len];
    byte[] inIv = new byte[len];
    byte[] outIv = new byte[len];

    try {
        // generate the cipher meta data with SecureRandom
        CryptoRandom secureRandom = CryptoRandomFactory.getCryptoRandom(properties);
        secureRandom.nextBytes(inKey);
        secureRandom.nextBytes(outKey);
        secureRandom.nextBytes(inIv);
        secureRandom.nextBytes(outIv);

        // create CryptoAES for server
        cryptoAES = new CryptoAES(transformation, properties, inKey, outKey, inIv, outIv);
        // create SaslCipherMeta and send to client,
        //  for client, the [inKey, outKey], [inIv, outIv] should be reversed
        RPCProtos.CryptoCipherMeta.Builder ccmBuilder = RPCProtos.CryptoCipherMeta.newBuilder();
        ccmBuilder.setTransformation(transformation);
        ccmBuilder.setInIv(getByteString(outIv));
        ccmBuilder.setInKey(getByteString(outKey));
        ccmBuilder.setOutIv(getByteString(inIv));
        ccmBuilder.setOutKey(getByteString(inKey));
        chrBuilder.setCryptoCipherMeta(ccmBuilder);
        useCryptoAesWrap = true;
    } catch (GeneralSecurityException | IOException ex) {
        throw new UnsupportedCryptoException(ex.getMessage(), ex);
    }
}