001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.config.mod;
014
015import static org.apache.juneau.common.internal.IOUtils.*;
016import static org.apache.juneau.common.internal.StringUtils.*;
017
018/**
019 * Simply XOR+Base64 encoder for obscuring passwords and other sensitive data in INI config files.
020 *
021 * <p>
022 * This is not intended to be used as strong encryption.
023 *
024 * <h5 class='section'>See Also:</h5><ul>
025 *    <li class='link'><a class="doclink" href="../../../../../index.html#jc.ModdedEntries">Overview &gt; juneau-config &gt; Modded/Encoded Entries</a>
026 * </ul>
027 */
028public class XorEncodeMod extends Mod {
029
030   /** Reusable XOR-ConfigEncoder instance. */
031   public static final XorEncodeMod INSTANCE = new XorEncodeMod();
032
033   private static final String KEY = System.getProperty("org.apache.juneau.config.XorEncoder.key",
034      "nuy7og796Vh6G9O6bG230SHK0cc8QYkH");   // The super-duper-secret key
035
036   /**
037    * Constructor.
038    */
039   public XorEncodeMod() {
040      super('*', null, null, null);
041   }
042
043   @Override
044   public String apply(String value) {
045      byte[] b = value.getBytes(UTF8);
046      for (int i = 0; i < b.length; i++) {
047            int j = i % KEY.length();
048         b[i] = (byte)(b[i] ^ KEY.charAt(j));
049      }
050      return "{" + base64Encode(b) + "}";
051   }
052
053   @Override
054   public String remove(String value) {
055      value = value.trim();
056      value = value.substring(1, value.length()-1);
057      byte[] b = base64Decode(value);
058      for (int i = 0; i < b.length; i++) {
059         int j = i % KEY.length();
060         b[i] = (byte)(b[i] ^ KEY.charAt(j));
061      }
062      return new String(b, UTF8);
063   }
064
065   @Override
066   public boolean isApplied(String value) {
067      return startsWith(value, '{') && endsWith(value, '}');
068   }
069}