001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose.util;
019
020
021import java.io.Serializable;
022import java.nio.charset.Charset;
023import java.math.BigInteger;
024
025import net.jcip.annotations.Immutable;
026
027import net.minidev.json.JSONAware;
028import net.minidev.json.JSONValue;
029
030
031/**
032 * Base64-encoded object.
033 *
034 * @author Vladimir Dzhuvinov
035 */
036@Immutable
037public class Base64 implements JSONAware, Serializable {
038
039
040        private static final long serialVersionUID = 1L;
041
042
043        /**
044         * UTF-8 is the required character set for all JOSE + JWT objects.
045         */
046        public static final Charset CHARSET = Charset.forName("UTF-8");
047
048
049        /**
050         * The Base64 value.
051         */
052        private final String value;
053
054
055        /**
056         * Creates a new Base64-encoded object.
057         *
058         * @param base64 The Base64-encoded object value. The value is not 
059         *               validated for having characters from a Base64 
060         *               alphabet. Must not be {@code null}.
061         */
062        public Base64(final String base64) {
063
064                if (base64 == null) {
065
066                        throw new IllegalArgumentException("The Base64 value must not be null");
067                }
068
069                value = base64;
070        }
071
072
073        /**
074         * Decodes this Base64 object to a byte array.
075         *
076         * @return The resulting byte array.
077         */
078        public byte[] decode() {
079
080                return Base64Codec.decode(value);
081        }
082
083
084        /**
085         * Decodes this Base64 object to an unsigned big integer.
086         *
087         * <p>Same as {@code new BigInteger(1, base64.decode())}.
088         *
089         * @return The resulting big integer.
090         */
091        public BigInteger decodeToBigInteger() {
092
093                return new BigInteger(1, decode());
094        }
095
096
097        /**
098         * Decodes this Base64 object to a string.
099         *
100         * @return The resulting string, in the UTF-8 character set.
101         */
102        public String decodeToString() {
103
104                return new String(decode(), CHARSET);
105        }
106
107
108        /**
109         * Returns a JSON string representation of this object.
110         *
111         * @return The JSON string representation of this object.
112         */
113        @Override
114        public String toJSONString() {
115
116                return "\"" + JSONValue.escape(value) + "\"";
117        }
118
119
120        /**
121         * Returns a Base64 string representation of this object. The string 
122         * will be chunked into 76 character blocks separated by CRLF.
123         *
124         * @return The Base64 string representation, chunked into 76 character 
125         *         blocks separated by CRLF.
126         */
127        @Override
128        public String toString() {
129
130                return value;
131        }
132
133
134        /**
135         * Overrides {@code Object.hashCode()}.
136         *
137         * @return The object hash code.
138         */
139        @Override
140        public int hashCode() {
141
142                return value.hashCode();
143        }
144
145
146        /**
147         * Overrides {@code Object.equals()}.
148         *
149         * @param object The object to compare to.
150         *
151         * @return {@code true} if the objects have the same value, otherwise
152         *         {@code false}.
153         */
154        @Override
155        public boolean equals(final Object object) {
156
157                return object != null && 
158                       object instanceof Base64 && 
159                       this.toString().equals(object.toString());
160        }
161
162
163        /**
164         * Base64-encodes the specified byte array. 
165         *
166         * @param bytes The byte array to encode. Must not be {@code null}.
167         *
168         * @return The resulting Base64 object.
169         */
170        public static Base64 encode(final byte[] bytes) {
171
172                return new Base64(Base64Codec.encodeToString(bytes, false));
173        }
174
175
176        /**
177         * Base64-encodes the specified big integer, without the sign bit.
178         *
179         * @param bigInt The big integer to encode. Must not be {@code null}.
180         *
181         * @return The resulting Base64 object.
182         */
183        public static Base64 encode(final BigInteger bigInt) {
184
185                return encode(BigIntegerUtils.toBytesUnsigned(bigInt));
186        }
187
188
189        /**
190         * Base64-encodes the specified string.
191         *
192         * @param text The string to encode. Must be in the UTF-8 character set
193         *             and not {@code null}.
194         *
195         * @return The resulting Base64 object.
196         */
197        public static Base64 encode(final String text) {
198
199                return encode(text.getBytes(CHARSET));
200        }
201}