use base64 in the right way
authorAleš Křenek <ljocha@ics.muni.cz>
Thu, 10 Apr 2008 08:56:55 +0000 (08:56 +0000)
committerAleš Křenek <ljocha@ics.muni.cz>
Thu, 10 Apr 2008 08:56:55 +0000 (08:56 +0000)
org.glite.jobid.api-java/src/org/apache/commons/codec/binary/package.html
org.glite.jobid.api-java/src/org/glite/jobid/api_java/CheckedString.java
org.glite.jobid.api-java/src/org/glite/jobid/api_java/Jobid.java

index 71b63cd..844d918 100644 (file)
@@ -1,20 +1,20 @@
-<!--\r
-Copyright 2003-2004 The Apache Software Foundation.\r
\r
-Licensed under the Apache License, Version 2.0 (the "License");\r
-you may not use this file except in compliance with the License.\r
-You may obtain a copy of the License at\r
-\r
-     http://www.apache.org/licenses/LICENSE-2.0\r
-\r
-Unless required by applicable law or agreed to in writing, software\r
-distributed under the License is distributed on an "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-See the License for the specific language governing permissions and\r
-limitations under the License.\r
--->\r
-<html>\r
- <body>\r
-  Base64, Binary, and Hexadecimal String encoding and decoding.\r
- </body>\r
-</html>\r
+<!--
+Copyright 2003-2004 The Apache Software Foundation.
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+<html>
+ <body>
+  Base64, Binary, and Hexadecimal String encoding and decoding.
+ </body>
+</html>
index ec8dedd..13ec053 100755 (executable)
@@ -22,9 +22,7 @@ public class CheckedString {
             throw new IllegalArgumentException("checkedString is null");
         }
         
-        checkedString = checkedString.replaceAll("[\\\"]", "\\\\\"");
-        checkedString = checkedString.replaceAll("[\n]", "\\\\\\\\n");
-        this.checkedString = checkedString;
+        setCheckedString(checkedString);
     }
 
     /**
@@ -44,6 +42,8 @@ public class CheckedString {
     public void setCheckedString(String checkedString) {
         checkedString = checkedString.replaceAll("[\\\"]", "\\\\\"");
         checkedString = checkedString.replaceAll("[\n]", "\\\\\\\\n");
+        checkedString = checkedString.replaceAll("[/]", "_");
+        checkedString = checkedString.replaceAll("[\\+]", "-");
         this.checkedString = checkedString;
     }
     
@@ -52,6 +52,7 @@ public class CheckedString {
      * 
      * @return converted string
      */
+    @Override
     public String toString() {
         return checkedString;
     }
index 9cdedea..e65f527 100755 (executable)
@@ -1,6 +1,5 @@
 package org.glite.jobid.api_java;
 
-import java.math.BigInteger;
 import java.net.UnknownHostException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
@@ -29,7 +28,8 @@ public class Jobid {
 
     /**
      * Creates new instace of JobId with BK server address and port number, unique part 
-     * is generated
+     * is generated. If some exception is catched during generating the unique part, then
+     * System.exit(-1); is called.
      * 
      * @param bkserver BK server address
      * @param port BK server port
@@ -69,41 +69,14 @@ public class Jobid {
                      new Random().nextInt(999999);
             
             digest.update(unique.getBytes(),0,unique.length());
-            unique = new BigInteger(1,digest.digest()).toString(16);
+            Base64 base64 = new Base64();
+            byte[] tmp = base64.encode(digest.digest());
+            unique = new CheckedString(new String(tmp, 0, tmp.length-2)).toString();
             
         } catch (NoSuchAlgorithmException ex) {
             System.err.println(ex);
+            System.exit(-1);
         }
-        
-        Base64 base64 = new Base64();
-        byte[] tmp = base64.decode(unique.getBytes());
-        boolean test = false;
-        while(!test) {
-            test = true;
-            for (int i = 0; i < tmp.length; i++) {
-                if (tmp[i] < 0) {
-                    tmp[i] = (byte)(-1*tmp[i]);
-                    test = false;
-                }
-                if (tmp[i] <= 47) {
-                    tmp[i] = (byte)(10+tmp[i]);
-                    test = false;
-                }
-                if (tmp[i] <= 64 && tmp[i] >= 58) {
-                    tmp[i] = (byte)(7+tmp[i]);
-                    test = false;
-                }
-                if (tmp[i] <= 96 && tmp[i] >= 91) {
-                    tmp[i] = (byte)(6+tmp[i]);
-                    test = false;
-                }
-                if (tmp[i] >= 123) {
-                    tmp[i] = (byte)(5-tmp[i]);
-                    test = false;
-                }
-            }
-        }
-        unique = new String(tmp);
     }
     
     /**