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 2018-01-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 != null && 067 object instanceof Base64URL && 068 this.toString().equals(object.toString()); 069 } 070 071 072 /** 073 * Base64URL-encodes the specified byte array. 074 * 075 * @param bytes The byte array to encode. Must not be {@code null}. 076 * 077 * @return The resulting Base64URL object. 078 */ 079 public static Base64URL encode(final byte[] bytes) { 080 081 return new Base64URL(Base64Codec.encodeToString(bytes, true)); 082 } 083 084 085 /** 086 * Base64URL-encodes the specified big integer, without the sign bit. 087 * 088 * @param bigInt The big integer to encode. Must not be {@code null}. 089 * 090 * @return The resulting Base64URL object. 091 */ 092 public static Base64URL encode(final BigInteger bigInt) { 093 094 return encode(BigIntegerUtils.toBytesUnsigned(bigInt)); 095 } 096 097 098 /** 099 * Base64URL-encodes the specified string. 100 * 101 * @param text The string to encode. Must be in the UTF-8 character set 102 * and not {@code null}. 103 * 104 * @return The resulting Base64URL object. 105 */ 106 public static Base64URL encode(final String text) { 107 108 return encode(text.getBytes(StandardCharset.UTF_8)); 109 } 110}