org.apache.hadoop.io.crypto.tool.CryptoApiTool.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.hadoop.io.crypto.tool.CryptoApiTool.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.io.crypto.tool;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.math.BigInteger;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.compress.CompressionInputStream;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.io.crypto.CryptoCodec;
import org.apache.hadoop.io.crypto.CryptoContext;
import org.apache.hadoop.io.crypto.Key;
import org.apache.hadoop.io.crypto.aes.AESCodec;

public class CryptoApiTool {
    private static final Log LOG = LogFactory.getLog(CryptoApiTool.class);

    public String strInput = "";
    public String strOutput = "";
    public String strHexKey = "";
    public String strDecKey = "";
    public boolean isEncrypt = true;

    public CryptoApiTool() {
    }

    public CryptoApiTool(String strHexKey, String strInput, String strOutput) {
        this.strHexKey = strHexKey;
        this.strInput = strInput;
        this.strOutput = strOutput;
    }

    private void writeStream(DataInputStream input, CompressionOutputStream output) throws IOException {
        int read = 0;
        byte[] buffer = new byte[128 * 1024];

        // while (0 < (read = input.read(buffer, 0, 64 * 1024))) {
        while (0 < (read = input.read(buffer, 0, 67 * 1024))) {
            output.write(buffer, 0, read);
        }
        output.flush();

    }

    private void writeStreamLine(DataInputStream input, CompressionOutputStream output) throws IOException {
        String line;
        while ((line = input.readLine()) != null) {
            output.write(line.getBytes("UTF-8"));
            output.write("\n".getBytes("UTF-8"));
        }

        output.flush();
    }

    private void writeStream(CompressionInputStream input, DataOutputStream output) throws IOException {
        int read = 0;
        byte[] buffer = new byte[64 * 1024];
        // while (0 < (read = input.read(buffer, 0, 64 * 1024))) {
        while (0 < (read = input.read(buffer, 0, 12 * 1024))) {
            output.write(buffer, 0, read);
        }
        output.flush();

    }

    private Key initKey(String sHexKey) throws Exception {
        if (sHexKey == null)
            throw new Exception("Fail to get key(cypto.secret.key) from Configuration");

        byte[] rawKey = Hex.decodeHex(sHexKey.toCharArray());
        int cryptographicLength = 256;
        if (rawKey.length == 16)
            cryptographicLength = 128;
        else if (rawKey.length == 32)
            cryptographicLength = 256;
        else
            throw new Exception(
                    "Invalid key length(only support 16(128)/32(256) bytes): rawKey.length=" + rawKey.length);
        // Key key = new Key (keyType, algorithm, 0, format, rawKey);
        return new Key(Key.KeyType.SYMMETRIC_KEY, "AES", cryptographicLength, "RAW", rawKey);

    }

    public void apiEncryption() {
        try {
            Key key = initKey(strHexKey);

            CryptoContext cryptoContext = new CryptoContext();
            cryptoContext.setKey(key);

            CryptoCodec cryptoCodec = (CryptoCodec) new AESCodec();
            cryptoCodec.setCryptoContext(cryptoContext);

            Path inputFile = new Path(strInput);
            Path outputFile = new Path(strOutput);
            Configuration conf = new Configuration();
            DataInputStream input = inputFile.getFileSystem(conf).open(inputFile);
            DataOutputStream outputStream = outputFile.getFileSystem(conf).create(outputFile);
            CompressionOutputStream output = cryptoCodec.createOutputStream(outputStream);
            writeStream(input, output);
            input.close();
            output.close();

        } catch (Exception e) {
            LOG.error("Encryption Fail:", e);
            throw new RuntimeException("Encryption Fail.");
        }
    }

    public void apiDecryption() {

        try {
            Key key = initKey(strHexKey);

            CryptoContext cryptoContext = new CryptoContext();
            cryptoContext.setKey(key);

            CryptoCodec cryptoCodec = (CryptoCodec) new AESCodec();
            cryptoCodec.setCryptoContext(cryptoContext);

            Configuration conf = new Configuration();
            Path inputFile = new Path(strInput);
            DataInputStream inputStream = inputFile.getFileSystem(conf).open(inputFile);
            CompressionInputStream input = cryptoCodec.createInputStream(inputStream);

            Path outputFile = new Path(strOutput);
            DataOutputStream output = outputFile.getFileSystem(conf).create(outputFile);

            writeStream(input, output);
            input.close();
            output.close();
        } catch (Exception e) {
            LOG.error("Decryption Fail:", e);
            throw new RuntimeException("Decryption Fail.");
        }
    }

    public void setupOptions(String[] args) throws ParseException {
        Options options = new Options();
        options.addOption("input", true, "e.g. /tmp/install.log");
        options.addOption("output", true, "e.g. /tmp/wordCount");
        options.addOption("hexKey", true, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        options.addOption("decKey", true, "xxxxx");
        options.addOption("isEncrypt", true, "true, false");
        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options, args);

        HelpFormatter formatter = new HelpFormatter();
        if (cmd.hasOption("input"))
            strInput = cmd.getOptionValue("input");

        if (cmd.hasOption("output"))
            strOutput = cmd.getOptionValue("output");

        if (cmd.hasOption("hexKey"))
            strHexKey = cmd.getOptionValue("hexKey");

        if (cmd.hasOption("decKey")) {
            strDecKey = cmd.getOptionValue("decKey");
            if (!strHexKey.isEmpty()) {
                System.out.println("Parameter Error!!! Only accept one of hexKey or DecKey");
                System.exit(1);
            }
            strHexKey = new BigInteger(strDecKey).toString(16);
        }

        if (cmd.hasOption("isEncrypt"))
            if (cmd.getOptionValue("isEncrypt").compareToIgnoreCase("false") == 0)
                isEncrypt = false;

        if (strInput.isEmpty() || strOutput.isEmpty()) {
            System.out.println("Parameter Error!!! Parameter input & output cannot be null");
            formatter.printHelp(" ", options);
            System.exit(1);
        }

        System.out.println("input: " + strInput + ", output:" + strOutput);
    }

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        /*
         * hadoop jar /root/myorg.jar org.myorg.CryptoApiTool -input
         * file:///root/install.log -output file:///tmp/install.log.encrypted
         * -hexKey xxxxxx -decKey
         * 34599082064196559951068791877453657754940655831264375231727108186005759618471
         * -input /tmp/install.log -output /tmp/install.log.intel_aes -isEncrypt
         * false -hexKey
         * 4c7e62f001ae63fd7567f9c9bd5ca1e161396726bea70fe843fec0f771546da7 -input
         * /tmp/install.log.intel_aes -output /tmp/install.log.decrypt
         */
        CryptoApiTool apiTool = new CryptoApiTool();
        apiTool.setupOptions(args);
        System.out.println("isEncrypt: " + String.valueOf(apiTool.isEncrypt) + ", hexKey: " + apiTool.strHexKey
                + ",decKey:" + apiTool.strDecKey);

        if (apiTool.isEncrypt)
            apiTool.apiEncryption();
        else
            apiTool.apiDecryption();
    }
}