Coverage Report - org.apache.commons.codec.digest.B64
 
Classes in this File Line Coverage Branch Coverage Complexity
B64
100%
11/11
100%
4/4
2
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.codec.digest;
 18  
 
 19  
 import java.util.Random;
 20  
 
 21  
 /**
 22  
  * Base64 like method to convert binary bytes into ASCII chars.
 23  
  *
 24  
  * TODO: Can Base64 be reused?
 25  
  *
 26  
  * <p>
 27  
  * This class is immutable and thread-safe.
 28  
  * </p>
 29  
  *
 30  
  * @version $Id$
 31  
  * @since 1.7
 32  
  */
 33  1
 class B64 {
 34  
 
 35  
     /**
 36  
      * Table with characters for Base64 transformation.
 37  
      */
 38  
     static final String B64T = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 39  
 
 40  
     /**
 41  
      * Base64 like conversion of bytes to ASCII chars.
 42  
      *
 43  
      * @param b2
 44  
      *            A byte from the result.
 45  
      * @param b1
 46  
      *            A byte from the result.
 47  
      * @param b0
 48  
      *            A byte from the result.
 49  
      * @param outLen
 50  
      *            The number of expected output chars.
 51  
      * @param buffer
 52  
      *            Where the output chars is appended to.
 53  
      */
 54  
     static void b64from24bit(byte b2, byte b1, byte b0, int outLen, StringBuilder buffer) {
 55  
         // The bit masking is necessary because the JVM byte type is signed!
 56  732
         int w = ((b2 << 16) & 0x00ffffff) | ((b1 << 8) & 0x00ffff) | (b0 & 0xff);
 57  
         // It's effectively a "for" loop but kept to resemble the original C code.
 58  732
         int n = outLen;
 59  3548
         while (n-- > 0) {
 60  2816
             buffer.append(B64T.charAt(w & 0x3f));
 61  2816
             w >>= 6;
 62  
         }
 63  732
     }
 64  
 
 65  
     /**
 66  
      * Generates a string of random chars from the B64T set.
 67  
      *
 68  
      * @param num
 69  
      *            Number of chars to generate.
 70  
      */
 71  
     static String getRandomSalt(int num) {
 72  18
         StringBuilder saltString = new StringBuilder();
 73  162
         for (int i = 1; i <= num; i++) {
 74  144
             saltString.append(B64T.charAt(new Random().nextInt(B64T.length())));
 75  
         }
 76  18
         return saltString.toString();
 77  
     }
 78  
 }