001package com.nimbusds.jose.util;
002
003
004import java.math.BigInteger;
005
006import net.jcip.annotations.Immutable;
007
008
009/**
010 * Base64URL-encoded object.
011 *
012 * <p>Related specifications:
013 *
014 * <ul>
015 *     <li>RFC 4648.
016 * </ul>
017 *
018 * @author Vladimir Dzhuvinov
019 * @version $version$ (2013-05-16)
020 */
021@Immutable
022public class Base64URL extends Base64 {
023
024
025        /**
026         * Creates a new Base64URL-encoded object.
027         *
028         * @param base64URL The Base64URL-encoded object value. The value is 
029         *                  not validated for having characters from the 
030         *                  Base64URL alphabet. Must not be {@code null}.
031         */
032        public Base64URL(final String base64URL) {
033
034                super(base64URL);
035        }
036
037
038        /**
039         * Overrides {@code Object.equals()}.
040         *
041         * @param object The object to compare to.
042         *
043         * @return {@code true} if the objects have the same value, otherwise
044         *         {@code false}.
045         */
046        @Override
047        public boolean equals(final Object object) {
048
049                return object != null && 
050                       object instanceof Base64URL && 
051                       this.toString().equals(object.toString());
052        }
053
054
055        /**
056         * Base64URL-encodes the specified byte array.
057         *
058         * @param bytes The byte array to encode. Must not be {@code null}.
059         *
060         * @return The resulting Base64URL object.
061         */
062        public static Base64URL encode(final byte[] bytes) {
063
064                return new Base64URL(org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(bytes));
065        }
066
067
068        /**
069         * Base64URL-encodes the specified big integer, without the sign bit.
070         *
071         * @param bigInt The big integer to encode. Must not be {@code null}.
072         *
073         * @return The resulting Base64URL object.
074         */
075        public static Base64URL encode(final BigInteger bigInt) {
076
077                return encode(BigIntegerUtils.toBytesUnsigned(bigInt));
078        }
079
080
081        /**
082         * Base64URL-encodes the specified string.
083         *
084         * @param text The string to encode. Must be in the UTF-8 character set
085         *             and not {@code null}.
086         *
087         * @return The resulting Base64URL object.
088         */
089        public static Base64URL encode(final String text) {
090
091                return encode(text.getBytes(Base64.CHARSET));
092        }
093}