1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02:00

Create smack.util.stringencoder for Base64, Base32,…

Use Android's Base64 implementation when on Android, otherwise, when on
Java7, use the existing one.
This commit is contained in:
Florian Schmaus 2014-09-04 11:04:51 +02:00
parent 90c0064394
commit 5d4aa76d19
32 changed files with 491 additions and 231 deletions

View file

@ -27,7 +27,7 @@ import java.net.UnknownHostException;
import javax.net.SocketFactory;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -92,7 +92,7 @@ class HTTPProxySocketFactory
else
{
String password = proxy.getProxyPassword();
proxyLine = "\r\nProxy-Authorization: Basic " + StringUtils.encodeBase64(username + ":" + password);
proxyLine = "\r\nProxy-Authorization: Basic " + Base64.encode(username + ":" + password);
}
socket.getOutputStream().write((hostport + " HTTP/1.1\r\nHost: "
+ hostport + proxyLine + "\r\n\r\n").getBytes("UTF-8"));

View file

@ -28,9 +28,9 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.RosterPacket.Item;
import org.jivesoftware.smack.util.Base32Encoder;
import org.jivesoftware.smack.util.FileUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smack.util.stringencoder.Base32;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@ -298,7 +298,7 @@ public class DirectoryRosterStore implements RosterStore {
private File getBareJidFile(String bareJid) {
String encodedJid = Base32Encoder.getInstance().encode(bareJid);
String encodedJid = Base32.encode(bareJid);
return new File(fileDir, ENTRY_PREFIX + encodedJid);
}

View file

@ -22,6 +22,7 @@ import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.sasl.packet.SaslStanzas.AuthMechanism;
import org.jivesoftware.smack.sasl.packet.SaslStanzas.Response;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import javax.security.auth.callback.CallbackHandler;
@ -172,7 +173,7 @@ public abstract class SASLMechanism implements Comparable<SASLMechanism> {
byte[] authenticationBytes = getAuthenticationText();
String authenticationText;
if (authenticationBytes != null) {
authenticationText = StringUtils.encodeBase64(authenticationBytes);
authenticationText = Base64.encodeToString(authenticationBytes);
} else {
// RFC6120 6.4.2 "If the initiating entity needs to send a zero-length initial response,
// it MUST transmit the response as a single equals sign character ("="), which
@ -202,7 +203,7 @@ public abstract class SASLMechanism implements Comparable<SASLMechanism> {
* @throws SmackException
*/
public final void challengeReceived(String challengeString, boolean finalChallenge) throws SmackException, NotConnectedException {
byte[] challenge = StringUtils.decodeBase64(challengeString);
byte[] challenge = Base64.decode(challengeString);
byte[] response = evaluateChallenge(challenge);
if (finalChallenge) {
return;
@ -213,7 +214,7 @@ public abstract class SASLMechanism implements Comparable<SASLMechanism> {
responseStanza = new Response();
}
else {
responseStanza = new Response(StringUtils.encodeBase64(response, false));
responseStanza = new Response(Base64.encodeToString(response));
}
// Send the authentication to the server

View file

@ -1,44 +0,0 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
/**
* A Base 64 encoding implementation.
* @author Florian Schmaus
*/
public class Base64Encoder implements StringEncoder {
private static Base64Encoder instance = new Base64Encoder();
private Base64Encoder() {
// Use getInstance()
}
public static Base64Encoder getInstance() {
return instance;
}
public String encode(String s) {
return Base64.encodeBytes(s.getBytes());
}
public String decode(String s) {
return new String(Base64.decode(s));
}
}

View file

@ -1,50 +0,0 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
/**
* A Base 64 encoding implementation that generates filename and Url safe encodings.
*
* <p>
* Note: This does NOT produce standard Base 64 encodings, but a variant as defined in
* Section 4 of RFC3548:
* <a href="http://www.faqs.org/rfcs/rfc3548.html">http://www.faqs.org/rfcs/rfc3548.html</a>.
*
* @author Robin Collier
*/
public class Base64FileUrlEncoder implements StringEncoder {
private static Base64FileUrlEncoder instance = new Base64FileUrlEncoder();
private Base64FileUrlEncoder() {
// Use getInstance()
}
public static Base64FileUrlEncoder getInstance() {
return instance;
}
public String encode(String s) {
return Base64.encodeBytes(s.getBytes(), Base64.URL_SAFE);
}
public String decode(String s) {
return new String(Base64.decode(s, Base64.URL_SAFE));
}
}

View file

@ -31,6 +31,7 @@ public class StringUtils {
public static final String MD5 = "MD5";
public static final String SHA1 = "SHA-1";
public static final String UTF8 = "UTF-8";
public static final String USASCII = "US-ASCII";
public static final String QUOTE_ENCODE = "&quot;";
public static final String APOS_ENCODE = "&apos;";
@ -160,67 +161,6 @@ public class StringUtils {
throw new IllegalStateException("UTF-8 encoding not supported by platform", e);
}
}
/**
* Encodes a String as a base64 String.
*
* @param data a String to encode.
* @return a base64 encoded String.
*/
public static String encodeBase64(String data) {
byte [] bytes = toBytes(data);
return encodeBase64(bytes);
}
/**
* Encodes a byte array into a base64 String.
*
* @param data a byte array to encode.
* @return a base64 encode String.
*/
public static String encodeBase64(byte[] data) {
return encodeBase64(data, false);
}
/**
* Encodes a byte array into a bse64 String.
*
* @param data The byte arry to encode.
* @param lineBreaks True if the encoding should contain line breaks and false if it should not.
* @return A base64 encoded String.
*/
public static String encodeBase64(byte[] data, boolean lineBreaks) {
return encodeBase64(data, 0, data.length, lineBreaks);
}
/**
* Encodes a byte array into a bse64 String.
*
* @param data The byte arry to encode.
* @param offset the offset of the bytearray to begin encoding at.
* @param len the length of bytes to encode.
* @param lineBreaks True if the encoding should contain line breaks and false if it should not.
* @return A base64 encoded String.
*/
public static String encodeBase64(byte[] data, int offset, int len, boolean lineBreaks) {
return Base64.encodeBytes(data, offset, len, (lineBreaks ? Base64.NO_OPTIONS : Base64.DONT_BREAK_LINES));
}
/**
* Decodes a base64 String.
* Unlike Base64.decode() this method does not try to detect and decompress a gzip-compressed input.
*
* @param data a base64 encoded String to decode.
* @return the decoded String.
*/
public static byte[] decodeBase64(String data) {
byte[] bytes = toBytes(data);
return decodeBase64(bytes);
}
public static byte[] decodeBase64(byte[] data) {
return Base64.decode(data, 0, data.length, Base64.NO_OPTIONS);
}
/**
* Pseudo-random number generator object for use with randomString().

View file

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
package org.jivesoftware.smack.util.stringencoder;
import java.io.ByteArrayOutputStream;
@ -30,21 +30,28 @@ import java.io.IOException;
* @see <a href="http://en.wikipedia.org/wiki/Base32">Base32 Wikipedia entry</a>
*
*/
public class Base32Encoder implements StringEncoder {
public class Base32 {
private static Base32Encoder instance = new Base32Encoder();
private static final StringEncoder base32Stringencoder = new StringEncoder() {
@Override
public String encode(String string) {
return Base32.encode(string);
}
@Override
public String decode(String string) {
return Base32.decode(string);
}
};
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ2345678";
private Base32Encoder() {
// Use getInstance()
public static StringEncoder getStringEncoder() {
return base32Stringencoder;
}
public static Base32Encoder getInstance() {
return instance;
}
@Override
public String decode(String str) {
public static String decode(String str) {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
byte[] raw = str.getBytes();
for (int i = 0; i < raw.length; i++) {
@ -102,8 +109,7 @@ public class Base32Encoder implements StringEncoder {
return new String(bs.toByteArray());
}
@Override
public String encode(String str) {
public static String encode(String str) {
byte[] b = str.getBytes();
ByteArrayOutputStream os = new ByteArrayOutputStream();

View file

@ -0,0 +1,107 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util.stringencoder;
import java.io.UnsupportedEncodingException;
import org.jivesoftware.smack.util.StringUtils;
public class Base64 {
private static Base64.Encoder base64encoder;
public static void setEncoder(Base64.Encoder encoder) {
if (encoder == null) {
throw new IllegalArgumentException("encoder must no be null");
}
base64encoder = encoder;
}
public static final String encode(String string) {
try {
return encodeToString(string.getBytes(StringUtils.UTF8));
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported", e);
}
}
public static final String encodeToString(byte[] input) {
byte[] bytes = encode(input);
try {
return new String(bytes, StringUtils.USASCII);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
public static final String encodeToString(byte[] input, int offset, int len) {
byte[] bytes = encode(input, offset, len);
try {
return new String(bytes, StringUtils.USASCII);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
public static final byte[] encode(byte[] input) {
return encode(input, 0, input.length);
}
public static final byte[] encode(byte[] input, int offset, int len) {
return base64encoder.encode(input, offset, len);
}
public static final String decodeToString(String string) {
byte[] bytes = decode(string);
try {
return new String(bytes, StringUtils.UTF8);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported", e);
}
}
public static final String decodeToString(byte[] input, int offset, int len) {
byte[] bytes = decode(input, offset, len);
try {
return new String(bytes, StringUtils.UTF8);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported", e);
}
}
public static final byte[] decode(String string) {
return base64encoder.decode(string);
}
public static final byte[] decode(byte[] input) {
return base64encoder.decode(input, 0, input.length);
}
public static final byte[] decode(byte[] input, int offset, int len) {
return base64encoder.decode(input, offset, len);
}
public interface Encoder {
byte[] decode(String string);
byte[] decode(byte[] input, int offset, int len);
String encodeToString(byte[] input, int offset, int len);
byte[] encode(byte[] input, int offset, int len);
}
}

View file

@ -0,0 +1,42 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util.stringencoder;
public class Base64UrlSafeEncoder {
private static StringEncoder base64UrlSafeEncoder;
public static void setEncoder(StringEncoder encoder) {
if (encoder == null) {
throw new IllegalArgumentException("encoder must no be null");
}
base64UrlSafeEncoder = encoder;
}
public static StringEncoder getStringEncoder() {
return base64UrlSafeEncoder;
}
public static final String encode(String string) {
return base64UrlSafeEncoder.encode(string);
}
public static final String decode(String string) {
return base64UrlSafeEncoder.decode(string);
}
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2013 Florian Schmaus
* Copyright 2013-2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
package org.jivesoftware.smack.util.stringencoder;
/**
* @author Florian Schmaus
@ -27,7 +27,7 @@ public interface StringEncoder {
* @return the encoded String
*/
String encode(String string);
/**
* Decodes an string back to it's initial representation
*

View file

@ -136,51 +136,6 @@ public class StringUtilsTest {
new String(output.getBytes()));
}
/**
* This method tests 2 StringUtil methods - encodeBase64(String) and encodeBase64(byte[]).
*/
@Test
public void testEncodeBase64() {
String input = "";
String output = "";
assertEquals(StringUtils.encodeBase64(input), output);
input = "foo bar 123";
output = "Zm9vIGJhciAxMjM=";
assertEquals(StringUtils.encodeBase64(input), output);
input = "=";
output = "PQ==";
assertEquals(StringUtils.encodeBase64(input), output);
input = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*";
output = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio=";
assertEquals(StringUtils.encodeBase64(input), output);
}
/***
* This method tests 2 StringUtil methods - decodeBase64(String) and decodeBase64(byte[]).
*/
/*
public void testDecodeBase64() {
String input = "";
String output = "";
assertEquals(StringUtils.decodeBase64(input), output);
input = "Zm9vIGJhciAxMjM=";
output = "foo bar 123";
assertEquals(StringUtils.decodeBase64(input), output);
input = "PQ==";
output = "=";
assertEquals(StringUtils.decodeBase64(input), output);
input = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio=";
output = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*";
assertEquals(StringUtils.decodeBase64(input), output);
}
*/
@Test
public void testRandomString() {
// Boundary test

View file

@ -0,0 +1,65 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util.stringencoder;
import static org.junit.Assert.assertEquals;
// TODO those tests should be run with the Java7 and Android impl
public class Base64Test {
/**
* This method tests 2 StringUtil methods - encodeBase64(String) and encodeBase64(byte[]).
*/
public void testEncodeBase64() {
String input = "";
String output = "";
assertEquals(Base64.encode(input), output);
input = "foo bar 123";
output = "Zm9vIGJhciAxMjM=";
assertEquals(Base64.encode(input), output);
input = "=";
output = "PQ==";
assertEquals(Base64.encode(input), output);
input = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*";
output = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio=";
assertEquals(Base64.encode(input), output);
}
/***
* This method tests 2 StringUtil methods - decodeBase64(String) and decodeBase64(byte[]).
*/
public void testDecodeBase64() {
String input = "";
String output = "";
assertEquals(Base64.decodeToString(input), output);
input = "Zm9vIGJhciAxMjM=";
output = "foo bar 123";
assertEquals(Base64.decodeToString(input), output);
input = "PQ==";
output = "=";
assertEquals(Base64.decodeToString(input), output);
input = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio=";
output = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*";
assertEquals(Base64.decodeToString(input), output);
}
}