001    package com.nimbusds.jose.util;
002    
003    
004    import net.minidev.json.JSONAware;
005    import net.minidev.json.JSONValue;
006    
007    import net.jcip.annotations.Immutable;
008    
009    
010    /**
011     * Base64-encoded object.
012     *
013     * @author Vladimir Dzhuvinov
014     * @version $version$ (2012-10-23)
015     */
016    @Immutable
017    public class Base64 implements JSONAware {
018            
019            
020            /**
021             * The Base64 value.
022             */
023            private final String value;
024            
025            
026            /**
027             * Creates a new Base64-encoded object.
028             *
029             * @param base64 The Base64-encoded object value. The value is not 
030             *               validated for having characters from a Base64 
031             *               alphabet. Must not be {@code null}.
032             */
033            public Base64(final String base64) {
034            
035                    if (base64 == null)
036                            throw new IllegalArgumentException("The Base64 value must not be null");
037                    
038                    value = base64;
039            }
040            
041            
042            /**
043             * Decodes this Base64 object to a byte array.
044             *
045             * @return The resulting byte array.
046             */
047            public byte[] decode() {
048            
049                    return org.apache.commons.codec.binary.Base64.decodeBase64(value);
050            }
051            
052            
053            /**
054             * Returns a JSON string representation of this object.
055             *
056             * @return The JSON string representation of this object.
057             */
058            public String toJSONString() {
059            
060                    return "\"" + JSONValue.escape(value) + "\"";
061            }
062            
063            
064            /**
065             * Returns a Base64 string representation of this object. The string 
066             * will be chunked into 76 character blocks separated by CRLF.
067             *
068             * @return The Base64 string representation, chunked into 76 character 
069             *         blocks separated by CRLF.
070             */
071            public String toString() {
072            
073                    return value;
074            }
075            
076            
077            /**
078             * Overrides {@code Object.hashCode()}.
079             *
080             * @return The object hash code.
081             */
082            public int hashCode() {
083    
084                    return value.hashCode();
085            }
086    
087    
088            /**
089             * Overrides {@code Object.equals()}.
090             *
091             * @param object The object to compare to.
092             *
093             * @return {@code true} if the objects have the same value, otherwise
094             *         {@code false}.
095             */
096            public boolean equals(final Object object) {
097    
098                    return object instanceof Base64 && this.toString().equals(object.toString());
099            }
100    
101            
102            
103            /**
104             * Base64-encode the specified byte array. 
105             *
106             * @param bytes The byte array to encode. Must not be {@code null}.
107             *
108             * @return The resulting Base64 object.
109             */
110            public static Base64URL encode(final byte[] bytes) {
111            
112                    return new Base64URL(org.apache.commons.codec.binary.Base64.encodeBase64String(bytes));
113            }
114    }