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.math.BigInteger;
022
023import net.jcip.annotations.Immutable;
024
025
026/**
027 * Base64URL-encoded object.
028 *
029 * <p>Related specifications:
030 *
031 * <ul>
032 *     <li>RFC 4648.
033 * </ul>
034 *
035 * @author Vladimir Dzhuvinov
036 * @version 2019-10-04
037 */
038@Immutable
039public class Base64URL extends Base64 {
040
041
042        /**
043         * Creates a new Base64URL-encoded object.
044         *
045         * @param base64URL The Base64URL-encoded object value. The value is 
046         *                  not validated for having characters from the 
047         *                  Base64URL alphabet. Must not be {@code null}.
048         */
049        public Base64URL(final String base64URL) {
050
051                super(base64URL);
052        }
053
054
055        /**
056         * Overrides {@code Object.equals()}.
057         *
058         * @param object The object to compare to.
059         *
060         * @return {@code true} if the objects have the same value, otherwise
061         *         {@code false}.
062         */
063        @Override
064        public boolean equals(final Object object) {
065
066                return object instanceof Base64URL &&
067                        this.toString().equals(object.toString());
068        }
069        
070        
071        /**
072         * Creates a new Base64URL-encoded object from the specified string.
073         *
074         * @param base64URL The Base64URL-encoded object value, {@code null} if
075         *                  not specified. The value is not validated for
076         *                  having characters from the Base64URL alphabet.
077         *
078         * @return The Base64URL-encoded object, {@code null} if not specified.
079         */
080        public static Base64URL from(final String base64URL) {
081                
082                if (base64URL == null) {
083                        return null;
084                }
085                
086                return new Base64URL(base64URL);
087        }
088
089
090        /**
091         * Base64URL-encodes the specified byte array.
092         *
093         * @param bytes The byte array to encode. Must not be {@code null}.
094         *
095         * @return The resulting Base64URL object.
096         */
097        public static Base64URL encode(final byte[] bytes) {
098
099                return new Base64URL(Base64Codec.encodeToString(bytes, true));
100        }
101
102
103        /**
104         * Base64URL-encodes the specified big integer, without the sign bit.
105         *
106         * @param bigInt The big integer to encode. Must not be {@code null}.
107         *
108         * @return The resulting Base64URL object.
109         */
110        public static Base64URL encode(final BigInteger bigInt) {
111
112                return encode(BigIntegerUtils.toBytesUnsigned(bigInt));
113        }
114
115
116        /**
117         * Base64URL-encodes the specified string.
118         *
119         * @param text The string to encode. Must be in the UTF-8 character set
120         *             and not {@code null}.
121         *
122         * @return The resulting Base64URL object.
123         */
124        public static Base64URL encode(final String text) {
125
126                return encode(text.getBytes(StandardCharset.UTF_8));
127        }
128}