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