001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util.encrypt; 017 018import static com.google.common.base.Optional.absent; 019import static com.google.common.base.Preconditions.checkState; 020import static org.apache.commons.io.FileUtils.readFileToString; 021import static org.kuali.common.util.base.Exceptions.illegalState; 022import static org.kuali.common.util.encrypt.Encryption.getDefaultEncryptor; 023 024import java.io.File; 025import java.io.IOException; 026import java.util.List; 027 028import org.kuali.common.util.file.CanonicalFile; 029 030import com.google.common.base.Optional; 031import com.google.common.base.Splitter; 032 033public final class EncryptionMain { 034 035 private static final String FS = File.separator; 036 private static final String USER_HOME_TOKEN = "~" + FS; 037 private static final String USER_HOME_REPLACEMENT = System.getProperty("user.home") + FS; 038 039 public static void main(String[] args) { 040 if (args == null || args.length == 0) { 041 usage(); 042 System.exit(1); 043 } 044 boolean encrypt = matches(args, "-e", "--encrypt"); 045 boolean decrypt = matches(args, "-d", "--decrypt"); 046 if (!encrypt && !decrypt) { 047 usage(); 048 System.exit(1); 049 } 050 String text = getText(args); 051 if (encrypt) { 052 System.out.println(getDefaultEncryptor().encrypt(text)); 053 } 054 if (decrypt) { 055 System.out.println(getDefaultEncryptor().decrypt(text)); 056 } 057 } 058 059 private static String getText(String[] args) { 060 Optional<CanonicalFile> file = getFile(args); 061 if (file.isPresent()) { 062 try { 063 return readFileToString(file.get()); 064 } catch (IOException e) { 065 throw illegalState(e); 066 } 067 } else { 068 return args[args.length - 1]; 069 } 070 } 071 072 private static Optional<CanonicalFile> getFile(String[] args) { 073 for (String arg : args) { 074 if (arg.startsWith("--file")) { 075 List<String> tokens = Splitter.on('=').splitToList(arg); 076 checkState(tokens.size() == 2, "expected 2 tokens from [%s], but got %s instead", arg, tokens.size()); 077 String filename = tokens.get(1).replace(USER_HOME_TOKEN, USER_HOME_REPLACEMENT); 078 return Optional.of(new CanonicalFile(filename)); 079 } 080 } 081 return absent(); 082 } 083 084 private static boolean matches(String[] args, String option, String longoption) { 085 for (String arg : args) { 086 if (arg.equals(option) || arg.equals(longoption)) { 087 return true; 088 } 089 } 090 return false; 091 } 092 093 private static void usage() { 094 System.out.println("Usage: EncryptionMain --encrypt --decrypt [--file=filename] text"); 095 } 096}