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 != null && 067 object instanceof Base64URL && 068 this.toString().equals(object.toString()); 069 } 070 071 072 /** 073 * Creates a new Base64URL-encoded object from the specified string. 074 * 075 * @param base64URL The Base64URL-encoded object value, {@code null} if 076 * not specified. The value is not validated for 077 * having characters from the Base64URL alphabet. 078 * 079 * @return The Base64URL-encoded object, {@code null} if not specified. 080 */ 081 public static Base64URL from(final String base64URL) { 082 083 if (base64URL == null) { 084 return null; 085 } 086 087 return new Base64URL(base64URL); 088 } 089 090 091 /** 092 * Base64URL-encodes the specified byte array. 093 * 094 * @param bytes The byte array to encode. Must not be {@code null}. 095 * 096 * @return The resulting Base64URL object. 097 */ 098 public static Base64URL encode(final byte[] bytes) { 099 100 return new Base64URL(Base64Codec.encodeToString(bytes, true)); 101 } 102 103 104 /** 105 * Base64URL-encodes the specified big integer, without the sign bit. 106 * 107 * @param bigInt The big integer to encode. Must not be {@code null}. 108 * 109 * @return The resulting Base64URL object. 110 */ 111 public static Base64URL encode(final BigInteger bigInt) { 112 113 return encode(BigIntegerUtils.toBytesUnsigned(bigInt)); 114 } 115 116 117 /** 118 * Base64URL-encodes the specified string. 119 * 120 * @param text The string to encode. Must be in the UTF-8 character set 121 * and not {@code null}. 122 * 123 * @return The resulting Base64URL object. 124 */ 125 public static Base64URL encode(final String text) { 126 127 return encode(text.getBytes(StandardCharset.UTF_8)); 128 } 129}