Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * The MIT License
   * 
   * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
   * 
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  * 
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
  * 
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
 package hudson.util;
 
 import  hudson.model.Hudson;
 import  hudson.Util;
 
Glorified String that uses encryption in the persisted form, to avoid accidental exposure of a secret.

Note that since the cryptography relies on Hudson.getSecretKey(), this is not meant as a protection against code running in the same VM, nor against an attacker who has local file system access.

Author(s):
Kohsuke Kawaguchi
 
 public final class Secret {
    
Unencrypted secret text.
 
     private final String value;
 
     private Secret(String value) {
         this. = value;
     }

    
Obtains the secret in a plain text.

See also:
getEncryptedValue()
 
     public String toString() {
         return ;
     }
 
     public boolean equals(Object that) {
         return that instanceof Secret && .equals(((Secret)that).);
     }
 
     public int hashCode() {
         return .hashCode();
     }

    
Turns Hudson.getSecretKey() into an AES key.
 
     private static SecretKey getKey() throws UnsupportedEncodingExceptionGeneralSecurityException {
         String secret = ;
         if(secret==null)    return Hudson.getInstance().getSecretKeyAsAES128();
         return Util.toAes128Key(secret);
     }

    
Encrypts value and returns it in an encoded printable form.

See also:
toString()
 
     public String getEncryptedValue() {
         try {
             Cipher cipher = Cipher.getInstance("AES");
             cipher.init(.getKey());
             // add the magic suffix which works like a check sum.
             return new String(Base64.encode(cipher.doFinal((+).getBytes("UTF-8"))));
         } catch (GeneralSecurityException e) {
             throw new Error(e); // impossible
         } catch (UnsupportedEncodingException e) {
            throw new Error(e); // impossible
        }
    }

    
Reverse operation of getEncryptedValue(). Returns null if the given cipher text was invalid.
    public static Secret decrypt(String data) {
        if(data==null)      return null;
        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(.getKey());
            String plainText = new String(cipher.doFinal(Base64.decode(data.toCharArray())), "UTF-8");
            if(plainText.endsWith())
                return new Secret(plainText.substring(0,plainText.length()-.length()));
            return null;
        } catch (GeneralSecurityException e) {
            return null;
        } catch (UnsupportedEncodingException e) {
            throw new Error(e); // impossible
        } catch (IOException e) {
            return null;
        }
    }

    
Attempts to treat the given string first as a cipher text, and if it doesn't work, treat the given string as the unencrypted secret value.

Useful for recovering a value from a form field.

Returns:
never null
    public static Secret fromString(String data) {
        Secret s = decrypt(data);
        if(s==nulls=new Secret(data);
        return s;
    }
    public static final class ConverterImpl implements Converter {
        public ConverterImpl() {
        }
        public boolean canConvert(Class type) {
            return type==Secret.class;
        }
        public void marshal(Object sourceHierarchicalStreamWriter writerMarshallingContext context) {
            Secret src = (Secretsource;
            writer.setValue(src.getEncryptedValue());
        }
        public Object unmarshal(HierarchicalStreamReader readerfinal UnmarshallingContext context) {
            return Secret.decrypt(reader.getValue());
        }
    }
    private static final String MAGIC = "::::MAGIC::::";

    
For testing only. Override the secret key so that we can test this class without Hudson.
    /*package*/ static String SECRET = null;
New to GrepCode? Check out our FAQ X