B64.java

  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. import java.util.Random;

  19. /**
  20.  * Base64 like method to convert binary bytes into ASCII chars.
  21.  *
  22.  * TODO: Can Base64 be reused?
  23.  *
  24.  * <p>
  25.  * This class is immutable and thread-safe.
  26.  * </p>
  27.  *
  28.  * @version $Id: B64.java 1435550 2013-01-19 14:09:52Z tn $
  29.  * @since 1.7
  30.  */
  31. class B64 {

  32.     /**
  33.      * Table with characters for Base64 transformation.
  34.      */
  35.     static final String B64T = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

  36.     /**
  37.      * Base64 like conversion of bytes to ASCII chars.
  38.      *
  39.      * @param b2
  40.      *            A byte from the result.
  41.      * @param b1
  42.      *            A byte from the result.
  43.      * @param b0
  44.      *            A byte from the result.
  45.      * @param outLen
  46.      *            The number of expected output chars.
  47.      * @param buffer
  48.      *            Where the output chars is appended to.
  49.      */
  50.     static void b64from24bit(final byte b2, final byte b1, final byte b0, final int outLen,
  51.                              final StringBuilder buffer) {
  52.         // The bit masking is necessary because the JVM byte type is signed!
  53.         int w = ((b2 << 16) & 0x00ffffff) | ((b1 << 8) & 0x00ffff) | (b0 & 0xff);
  54.         // It's effectively a "for" loop but kept to resemble the original C code.
  55.         int n = outLen;
  56.         while (n-- > 0) {
  57.             buffer.append(B64T.charAt(w & 0x3f));
  58.             w >>= 6;
  59.         }
  60.     }

  61.     /**
  62.      * Generates a string of random chars from the B64T set.
  63.      *
  64.      * @param num
  65.      *            Number of chars to generate.
  66.      */
  67.     static String getRandomSalt(final int num) {
  68.         final StringBuilder saltString = new StringBuilder();
  69.         for (int i = 1; i <= num; i++) {
  70.             saltString.append(B64T.charAt(new Random().nextInt(B64T.length())));
  71.         }
  72.         return saltString.toString();
  73.     }
  74. }