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.hex;
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.DecoderException;
027import org.apache.commons.codec.binary.Hex;
028
029/**
030 * Codec that uses hex encoding/decoding.
031 * @author Paul Ferraro
032 */
033public class HexCodecFactory extends AbstractCodec
034{
035        private static final long serialVersionUID = 5273729775503057299L;
036
037        @Override
038        public String getId()
039        {
040                return "16";
041        }
042
043        /**
044         * {@inheritDoc}
045         * @see net.sf.hajdbc.codec.Codec#decode(java.lang.String)
046         */
047        @Override
048        public String decode(String value) throws SQLException
049        {
050                try
051                {
052                        return new String(Hex.decodeHex(value.toCharArray()));
053                }
054                catch (DecoderException e)
055                {
056                        throw new SQLException(e);
057                }
058        }
059
060        /**
061         * {@inheritDoc}
062         * @see net.sf.hajdbc.codec.Codec#encode(java.lang.String)
063         */
064        @Override
065        public String encode(String value)
066        {
067                return new String(Hex.encodeHex(value.getBytes()));
068        }
069        
070        public static void main(String... args)
071        {
072                if (args.length != 2)
073                {
074                        System.err.println(String.format("Usage:%s\tjava %s <cluster-id> <password-to-encrypt>", Strings.NEW_LINE, HexCodecFactory.class.getName()));
075                        System.exit(1);
076                        return;
077                }
078                
079                String clusterId = args[0];
080                String value = args[1];
081                
082                try
083                {
084                        Codec codec = new HexCodecFactory().createCodec(clusterId);
085
086                        System.out.println(codec.encode(value));
087                }
088                catch (SQLException e)
089                {
090                        e.printStackTrace(System.err);
091                }
092        }
093}