Merge "System dependent separators"
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / Symm.java
index 82645c3..afc1d97 100644 (file)
@@ -117,7 +117,8 @@ public class Symm {
        private static char passChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+!@#$%^&*(){}[]?:;,.".toCharArray();
                        
 
-
+       private static Symm internalOnly = null;
+       
        /**
         * Use this to create special case Case Sets and/or Line breaks
         * 
@@ -137,7 +138,7 @@ public class Symm {
                // There can be time efficiencies gained when the underlying keyset consists mainly of ordered 
                // data (i.e. abcde...).  Therefore, we'll quickly analyze the keyset.  If it proves to have
                // too much entropy, the "Unordered" algorithm, which is faster in such cases is used.
-               ArrayList<int[]> la = new ArrayList<int[]>();
+               ArrayList<int[]> la = new ArrayList<>();
                for(int i=0;i<codeset.length;++i) {
                        curr = codeset[i];
                        if(prev+1==curr) { // is next character in set
@@ -449,9 +450,11 @@ public class Symm {
                   this.range = range;
           }
           public int convert(int read) throws IOException {
+                  // System.out.print((char)read);
                   switch(read) {
                           case -1: 
                           case '=':
+                          case ' ':
                           case '\n':
                           case '\r':
                                   return -1;
@@ -480,7 +483,8 @@ public class Symm {
                   switch(read) {
                           case -1: 
                           case '=':
-                          case '\n': 
+                                case '\n':
+                                case '\r':
                                   return -1;
                   }
                   for(int i=0;i<codec.length;++i) {
@@ -537,10 +541,10 @@ public class Symm {
  * @throws CadiException 
     */
    public static Symm obtain(Access access) throws CadiException {
-               Symm symm = Symm.baseCrypt();
-
                String keyfile = access.getProperty(Config.CADI_KEYFILE,null);
                if(keyfile!=null) {
+                       Symm symm = Symm.baseCrypt();
+
                        File file = new File(keyfile);
                        try {
                                access.log(Level.INIT, Config.CADI_KEYFILE,"points to",file.getCanonicalPath());
@@ -570,8 +574,14 @@ public class Symm {
                                }
                                throw new CadiException("ERROR: " + filename + " does not exist!");
                        }
+                       return symm;
+               } else {
+                       try {
+                               return internalOnly();
+                       } catch (IOException e) {
+                               throw new CadiException(e);
+                       }
                }
-               return symm;
    }
   /**
    *  Create a new random key 
@@ -653,6 +663,9 @@ public class Symm {
    * @throws IOException
    */
   public void enpass(final String password, final OutputStream os) throws IOException {
+           if(password==null) {
+               throw new IOException("Invalid password passed");
+           }
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);
                byte[] bytes = password.getBytes();
@@ -855,4 +868,22 @@ public class Symm {
 
          return newSymm;
   }
+  
+  /** 
+   * This Symm is generated for internal JVM use.  It has no external keyfile, but can be used
+   * for securing Memory, as it remains the same ONLY of the current JVM
+   * @return
+ * @throws IOException 
+   */
+  public static synchronized Symm internalOnly() throws IOException {
+         if(internalOnly==null) {
+                 ByteArrayInputStream baos = new ByteArrayInputStream(keygen());
+                 try {
+                         internalOnly = Symm.obtain(baos);
+                 } finally {
+                         baos.close();
+                 }
+         }
+         return internalOnly;
+  }
 }