001/* 002 * HA-JDBC: High-Availability JDBC 003 * Copyright (C) 2012 Paul Ferraro 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Lesser General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package net.sf.hajdbc.codec.base64; 019 020import java.sql.SQLException; 021 022import net.sf.hajdbc.codec.AbstractCodec; 023import net.sf.hajdbc.codec.Codec; 024import net.sf.hajdbc.util.Strings; 025 026import org.apache.commons.codec.binary.Base64; 027 028/** 029 * Codec that uses base-64 encoding/decoding. 030 * @author Paul Ferraro 031 */ 032public class Base64CodecFactory extends AbstractCodec 033{ 034 private static final long serialVersionUID = -2286529406290006597L; 035 036 @Override 037 public String getId() 038 { 039 return "64"; 040 } 041 042 /** 043 * {@inheritDoc} 044 * @see net.sf.hajdbc.codec.Codec#decode(java.lang.String) 045 */ 046 @Override 047 public String decode(String value) 048 { 049 return new String(Base64.decodeBase64(value.getBytes())); 050 } 051 052 /** 053 * {@inheritDoc} 054 * @see net.sf.hajdbc.codec.Codec#encode(java.lang.String) 055 */ 056 @Override 057 public String encode(String value) 058 { 059 return new String(Base64.encodeBase64(value.getBytes())); 060 } 061 062 public static void main(String... args) 063 { 064 if (args.length != 2) 065 { 066 System.err.println(String.format("Usage:%s\tjava %s <cluster-id> <password-to-encrypt>", Strings.NEW_LINE, Base64CodecFactory.class.getName())); 067 System.exit(1); 068 return; 069 } 070 071 String clusterId = args[0]; 072 String value = args[1]; 073 074 try 075 { 076 Codec codec = new Base64CodecFactory().createCodec(clusterId); 077 078 System.out.println(codec.encode(value)); 079 } 080 catch (SQLException e) 081 { 082 e.printStackTrace(System.err); 083 } 084 } 085}