1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-24 19:47:58 +01:00

Throw IllegalStateException in StringUtils.hash()

instead of Log.

Also create UTF8 and SHA1 constant.
This commit is contained in:
Florian Schmaus 2014-07-07 00:08:37 +02:00
parent e469601468
commit ed7dc1800d

View file

@ -22,14 +22,14 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Collection; import java.util.Collection;
import java.util.Random; import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* A collection of utility methods for String objects. * A collection of utility methods for String objects.
*/ */
public class StringUtils { public class StringUtils {
private static final Logger LOGGER = Logger.getLogger(StringUtils.class.getName());
public static final String SHA1 = "SHA-1";
public static final String UTF8 = "UTF-8";
public static final String QUOTE_ENCODE = """; public static final String QUOTE_ENCODE = """;
public static final String APOS_ENCODE = "'"; public static final String APOS_ENCODE = "'";
@ -120,18 +120,22 @@ public class StringUtils {
public synchronized static String hash(String data) { public synchronized static String hash(String data) {
if (digest == null) { if (digest == null) {
try { try {
digest = MessageDigest.getInstance("SHA-1"); digest = MessageDigest.getInstance(SHA1);
} }
catch (NoSuchAlgorithmException nsae) { catch (NoSuchAlgorithmException nsae) {
LOGGER.log(Level.SEVERE, "Failed to load the SHA-1 MessageDigest. Smack will be unable to function normally.", nsae); // 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);
} }
} }
// Now, compute hash. // Now, compute hash.
try { try {
digest.update(data.getBytes("UTF-8")); digest.update(data.getBytes(UTF8));
} }
catch (UnsupportedEncodingException e) { catch (UnsupportedEncodingException e) {
LOGGER.log(Level.SEVERE, "Error computing hash", e); // 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);
} }
return encodeHex(digest.digest()); return encodeHex(digest.digest());
} }