001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.shiro.lang.codec; 020 021/** 022 * Provides <a href="http://en.wikipedia.org/wiki/Base64">Base 64</a> encoding and decoding as defined by 023 * <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>. 024 * <p/> 025 * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose 026 * Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein. 027 * <p/> 028 * This class was borrowed from Apache Commons Codec SVN repository (rev. 618419) with modifications 029 * to enable Base64 conversion without a full dependency on Commons Codec. We didn't want to reinvent the wheel of 030 * great work they've done, but also didn't want to force every Shiro user to depend on the commons-codec.jar 031 * <p/> 032 * As per the Apache 2.0 license, the original copyright notice and all author and copyright information have 033 * remained intact. 034 * 035 * @see <a href="http://en.wikipedia.org/wiki/Base64">Wikipedia: Base 64</a> 036 * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a> 037 * @since 0.9 038 */ 039public final class Base64 { 040 041 private Base64() { 042 } 043 044 /** 045 * Base64 encodes the specified byte array and then encodes it as a String using Shiro's preferred character 046 * encoding (UTF-8). 047 * 048 * @param bytes the byte array to Base64 encode. 049 * @return a UTF-8 encoded String of the resulting Base64 encoded byte array. 050 */ 051 public static String encodeToString(byte[] bytes) { 052 byte[] encoded = encode(bytes); 053 return CodecSupport.toString(encoded); 054 } 055 056 /** 057 * Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet. 058 * 059 * @param pArray a byte array containing binary data 060 * @return A byte array containing only Base64 character data 061 */ 062 public static byte[] encode(byte[] pArray) { 063 return java.util.Base64.getEncoder().encode(pArray); 064 } 065 066 067 /** 068 * Converts the specified UTF-8 Base64 encoded String and decodes it to a resultant UTF-8 encoded string. 069 * 070 * @param base64Encoded a UTF-8 Base64 encoded String 071 * @return the decoded String, UTF-8 encoded. 072 */ 073 public static String decodeToString(String base64Encoded) { 074 byte[] encodedBytes = CodecSupport.toBytes(base64Encoded); 075 return decodeToString(encodedBytes); 076 } 077 078 /** 079 * Decodes the specified Base64 encoded byte array and returns the decoded result as a UTF-8 encoded. 080 * 081 * @param base64Encoded a Base64 encoded byte array 082 * @return the decoded String, UTF-8 encoded. 083 */ 084 public static String decodeToString(byte[] base64Encoded) { 085 byte[] decoded = decode(base64Encoded); 086 return CodecSupport.toString(decoded); 087 } 088 089 /** 090 * Converts the specified UTF-8 Base64 encoded String and decodes it to a raw Base64 decoded byte array. 091 * 092 * @param base64Encoded a UTF-8 Base64 encoded String 093 * @return the raw Base64 decoded byte array. 094 */ 095 public static byte[] decode(String base64Encoded) { 096 byte[] bytes = CodecSupport.toBytes(base64Encoded); 097 return decode(bytes); 098 } 099 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}