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