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.math.BigInteger;
023
024import net.jcip.annotations.Immutable;
025import net.minidev.json.JSONAware;
026import net.minidev.json.JSONValue;
027
028
029/**
030 * Base64-encoded object.
031 *
032 * @author Vladimir Dzhuvinov
033 * @version 2019-10-04
034 */
035@Immutable
036public class Base64 implements JSONAware, Serializable {
037
038
039        private static final long serialVersionUID = 1L;
040
041
042        /**
043         * The Base64 value.
044         */
045        private final String value;
046
047
048        /**
049         * Creates a new Base64-encoded object.
050         *
051         * @param base64 The Base64-encoded object value. The value is not 
052         *               validated for having characters from a Base64 
053         *               alphabet. Must not be {@code null}.
054         */
055        public Base64(final String base64) {
056
057                if (base64 == null) {
058
059                        throw new IllegalArgumentException("The Base64 value must not be null");
060                }
061
062                value = base64;
063        }
064
065
066        /**
067         * Decodes this Base64 object to a byte array.
068         *
069         * @return The resulting byte array.
070         */
071        public byte[] decode() {
072
073                return Base64Codec.decode(value);
074        }
075
076
077        /**
078         * Decodes this Base64 object to an unsigned big integer.
079         *
080         * <p>Same as {@code new BigInteger(1, base64.decode())}.
081         *
082         * @return The resulting big integer.
083         */
084        public BigInteger decodeToBigInteger() {
085
086                return new BigInteger(1, decode());
087        }
088
089
090        /**
091         * Decodes this Base64 object to a string.
092         *
093         * @return The resulting string, in the UTF-8 character set.
094         */
095        public String decodeToString() {
096
097                return new String(decode(), StandardCharset.UTF_8);
098        }
099
100
101        /**
102         * Returns a JSON string representation of this object.
103         *
104         * @return The JSON string representation of this object.
105         */
106        @Override
107        public String toJSONString() {
108
109                return "\"" + JSONValue.escape(value) + "\"";
110        }
111
112
113        /**
114         * Returns a Base64 string representation of this object. The string 
115         * will be chunked into 76 character blocks separated by CRLF.
116         *
117         * @return The Base64 string representation, chunked into 76 character 
118         *         blocks separated by CRLF.
119         */
120        @Override
121        public String toString() {
122
123                return value;
124        }
125
126
127        /**
128         * Overrides {@code Object.hashCode()}.
129         *
130         * @return The object hash code.
131         */
132        @Override
133        public int hashCode() {
134
135                return value.hashCode();
136        }
137
138
139        /**
140         * Overrides {@code Object.equals()}.
141         *
142         * @param object The object to compare to.
143         *
144         * @return {@code true} if the objects have the same value, otherwise
145         *         {@code false}.
146         */
147        @Override
148        public boolean equals(final Object object) {
149
150                return object != null && 
151                       object instanceof Base64 && 
152                       this.toString().equals(object.toString());
153        }
154        
155        
156        /**
157         * Creates a new Base64-encoded object from the specified string.
158         *
159         * @param base64 The Base64-encoded object value, {@code null} if not
160         *               specified. The value is not validated for having
161         *               characters from the Base64 alphabet.
162         *
163         * @return The Base64-encoded object, {@code null} if not specified.
164         */
165        public static Base64 from(final String base64) {
166                
167                if (base64 == null) {
168                        return null;
169                }
170                
171                return new Base64(base64);
172        }
173
174
175        /**
176         * Base64-encodes the specified byte array. 
177         *
178         * @param bytes The byte array to encode. Must not be {@code null}.
179         *
180         * @return The resulting Base64 object.
181         */
182        public static Base64 encode(final byte[] bytes) {
183
184                return new Base64(Base64Codec.encodeToString(bytes, false));
185        }
186
187
188        /**
189         * Base64-encodes the specified big integer, without the sign bit.
190         *
191         * @param bigInt The big integer to encode. Must not be {@code null}.
192         *
193         * @return The resulting Base64 object.
194         */
195        public static Base64 encode(final BigInteger bigInt) {
196
197                return encode(BigIntegerUtils.toBytesUnsigned(bigInt));
198        }
199
200
201        /**
202         * Base64-encodes the specified string.
203         *
204         * @param text The string to encode. Must be in the UTF-8 character set
205         *             and not {@code null}.
206         *
207         * @return The resulting Base64 object.
208         */
209        public static Base64 encode(final String text) {
210
211                return encode(text.getBytes(StandardCharset.UTF_8));
212        }
213}