1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-22 12:37:58 +01:00

Don't reinvent methods in HashManager

use the existing API from StringUtils instead.

Also rename StringUtils.toBytes() to 'toUtf8Bytes()'.
This commit is contained in:
Florian Schmaus 2017-08-16 14:51:57 +02:00
parent 6565618840
commit 2a97d7c5ea
8 changed files with 91 additions and 105 deletions

View file

@ -303,7 +303,7 @@ public abstract class SASLMechanism implements Comparable<SASLMechanism> {
protected abstract SASLMechanism newInstance();
protected static byte[] toBytes(String string) {
return StringUtils.toBytes(string);
return StringUtils.toUtf8Bytes(string);
}
/**

View file

@ -42,7 +42,7 @@ public class MD5 {
}
public static byte[] bytes(String string) {
return bytes(StringUtils.toBytes(string));
return bytes(StringUtils.toUtf8Bytes(string));
}
public static String hex(byte[] bytes) {
@ -50,7 +50,7 @@ public class MD5 {
}
public static String hex(String string) {
return hex(StringUtils.toBytes(string));
return hex(StringUtils.toUtf8Bytes(string));
}
}

View file

@ -43,7 +43,7 @@ public class SHA1 {
}
public static byte[] bytes(String string) {
return bytes(StringUtils.toBytes(string));
return bytes(StringUtils.toUtf8Bytes(string));
}
public static String hex(byte[] bytes) {
@ -51,7 +51,7 @@ public class SHA1 {
}
public static String hex(String string) {
return hex(StringUtils.toBytes(string));
return hex(StringUtils.toUtf8Bytes(string));
}
}

View file

@ -244,7 +244,7 @@ public class StringUtils {
return new String(hexChars);
}
public static byte[] toBytes(String string) {
public static byte[] toUtf8Bytes(String string) {
try {
return string.getBytes(StringUtils.UTF8);
}

View file

@ -34,7 +34,7 @@ import org.jxmpp.stringprep.XmppStringprepException;
public class DigestMd5SaslTest extends AbstractSaslTest {
protected static final String challenge = "realm=\"xmpp.org\",nonce=\"jgGgnz+cQcmyVaAs2n88kQ==\",qop=\"auth\",charset=utf-8,algorithm=md5-sess";
protected static final byte[] challengeBytes = StringUtils.toBytes(challenge);
protected static final byte[] challengeBytes = StringUtils.toUtf8Bytes(challenge);
public DigestMd5SaslTest(SASLMechanism saslMechanism) {
super(saslMechanism);

View file

@ -16,6 +16,8 @@
*/
package org.jivesoftware.smackx.hashes;
import static org.jivesoftware.smack.util.StringUtils.encodeHex;
import static org.jivesoftware.smack.util.StringUtils.toUtf8Bytes;
import static org.jivesoftware.smackx.hashes.HashManager.ALGORITHM.BLAKE2B160;
import static org.jivesoftware.smackx.hashes.HashManager.ALGORITHM.BLAKE2B256;
import static org.jivesoftware.smackx.hashes.HashManager.ALGORITHM.BLAKE2B384;
@ -31,8 +33,6 @@ import static org.jivesoftware.smackx.hashes.HashManager.ALGORITHM.SHA_256;
import static org.jivesoftware.smackx.hashes.HashManager.ALGORITHM.SHA_384;
import static org.jivesoftware.smackx.hashes.HashManager.ALGORITHM.SHA_512;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
@ -44,7 +44,6 @@ import java.util.WeakHashMap;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.hashes.element.HashElement;
@ -190,8 +189,10 @@ public final class HashManager extends Manager {
/**
* Compensational method for static 'valueOf' function.
*
* @param s
* @return
* @return the algorithm for the given string.
* @throws IllegalArgumentException if no algorithm for the given string is known.
*/
public static ALGORITHM valueOfName(String s) {
for (ALGORITHM a : ALGORITHM.values()) {
@ -205,16 +206,17 @@ public final class HashManager extends Manager {
/**
* Calculate the hash sum of data using algorithm.
* @param algorithm
* @param data
* @return
*
* @param algorithm the algorithm to use.
* @param data the data to calculate the hash for.
* @return the hash value produced by the given algorithm for the given data.
*/
public static byte[] hash(ALGORITHM algorithm, byte[] data) {
return getMessageDigest(algorithm).digest(data);
}
public static byte[] hash(ALGORITHM algorithm, String data) {
return hash(algorithm, utf8(data));
return hash(algorithm, toUtf8Bytes(data));
}
public static MessageDigest getMessageDigest(ALGORITHM algorithm) {
@ -264,7 +266,7 @@ public final class HashManager extends Manager {
md = MessageDigest.getInstance("BLAKE2b-512", PROVIDER);
break;
default:
throw new AssertionError("Invalid enum value.");
throw new AssertionError("Invalid enum value: " + algorithm);
}
return md;
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
@ -277,15 +279,15 @@ public final class HashManager extends Manager {
}
public static byte[] md5(String data) {
return md5(utf8(data));
return md5(toUtf8Bytes(data));
}
public static String md5HexString(byte[] data) {
return hex(md5(data));
return encodeHex(md5(data));
}
public static String md5HexString(String data) {
return hex(md5(data));
return encodeHex(md5(data));
}
public static byte[] sha_1(byte[] data) {
@ -293,15 +295,15 @@ public final class HashManager extends Manager {
}
public static byte[] sha_1(String data) {
return sha_1(utf8(data));
return sha_1(toUtf8Bytes(data));
}
public static String sha_1HexString(byte[] data) {
return hex(sha_1(data));
return encodeHex(sha_1(data));
}
public static String sha_1HexString(String data) {
return hex(sha_1(data));
return encodeHex(sha_1(data));
}
public static byte[] sha_224(byte[] data) {
@ -309,15 +311,15 @@ public final class HashManager extends Manager {
}
public static byte[] sha_224(String data) {
return sha_224(utf8(data));
return sha_224(toUtf8Bytes(data));
}
public static String sha_224HexString(byte[] data) {
return hex(sha_224(data));
return encodeHex(sha_224(data));
}
public static String sha_224HexString(String data) {
return hex(sha_224(data));
return encodeHex(sha_224(data));
}
public static byte[] sha_256(byte[] data) {
@ -325,15 +327,15 @@ public final class HashManager extends Manager {
}
public static byte[] sha_256(String data) {
return sha_256(utf8(data));
return sha_256(toUtf8Bytes(data));
}
public static String sha_256HexString(byte[] data) {
return hex(sha_256(data));
return encodeHex(sha_256(data));
}
public static String sha_256HexString(String data) {
return hex(sha_256(data));
return encodeHex(sha_256(data));
}
public static byte[] sha_384(byte[] data) {
@ -341,15 +343,15 @@ public final class HashManager extends Manager {
}
public static byte[] sha_384(String data) {
return sha_384(utf8(data));
return sha_384(toUtf8Bytes(data));
}
public static String sha_384HexString(byte[] data) {
return hex(sha_384(data));
return encodeHex(sha_384(data));
}
public static String sha_384HexString(String data) {
return hex(sha_384(data));
return encodeHex(sha_384(data));
}
public static byte[] sha_512(byte[] data) {
@ -357,15 +359,15 @@ public final class HashManager extends Manager {
}
public static byte[] sha_512(String data) {
return sha_512(utf8(data));
return sha_512(toUtf8Bytes(data));
}
public static String sha_512HexString(byte[] data) {
return hex(sha_512(data));
return encodeHex(sha_512(data));
}
public static String sha_512HexString(String data) {
return hex(sha_512(data));
return encodeHex(sha_512(data));
}
public static byte[] sha3_224(byte[] data) {
@ -373,15 +375,15 @@ public final class HashManager extends Manager {
}
public static byte[] sha3_224(String data) {
return sha3_224(utf8(data));
return sha3_224(toUtf8Bytes(data));
}
public static String sha3_224HexString(byte[] data) {
return hex(sha3_224(data));
return encodeHex(sha3_224(data));
}
public static String sha3_224HexString(String data) {
return hex(sha3_224(data));
return encodeHex(sha3_224(data));
}
public static byte[] sha3_256(byte[] data) {
@ -389,15 +391,15 @@ public final class HashManager extends Manager {
}
public static byte[] sha3_256(String data) {
return sha3_256(utf8(data));
return sha3_256(toUtf8Bytes(data));
}
public static String sha3_256HexString(byte[] data) {
return hex(sha3_256(data));
return encodeHex(sha3_256(data));
}
public static String sha3_256HexString(String data) {
return hex(sha3_256(data));
return encodeHex(sha3_256(data));
}
public static byte[] sha3_384(byte[] data) {
@ -405,15 +407,15 @@ public final class HashManager extends Manager {
}
public static byte[] sha3_384(String data) {
return sha3_384(utf8(data));
return sha3_384(toUtf8Bytes(data));
}
public static String sha3_384HexString(byte[] data) {
return hex(sha3_384(data));
return encodeHex(sha3_384(data));
}
public static String sha3_384HexString(String data) {
return hex(sha3_384(data));
return encodeHex(sha3_384(data));
}
public static byte[] sha3_512(byte[] data) {
@ -421,15 +423,15 @@ public final class HashManager extends Manager {
}
public static byte[] sha3_512(String data) {
return sha3_512(utf8(data));
return sha3_512(toUtf8Bytes(data));
}
public static String sha3_512HexString(byte[] data) {
return hex(sha3_512(data));
return encodeHex(sha3_512(data));
}
public static String sha3_512HexString(String data) {
return hex(sha3_512(data));
return encodeHex(sha3_512(data));
}
public static byte[] blake2b160(byte[] data) {
@ -437,15 +439,15 @@ public final class HashManager extends Manager {
}
public static byte[] blake2b160(String data) {
return blake2b160(utf8(data));
return blake2b160(toUtf8Bytes(data));
}
public static String blake2b160HexString(byte[] data) {
return hex(blake2b160(data));
return encodeHex(blake2b160(data));
}
public static String blake2b160HexString(String data) {
return hex(blake2b160(data));
return encodeHex(blake2b160(data));
}
public static byte[] blake2b256(byte[] data) {
@ -453,15 +455,15 @@ public final class HashManager extends Manager {
}
public static byte[] blake2b256(String data) {
return blake2b256(utf8(data));
return blake2b256(toUtf8Bytes(data));
}
public static String blake2b256HexString(byte[] data) {
return hex(blake2b256(data));
return encodeHex(blake2b256(data));
}
public static String blake2b256HexString(String data) {
return hex(blake2b256(data));
return encodeHex(blake2b256(data));
}
public static byte[] blake2b384(byte[] data) {
@ -469,15 +471,15 @@ public final class HashManager extends Manager {
}
public static byte[] blake2b384(String data) {
return blake2b384(utf8(data));
return blake2b384(toUtf8Bytes(data));
}
public static String blake2b384HexString(byte[] data) {
return hex(blake2b384(data));
return encodeHex(blake2b384(data));
}
public static String blake2b384HexString(String data) {
return hex(blake2b384(data));
return encodeHex(blake2b384(data));
}
public static byte[] blake2b512(byte[] data) {
@ -485,32 +487,15 @@ public final class HashManager extends Manager {
}
public static byte[] blake2b512(String data) {
return blake2b512(utf8(data));
return blake2b512(toUtf8Bytes(data));
}
public static String blake2b512HexString(byte[] data) {
return hex(blake2b512(data));
return encodeHex(blake2b512(data));
}
public static String blake2b512HexString(String data) {
return hex(blake2b512(data));
}
/**
* Encode a byte array in HEX.
* @param hash
* @return
*/
public static String hex(byte[] hash) {
return new BigInteger(1, hash).toString(16);
}
public static byte[] utf8(String data) {
try {
return data.getBytes(StringUtils.UTF8);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
return encodeHex(blake2b512(data));
}
}

View file

@ -39,7 +39,7 @@ public class HashElementTest extends SmackTestSuite {
@Test
public void stanzaTest() throws Exception {
String message = "Hello World!";
HashElement element = HashManager.calculateHashElement(SHA_256, HashManager.utf8(message));
HashElement element = HashManager.calculateHashElement(SHA_256, StringUtils.toUtf8Bytes(message));
String expected = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>f4OxZX/x/FO5LcGBSKHWXfwtSx+j1ncoSt3SABJtkGk=</hash>";
assertEquals(expected, element.toXML().toString());

View file

@ -19,6 +19,7 @@ package org.jivesoftware.smackx.hashes;
import static junit.framework.TestCase.assertEquals;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.util.StringUtils;
import org.junit.Test;
@ -47,7 +48,7 @@ public class HashTest extends SmackTestSuite {
private byte[] array() {
if (testArray == null) {
testArray = HashManager.utf8(testString);
testArray = StringUtils.toUtf8Bytes(testString);
}
return testArray;
}
@ -56,103 +57,103 @@ public class HashTest extends SmackTestSuite {
@Test
public void hashTest() {
assertEquals(md5sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.MD5, array())));
assertEquals(sha1sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.SHA_1, array())));
assertEquals(sha224sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.SHA_224, array())));
assertEquals(sha256sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.SHA_256, array())));
assertEquals(sha384sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.SHA_384, array())));
assertEquals(sha512sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.SHA_512, array())));
assertEquals(sha3_224sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.SHA3_224, array())));
assertEquals(sha3_256sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.SHA3_256, array())));
assertEquals(sha3_384sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.SHA3_384, array())));
assertEquals(sha3_512sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.SHA3_512, array())));
assertEquals(b2_160sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.BLAKE2B160, array())));
assertEquals(b2_256sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.BLAKE2B256, array())));
assertEquals(b2_384sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.BLAKE2B384, array())));
assertEquals(b2_512sum, HashManager.hex(HashManager.hash(HashManager.ALGORITHM.BLAKE2B512, array())));
assertEquals(md5sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.MD5, array())));
assertEquals(sha1sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.SHA_1, array())));
assertEquals(sha224sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.SHA_224, array())));
assertEquals(sha256sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.SHA_256, array())));
assertEquals(sha384sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.SHA_384, array())));
assertEquals(sha512sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.SHA_512, array())));
assertEquals(sha3_224sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.SHA3_224, array())));
assertEquals(sha3_256sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.SHA3_256, array())));
assertEquals(sha3_384sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.SHA3_384, array())));
assertEquals(sha3_512sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.SHA3_512, array())));
assertEquals(b2_160sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.BLAKE2B160, array())));
assertEquals(b2_256sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.BLAKE2B256, array())));
assertEquals(b2_384sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.BLAKE2B384, array())));
assertEquals(b2_512sum, StringUtils.encodeHex(HashManager.hash(HashManager.ALGORITHM.BLAKE2B512, array())));
}
@Test
public void md5Test() {
String actual = HashManager.hex(HashManager.md5(array()));
String actual = StringUtils.encodeHex(HashManager.md5(array()));
assertEquals(md5sum, actual);
}
@Test
public void sha1Test() {
String actual = HashManager.hex(HashManager.sha_1(array()));
String actual = StringUtils.encodeHex(HashManager.sha_1(array()));
assertEquals(sha1sum, actual);
}
@Test
public void sha224Test() {
String actual = HashManager.hex(HashManager.sha_224(array()));
String actual = StringUtils.encodeHex(HashManager.sha_224(array()));
assertEquals(sha224sum, actual);
}
@Test
public void sha256Test() {
String actual = HashManager.hex(HashManager.sha_256(array()));
String actual = StringUtils.encodeHex(HashManager.sha_256(array()));
assertEquals(sha256sum, actual);
}
@Test
public void sha384Test() {
String actual = HashManager.hex(HashManager.sha_384(array()));
String actual = StringUtils.encodeHex(HashManager.sha_384(array()));
assertEquals(sha384sum, actual);
}
@Test
public void sha512Test() {
String actual = HashManager.hex(HashManager.sha_512(array()));
String actual = StringUtils.encodeHex(HashManager.sha_512(array()));
assertEquals(sha512sum, actual);
}
@Test
public void sha3_224Test() {
String actual = HashManager.hex(HashManager.sha3_224(array()));
String actual = StringUtils.encodeHex(HashManager.sha3_224(array()));
assertEquals(sha3_224sum, actual);
}
@Test
public void sha3_256Test() {
String actual = HashManager.hex(HashManager.sha3_256(array()));
String actual = StringUtils.encodeHex(HashManager.sha3_256(array()));
assertEquals(sha3_256sum, actual);
}
@Test
public void sha3_384Test() {
String actual = HashManager.hex(HashManager.sha3_384(array()));
String actual = StringUtils.encodeHex(HashManager.sha3_384(array()));
assertEquals(sha3_384sum, actual);
}
@Test
public void sha3_512Test() {
String actual = HashManager.hex(HashManager.sha3_512(array()));
String actual = StringUtils.encodeHex(HashManager.sha3_512(array()));
assertEquals(sha3_512sum, actual);
}
@Test
public void blake2b160Test() {
String actual = HashManager.hex(HashManager.blake2b160(array()));
String actual = StringUtils.encodeHex(HashManager.blake2b160(array()));
assertEquals(b2_160sum, actual);
}
@Test
public void blake2b256Test() {
String actual = HashManager.hex(HashManager.blake2b256(array()));
String actual = StringUtils.encodeHex(HashManager.blake2b256(array()));
assertEquals(b2_256sum, actual);
}
@Test
public void blake2b384Test() {
String actual = HashManager.hex(HashManager.blake2b384(array()));
String actual = StringUtils.encodeHex(HashManager.blake2b384(array()));
assertEquals(b2_384sum, actual);
}
@Test
public void blake2b512Test() {
String actual = HashManager.hex(HashManager.blake2b512(array()));
String actual = StringUtils.encodeHex(HashManager.blake2b512(array()));
assertEquals(b2_512sum, actual);
}