1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-24 03:27:59 +01:00

Add convenience methods to HashManager

This commit is contained in:
vanitasvitae 2017-06-13 23:51:57 +02:00
parent acc98b4b2f
commit 3ecd01135c
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 186 additions and 11 deletions

View file

@ -19,9 +19,11 @@ package org.jivesoftware.smackx.hashes;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
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;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@ -194,6 +196,10 @@ public final class HashManager extends Manager {
return getMessageDigest(algorithm).digest(data);
}
public static byte[] hash(ALGORITHM algorithm, String data) {
return hash(algorithm, utf8(data));
}
public static MessageDigest getMessageDigest(ALGORITHM algorithm) {
MessageDigest md;
try {
@ -253,58 +259,226 @@ public final class HashManager extends Manager {
return getMessageDigest(MD5).digest(data);
}
public static byte[] md5(String data) {
return md5(utf8(data));
}
public static String md5HexString(byte[] data) {
return hex(md5(data));
}
public static String md5HexString(String data) {
return hex(md5(data));
}
public static byte[] sha_1(byte[] data) {
return getMessageDigest(SHA_1).digest(data);
}
public static byte[] sha_1(String data) {
return sha_1(utf8(data));
}
public static String sha_1HexString(byte[] data) {
return hex(sha_1(data));
}
public static String sha_1HexString(String data) {
return hex(sha_1(data));
}
public static byte[] sha_224(byte[] data) {
return getMessageDigest(SHA_224).digest(data);
}
public static byte[] sha_224(String data) {
return sha_224(utf8(data));
}
public static String sha_224HexString(byte[] data) {
return hex(sha_224(data));
}
public static String sha_224HexString(String data) {
return hex(sha_224(data));
}
public static byte[] sha_256(byte[] data) {
return getMessageDigest(SHA_256).digest(data);
}
public static byte[] sha_256(String data) {
return sha_256(utf8(data));
}
public static String sha_256HexString(byte[] data) {
return hex(sha_256(data));
}
public static String sha_256HexString(String data) {
return hex(sha_256(data));
}
public static byte[] sha_384(byte[] data) {
return getMessageDigest(SHA_384).digest(data);
}
public static byte[] sha_384(String data) {
return sha_384(utf8(data));
}
public static String sha_384HexString(byte[] data) {
return hex(sha_384(data));
}
public static String sha_384HexString(String data) {
return hex(sha_384(data));
}
public static byte[] sha_512(byte[] data) {
return getMessageDigest(SHA_512).digest(data);
}
public static byte[] sha_512(String data) {
return sha_512(utf8(data));
}
public static String sha_512HexString(byte[] data) {
return hex(sha_512(data));
}
public static String sha_512HexString(String data) {
return hex(sha_512(data));
}
public static byte[] sha3_224(byte[] data) {
return getMessageDigest(SHA3_224).digest(data);
}
public static byte[] sha3_224(String data) {
return sha3_224(utf8(data));
}
public static String sha3_224HexString(byte[] data) {
return hex(sha3_224(data));
}
public static String sha3_224HexString(String data) {
return hex(sha3_224(data));
}
public static byte[] sha3_256(byte[] data) {
return getMessageDigest(SHA3_256).digest(data);
}
public static byte[] sha3_256(String data) {
return sha3_256(utf8(data));
}
public static String sha3_256HexString(byte[] data) {
return hex(sha3_256(data));
}
public static String sha3_256HexString(String data) {
return hex(sha3_256(data));
}
public static byte[] sha3_384(byte[] data) {
return getMessageDigest(SHA3_384).digest(data);
}
public static byte[] sha3_384(String data) {
return sha3_384(utf8(data));
}
public static String sha3_384HexString(byte[] data) {
return hex(sha3_384(data));
}
public static String sha3_384HexString(String data) {
return hex(sha3_384(data));
}
public static byte[] sha3_512(byte[] data) {
return getMessageDigest(SHA3_512).digest(data);
}
public static byte[] sha3_512(String data) {
return sha3_512(utf8(data));
}
public static String sha3_512HexString(byte[] data) {
return hex(sha3_512(data));
}
public static String sha3_512HexString(String data) {
return hex(sha3_512(data));
}
public static byte[] blake2b160(byte[] data) {
return getMessageDigest(BLAKE2B160).digest(data);
}
public static byte[] blake2b160(String data) {
return blake2b160(utf8(data));
}
public static String blake2b160HexString(byte[] data) {
return hex(blake2b160(data));
}
public static String blake2b160HexString(String data) {
return hex(blake2b160(data));
}
public static byte[] blake2b256(byte[] data) {
return getMessageDigest(BLAKE2B256).digest(data);
}
public static byte[] blake2b256(String data) {
return blake2b256(utf8(data));
}
public static String blake2b256HexString(byte[] data) {
return hex(blake2b256(data));
}
public static String blake2b256HexString(String data) {
return hex(blake2b256(data));
}
public static byte[] blake2b384(byte[] data) {
return getMessageDigest(BLAKE2B384).digest(data);
}
public static byte[] blake2b384(String data) {
return blake2b384(utf8(data));
}
public String blake2b384HexString(byte[] data) {
return hex(blake2b384(data));
}
public String blake2b384HexString(String data) {
return hex(blake2b384(data));
}
public static byte[] blake2b512(byte[] data) {
return getMessageDigest(BLAKE2B512).digest(data);
}
public static byte[] blake2b512(String data) {
return blake2b512(utf8(data));
}
public String blake2b512HexString(byte[] data) {
return hex(blake2b512(data));
}
public String blake2b512HexString(String data) {
return hex(blake2b512(data));
}
/**
* Encode a byte array in HEX.
* @param hash
@ -314,4 +488,12 @@ public final class HashManager extends Manager {
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);
}
}
}

View file

@ -23,11 +23,11 @@ import org.jivesoftware.smackx.hashes.element.HashElement;
import org.jivesoftware.smackx.hashes.provider.HashElementProvider;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
import static org.jivesoftware.smackx.hashes.HashManager.ALGORITHM.SHA_256;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static junit.framework.TestCase.assertEquals;
/**
* Test toXML and parse of HashElement and HashElementProvider.
@ -37,7 +37,7 @@ public class HashElementTest extends SmackTestSuite {
@Test
public void stanzaTest() throws Exception {
String message = "Hello World!";
HashElement element = HashManager.calculateHashElement(SHA_256, message.getBytes(StringUtils.UTF8));
HashElement element = HashManager.calculateHashElement(SHA_256, HashManager.utf8(message));
String expected = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>f4OxZX/x/FO5LcGBSKHWXfwtSx+j1ncoSt3SABJtkGk=</hash>";
assertEquals(expected, element.toXML().toString());
@ -45,7 +45,7 @@ public class HashElementTest extends SmackTestSuite {
assertEquals(expected, parsed.toXML().toString());
assertEquals(SHA_256, parsed.getAlgorithm());
assertEquals("f4OxZX/x/FO5LcGBSKHWXfwtSx+j1ncoSt3SABJtkGk=", parsed.getHashB64());
assertArrayEquals(HashManager.sha_256(message.getBytes(StringUtils.UTF8)), parsed.getHash());
assertArrayEquals(HashManager.sha_256(message), parsed.getHash());
assertFalse(parsed.equals(expected));
assertFalse(parsed.equals(null));

View file

@ -17,11 +17,8 @@
package org.jivesoftware.smackx.hashes;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.util.StringUtils;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import static junit.framework.TestCase.assertEquals;
/**
@ -49,11 +46,7 @@ public class HashTest extends SmackTestSuite {
private byte[] array() {
if (testArray == null) {
try {
testArray = testString.getBytes(StringUtils.UTF8);
} catch (UnsupportedEncodingException e) {
throw new AssertionError("UTF8 MUST be supported.");
}
testArray = HashManager.utf8(testString);
}
return testArray;
}