View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.lang.codec;
20  
21  /**
22   * Provides <a href="http://en.wikipedia.org/wiki/Base64">Base 64</a> encoding and decoding as defined by
23   * <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>.
24   * <p/>
25   * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
26   * Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
27   * <p/>
28   * This class was borrowed from Apache Commons Codec SVN repository (rev. 618419) with modifications
29   * to enable Base64 conversion without a full dependency on Commons Codec.  We didn't want to reinvent the wheel of
30   * great work they've done, but also didn't want to force every Shiro user to depend on the commons-codec.jar
31   * <p/>
32   * As per the Apache 2.0 license, the original copyright notice and all author and copyright information have
33   * remained intact.
34   *
35   * @see <a href="http://en.wikipedia.org/wiki/Base64">Wikipedia: Base 64</a>
36   * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
37   * @since 0.9
38   */
39  public final class Base64 {
40  
41      private Base64() {
42      }
43  
44      /**
45       * Base64 encodes the specified byte array and then encodes it as a String using Shiro's preferred character
46       * encoding (UTF-8).
47       *
48       * @param bytes the byte array to Base64 encode.
49       * @return a UTF-8 encoded String of the resulting Base64 encoded byte array.
50       */
51      public static String encodeToString(byte[] bytes) {
52          byte[] encoded = encode(bytes);
53          return CodecSupport.toString(encoded);
54      }
55  
56      /**
57       * Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet.
58       *
59       * @param pArray a byte array containing binary data
60       * @return A byte array containing only Base64 character data
61       */
62      public static byte[] encode(byte[] pArray) {
63          return java.util.Base64.getEncoder().encode(pArray);
64      }
65  
66  
67      /**
68       * Converts the specified UTF-8 Base64 encoded String and decodes it to a resultant UTF-8 encoded string.
69       *
70       * @param base64Encoded a UTF-8 Base64 encoded String
71       * @return the decoded String, UTF-8 encoded.
72       */
73      public static String decodeToString(String base64Encoded) {
74          byte[] encodedBytes = CodecSupport.toBytes(base64Encoded);
75          return decodeToString(encodedBytes);
76      }
77  
78      /**
79       * Decodes the specified Base64 encoded byte array and returns the decoded result as a UTF-8 encoded.
80       *
81       * @param base64Encoded a Base64 encoded byte array
82       * @return the decoded String, UTF-8 encoded.
83       */
84      public static String decodeToString(byte[] base64Encoded) {
85          byte[] decoded = decode(base64Encoded);
86          return CodecSupport.toString(decoded);
87      }
88  
89      /**
90       * Converts the specified UTF-8 Base64 encoded String and decodes it to a raw Base64 decoded byte array.
91       *
92       * @param base64Encoded a UTF-8 Base64 encoded String
93       * @return the raw Base64 decoded byte array.
94       */
95      public static byte[] decode(String base64Encoded) {
96          byte[] bytes = CodecSupport.toBytes(base64Encoded);
97          return decode(bytes);
98      }
99  
100     /**
101      * Decodes Base64 data into octets
102      *
103      * @param base64Data Byte array containing Base64 data
104      * @return Array containing decoded data.
105      */
106     public static byte[] decode(byte[] base64Data) {
107         return java.util.Base64.getDecoder().decode(base64Data);
108     }
109 
110 }