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