2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2004-11-03 00:53:30 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* 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.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack.util;
|
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2003-01-13 17:58:47 +01:00
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2014-05-29 09:21:04 +02:00
|
|
|
import java.util.Collection;
|
2003-01-13 17:58:47 +01:00
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A collection of utility methods for String objects.
|
|
|
|
*/
|
|
|
|
public class StringUtils {
|
2014-07-07 00:08:37 +02:00
|
|
|
|
|
|
|
public static final String SHA1 = "SHA-1";
|
|
|
|
public static final String UTF8 = "UTF-8";
|
2010-08-15 15:20:48 +02:00
|
|
|
|
2014-03-18 18:40:08 +01:00
|
|
|
public static final String QUOTE_ENCODE = """;
|
|
|
|
public static final String APOS_ENCODE = "'";
|
|
|
|
public static final String AMP_ENCODE = "&";
|
|
|
|
public static final String LT_ENCODE = "<";
|
|
|
|
public static final String GT_ENCODE = ">";
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Escapes all necessary characters in the String so that it can be used
|
|
|
|
* in an XML doc.
|
|
|
|
*
|
|
|
|
* @param string the string to escape.
|
|
|
|
* @return the string with appropriate characters escaped.
|
|
|
|
*/
|
2014-03-21 09:51:52 +01:00
|
|
|
public static CharSequence escapeForXML(final String string) {
|
2003-01-13 17:58:47 +01:00
|
|
|
if (string == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-18 18:40:08 +01:00
|
|
|
final char[] input = string.toCharArray();
|
|
|
|
final int len = input.length;
|
|
|
|
final StringBuilder out = new StringBuilder((int)(len*1.3));
|
|
|
|
CharSequence toAppend;
|
2003-01-13 17:58:47 +01:00
|
|
|
char ch;
|
2014-03-18 18:40:08 +01:00
|
|
|
int last = 0;
|
|
|
|
int i = 0;
|
|
|
|
while (i < len) {
|
|
|
|
toAppend = null;
|
2003-01-13 17:58:47 +01:00
|
|
|
ch = input[i];
|
2014-03-18 18:40:08 +01:00
|
|
|
switch(ch) {
|
|
|
|
case '<':
|
|
|
|
toAppend = LT_ENCODE;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
toAppend = GT_ENCODE;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
toAppend = AMP_ENCODE;
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
toAppend = QUOTE_ENCODE;
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
toAppend = APOS_ENCODE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2014-03-18 18:40:08 +01:00
|
|
|
if (toAppend != null) {
|
2009-06-04 13:58:25 +02:00
|
|
|
if (i > last) {
|
|
|
|
out.append(input, last, i - last);
|
|
|
|
}
|
2014-03-18 18:40:08 +01:00
|
|
|
out.append(toAppend);
|
|
|
|
last = ++i;
|
|
|
|
} else {
|
|
|
|
i++;
|
2009-06-04 13:58:25 +02:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
if (last == 0) {
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
if (i > last) {
|
|
|
|
out.append(input, last, i - last);
|
|
|
|
}
|
2014-03-21 09:51:52 +01:00
|
|
|
return out;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by the hash method.
|
|
|
|
*/
|
|
|
|
private static MessageDigest digest = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hashes a String using the SHA-1 algorithm and returns the result as a
|
|
|
|
* String of hexadecimal numbers. This method is synchronized to avoid
|
|
|
|
* excessive MessageDigest object creation. If calling this method becomes
|
|
|
|
* a bottleneck in your code, you may wish to maintain a pool of
|
|
|
|
* MessageDigest objects instead of using this method.
|
|
|
|
* <p>
|
|
|
|
* A hash is a one-way function -- that is, given an
|
|
|
|
* input, an output is easily computed. However, given the output, the
|
|
|
|
* input is almost impossible to compute. This is useful for passwords
|
|
|
|
* since we can store the hash and a hacker will then have a very hard time
|
|
|
|
* determining the original password.
|
|
|
|
*
|
|
|
|
* @param data the String to compute the hash of.
|
|
|
|
* @return a hashed version of the passed-in String
|
|
|
|
*/
|
2005-08-26 18:49:25 +02:00
|
|
|
public synchronized static String hash(String data) {
|
2003-01-13 17:58:47 +01:00
|
|
|
if (digest == null) {
|
|
|
|
try {
|
2014-07-07 00:08:37 +02:00
|
|
|
digest = MessageDigest.getInstance(SHA1);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
catch (NoSuchAlgorithmException nsae) {
|
2014-07-07 00:08:37 +02:00
|
|
|
// Smack wont be able to function normally if this exception is thrown, wrap it into
|
|
|
|
// an ISE and make the user aware of the problem.
|
|
|
|
throw new IllegalStateException(nsae);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now, compute hash.
|
|
|
|
try {
|
2014-07-07 00:08:37 +02:00
|
|
|
digest.update(data.getBytes(UTF8));
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
catch (UnsupportedEncodingException e) {
|
2014-07-07 00:08:37 +02:00
|
|
|
// Smack wont be able to function normally if this exception is thrown, wrap it into an
|
|
|
|
// ISE and make the user aware of the problem.
|
|
|
|
throw new IllegalStateException(e);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
return encodeHex(digest.digest());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-08-26 18:49:25 +02:00
|
|
|
* Encodes an array of bytes as String representation of hexadecimal.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2005-08-26 18:49:25 +02:00
|
|
|
* @param bytes an array of bytes to convert to a hex string.
|
|
|
|
* @return generated hex string.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2005-08-26 18:49:25 +02:00
|
|
|
public static String encodeHex(byte[] bytes) {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder hex = new StringBuilder(bytes.length * 2);
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
for (byte aByte : bytes) {
|
|
|
|
if (((int) aByte & 0xff) < 0x10) {
|
2005-08-26 18:49:25 +02:00
|
|
|
hex.append("0");
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2006-07-18 07:14:33 +02:00
|
|
|
hex.append(Integer.toString((int) aByte & 0xff, 16));
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2006-05-03 23:13:25 +02:00
|
|
|
|
2005-08-26 18:49:25 +02:00
|
|
|
return hex.toString();
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = null;
|
|
|
|
try {
|
2003-06-21 21:40:32 +02:00
|
|
|
bytes = data.getBytes("ISO-8859-1");
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
catch (UnsupportedEncodingException uee) {
|
2014-02-02 23:39:07 +01:00
|
|
|
throw new IllegalStateException(uee);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
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) {
|
2006-05-03 23:13:25 +02:00
|
|
|
return encodeBase64(data, false);
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2006-05-03 23:13:25 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-05-03 23:13:25 +02:00
|
|
|
* Encodes a byte array into a bse64 String.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2006-05-03 23:13:25 +02:00
|
|
|
* @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.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2006-05-03 23:13:25 +02:00
|
|
|
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));
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-05-03 23:13:25 +02:00
|
|
|
* Decodes a base64 String.
|
2013-01-29 23:49:20 +01:00
|
|
|
* Unlike Base64.decode() this method does not try to detect and decompress a gzip-compressed input.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2006-05-03 23:13:25 +02:00
|
|
|
* @param data a base64 encoded String to decode.
|
2003-01-13 17:58:47 +01:00
|
|
|
* @return the decoded String.
|
|
|
|
*/
|
2006-05-03 23:13:25 +02:00
|
|
|
public static byte[] decodeBase64(String data) {
|
2013-01-29 23:49:20 +01:00
|
|
|
byte[] bytes;
|
|
|
|
try {
|
|
|
|
bytes = data.getBytes("UTF-8");
|
|
|
|
} catch (java.io.UnsupportedEncodingException uee) {
|
|
|
|
bytes = data.getBytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = Base64.decode(bytes, 0, bytes.length, Base64.NO_OPTIONS);
|
|
|
|
return bytes;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pseudo-random number generator object for use with randomString().
|
|
|
|
* The Random class is not considered to be cryptographically secure, so
|
|
|
|
* only use these random Strings for low to medium security applications.
|
|
|
|
*/
|
|
|
|
private static Random randGen = new Random();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of numbers and letters of mixed case. Numbers appear in the list
|
|
|
|
* twice so that there is a more equal chance that a number will be picked.
|
|
|
|
* We can use the array to get a random number or letter by picking a random
|
|
|
|
* array index.
|
|
|
|
*/
|
|
|
|
private static char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
|
|
|
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a random String of numbers and letters (lower and upper case)
|
|
|
|
* of the specified length. The method uses the Random class that is
|
|
|
|
* built-in to Java which is suitable for low to medium grade security uses.
|
|
|
|
* This means that the output is only pseudo random, i.e., each number is
|
|
|
|
* mathematically generated so is not truly random.<p>
|
|
|
|
*
|
|
|
|
* The specified length must be at least one. If not, the method will return
|
|
|
|
* null.
|
|
|
|
*
|
|
|
|
* @param length the desired length of the random String to return.
|
|
|
|
* @return a random String of numbers and letters of the specified length.
|
|
|
|
*/
|
2006-05-03 23:13:25 +02:00
|
|
|
public static String randomString(int length) {
|
2003-01-13 17:58:47 +01:00
|
|
|
if (length < 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Create a char buffer to put random letters and numbers in.
|
|
|
|
char [] randBuffer = new char[length];
|
|
|
|
for (int i=0; i<randBuffer.length; i++) {
|
2014-07-07 00:03:11 +02:00
|
|
|
randBuffer[i] = numbersAndLetters[randGen.nextInt(numbersAndLetters.length)];
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
return new String(randBuffer);
|
|
|
|
}
|
2003-01-16 16:40:55 +01:00
|
|
|
|
2014-03-06 09:42:25 +01:00
|
|
|
/**
|
2014-04-15 23:33:42 +02:00
|
|
|
* Returns true if CharSequence is not null and is not empty, false otherwise
|
2014-03-06 09:42:25 +01:00
|
|
|
* Examples:
|
|
|
|
* isNotEmpty(null) - false
|
|
|
|
* isNotEmpty("") - false
|
|
|
|
* isNotEmpty(" ") - true
|
|
|
|
* isNotEmpty("empty") - true
|
|
|
|
*
|
2014-04-15 23:33:42 +02:00
|
|
|
* @param cs checked CharSequence
|
2014-03-06 09:42:25 +01:00
|
|
|
* @return true if string is not null and is not empty, false otherwise
|
|
|
|
*/
|
2014-04-15 23:33:42 +02:00
|
|
|
public static boolean isNotEmpty(CharSequence cs) {
|
|
|
|
return !isNullOrEmpty(cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given CharSequence is not null or empty.
|
|
|
|
*
|
|
|
|
* @param cs
|
|
|
|
* @return true if the given CharSequence is not null or empty
|
|
|
|
*/
|
|
|
|
public static boolean isNullOrEmpty(CharSequence cs) {
|
|
|
|
return cs == null || cs.length() == 0;
|
2014-03-06 09:42:25 +01:00
|
|
|
}
|
2014-05-29 09:21:04 +02:00
|
|
|
|
|
|
|
public static String collectionToString(Collection<String> collection) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (String s : collection) {
|
|
|
|
sb.append(s);
|
|
|
|
sb.append(" ");
|
|
|
|
}
|
|
|
|
String res = sb.toString();
|
|
|
|
// Remove the trailing whitespace
|
|
|
|
res = res.substring(0, res.length() - 1);
|
|
|
|
return res;
|
|
|
|
}
|
2009-06-04 13:58:25 +02:00
|
|
|
}
|