mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-23 20:42:06 +01:00
Reorganize HashManager
This commit is contained in:
parent
cf875ef414
commit
3fb2835633
3 changed files with 107 additions and 73 deletions
|
@ -16,30 +16,32 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.hash;
|
package org.jivesoftware.smackx.hash;
|
||||||
|
|
||||||
import org.bouncycastle.jcajce.provider.digest.Blake2b;
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||||
import org.bouncycastle.jcajce.provider.digest.SHA224;
|
|
||||||
import org.bouncycastle.jcajce.provider.digest.SHA256;
|
|
||||||
import org.bouncycastle.jcajce.provider.digest.SHA3;
|
|
||||||
import org.bouncycastle.jcajce.provider.digest.SHA384;
|
|
||||||
import org.bouncycastle.jcajce.provider.digest.SHA512;
|
|
||||||
import org.jivesoftware.smack.Manager;
|
import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.XMPPConnection;
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smack.util.MD5;
|
|
||||||
import org.jivesoftware.smack.util.SHA1;
|
|
||||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.NoSuchProviderException;
|
||||||
|
import java.security.Security;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.BLAKE2B160;
|
||||||
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.BLAKE2B256;
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.BLAKE2B256;
|
||||||
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.BLAKE2B384;
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.BLAKE2B384;
|
||||||
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.BLAKE2B512;
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.BLAKE2B512;
|
||||||
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.MD5;
|
||||||
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA3_224;
|
||||||
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA3_256;
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA3_256;
|
||||||
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA3_384;
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA3_384;
|
||||||
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA3_512;
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA3_512;
|
||||||
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA_1;
|
||||||
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA_224;
|
||||||
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA_256;
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA_256;
|
||||||
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA_384;
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA_384;
|
||||||
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA_512;
|
import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA_512;
|
||||||
|
@ -48,9 +50,30 @@ import static org.jivesoftware.smackx.hash.HashManager.ALGORITHM.SHA_512;
|
||||||
* Manager that can be used to determine support for hash functions.
|
* Manager that can be used to determine support for hash functions.
|
||||||
*/
|
*/
|
||||||
public final class HashManager extends Manager {
|
public final class HashManager extends Manager {
|
||||||
public static final String NAMESPACE_V2 = "urn:xmpp:hashes:2";
|
|
||||||
|
static {
|
||||||
|
Security.addProvider(new BouncyCastleProvider());
|
||||||
|
}
|
||||||
|
public static final String PROVIDER = "BC";
|
||||||
|
|
||||||
public static final String PREFIX_NS_ALGO = "urn:xmpp:hash-function-text-names:";
|
public static final String PREFIX_NS_ALGO = "urn:xmpp:hash-function-text-names:";
|
||||||
|
|
||||||
|
public enum NAMESPACE {
|
||||||
|
V1 ("urn:xmpp:hashes:1"),
|
||||||
|
V2 ("urn:xmpp:hashes:2");
|
||||||
|
|
||||||
|
final String name;
|
||||||
|
|
||||||
|
NAMESPACE(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static final List<ALGORITHM> RECOMMENDED = Collections.unmodifiableList(Arrays.asList(
|
public static final List<ALGORITHM> RECOMMENDED = Collections.unmodifiableList(Arrays.asList(
|
||||||
SHA_256, SHA_384, SHA_512,
|
SHA_256, SHA_384, SHA_512,
|
||||||
SHA3_256, SHA3_384, SHA3_512,
|
SHA3_256, SHA3_384, SHA3_512,
|
||||||
|
@ -68,7 +91,7 @@ public final class HashManager extends Manager {
|
||||||
private HashManager(XMPPConnection connection) {
|
private HashManager(XMPPConnection connection) {
|
||||||
super(connection);
|
super(connection);
|
||||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||||
sdm.addFeature(NAMESPACE_V2);
|
sdm.addFeature(NAMESPACE.V2.toString());
|
||||||
addAlgorithmsToFeatures(RECOMMENDED);
|
addAlgorithmsToFeatures(RECOMMENDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,7 +167,7 @@ public final class HashManager extends Manager {
|
||||||
*/
|
*/
|
||||||
public static ALGORITHM valueOfName(String s) {
|
public static ALGORITHM valueOfName(String s) {
|
||||||
for (ALGORITHM a : ALGORITHM.values()) {
|
for (ALGORITHM a : ALGORITHM.values()) {
|
||||||
if (s.equals(a.toString())) {
|
if (a.toString().equals(s)) {
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -159,110 +182,118 @@ public final class HashManager extends Manager {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static byte[] hash(ALGORITHM algorithm, byte[] data) {
|
public static byte[] hash(ALGORITHM algorithm, byte[] data) {
|
||||||
byte[] hash;
|
return getMessageDigest(algorithm).digest(data);
|
||||||
switch (algorithm) {
|
}
|
||||||
case MD5:
|
|
||||||
hash = md5(data);
|
public static MessageDigest getMessageDigest(ALGORITHM algorithm) {
|
||||||
break;
|
MessageDigest md;
|
||||||
case SHA_1:
|
try {
|
||||||
hash = sha_1(data);
|
switch (algorithm) {
|
||||||
break;
|
case MD5:
|
||||||
case SHA_224:
|
md = MessageDigest.getInstance("MD5", PROVIDER);
|
||||||
hash = sha_224(data);
|
break;
|
||||||
break;
|
case SHA_1:
|
||||||
case SHA_256:
|
md = MessageDigest.getInstance("SHA-1", PROVIDER);
|
||||||
hash = sha_256(data);
|
break;
|
||||||
break;
|
case SHA_224:
|
||||||
case SHA_384:
|
md = MessageDigest.getInstance("SHA-224", PROVIDER);
|
||||||
hash = sha_384(data);
|
break;
|
||||||
break;
|
case SHA_256:
|
||||||
case SHA_512:
|
md = MessageDigest.getInstance("SHA-256", PROVIDER);
|
||||||
hash = sha_512(data);
|
break;
|
||||||
break;
|
case SHA_384:
|
||||||
case SHA3_224:
|
md = MessageDigest.getInstance("SHA-384", PROVIDER);
|
||||||
hash = sha3_224(data);
|
break;
|
||||||
break;
|
case SHA_512:
|
||||||
case SHA3_256:
|
md = MessageDigest.getInstance("SHA-512", PROVIDER);
|
||||||
hash = sha3_256(data);
|
break;
|
||||||
break;
|
case SHA3_224:
|
||||||
case SHA3_384:
|
md = MessageDigest.getInstance("SHA3-224", PROVIDER);
|
||||||
hash = sha3_384(data);
|
break;
|
||||||
break;
|
case SHA3_256:
|
||||||
case SHA3_512:
|
md = MessageDigest.getInstance("SHA3-256", PROVIDER);
|
||||||
hash = sha3_512(data);
|
break;
|
||||||
break;
|
case SHA3_384:
|
||||||
case BLAKE2B160:
|
md = MessageDigest.getInstance("SHA3-384", PROVIDER);
|
||||||
hash = blake2b160(data);
|
break;
|
||||||
break;
|
case SHA3_512:
|
||||||
case BLAKE2B256:
|
md = MessageDigest.getInstance("SHA3-512", PROVIDER);
|
||||||
hash = blake2b256(data);
|
break;
|
||||||
break;
|
case BLAKE2B160:
|
||||||
case BLAKE2B384:
|
md = MessageDigest.getInstance("BLAKE2b-160", PROVIDER);
|
||||||
hash = blake2b384(data);
|
break;
|
||||||
break;
|
case BLAKE2B256:
|
||||||
case BLAKE2B512:
|
md = MessageDigest.getInstance("BLAKE2b-256", PROVIDER);
|
||||||
hash = blake2b512(data);
|
break;
|
||||||
break;
|
case BLAKE2B384:
|
||||||
default:
|
md = MessageDigest.getInstance("BLAKE2b-384", PROVIDER);
|
||||||
throw new AssertionError("Invalid enum value.");
|
break;
|
||||||
|
case BLAKE2B512:
|
||||||
|
md = MessageDigest.getInstance("BLAKE2b-512", PROVIDER);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new AssertionError("Invalid enum value.");
|
||||||
|
}
|
||||||
|
return md;
|
||||||
|
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
|
||||||
|
throw new AssertionError(e);
|
||||||
}
|
}
|
||||||
return hash;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] md5(byte[] data) {
|
public static byte[] md5(byte[] data) {
|
||||||
return MD5.bytes(data);
|
return getMessageDigest(MD5).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] sha_1(byte[] data) {
|
public static byte[] sha_1(byte[] data) {
|
||||||
return SHA1.bytes(data);
|
return getMessageDigest(SHA_1).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] sha_224(byte[] data) {
|
public static byte[] sha_224(byte[] data) {
|
||||||
return new SHA224.Digest().digest(data);
|
return getMessageDigest(SHA_224).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] sha_256(byte[] data) {
|
public static byte[] sha_256(byte[] data) {
|
||||||
return new SHA256.Digest().digest(data);
|
return getMessageDigest(SHA_256).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] sha_384(byte[] data) {
|
public static byte[] sha_384(byte[] data) {
|
||||||
return new SHA384.Digest().digest(data);
|
return getMessageDigest(SHA_384).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] sha_512(byte[] data) {
|
public static byte[] sha_512(byte[] data) {
|
||||||
return new SHA512.Digest().digest(data);
|
return getMessageDigest(SHA_512).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] sha3_224(byte[] data) {
|
public static byte[] sha3_224(byte[] data) {
|
||||||
return new SHA3.Digest224().digest(data);
|
return getMessageDigest(SHA3_224).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] sha3_256(byte[] data) {
|
public static byte[] sha3_256(byte[] data) {
|
||||||
return new SHA3.Digest256().digest(data);
|
return getMessageDigest(SHA3_256).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] sha3_384(byte[] data) {
|
public static byte[] sha3_384(byte[] data) {
|
||||||
return new SHA3.Digest384().digest(data);
|
return getMessageDigest(SHA3_384).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] sha3_512(byte[] data) {
|
public static byte[] sha3_512(byte[] data) {
|
||||||
return new SHA3.Digest512().digest(data);
|
return getMessageDigest(SHA3_512).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] blake2b160(byte[] data) {
|
public static byte[] blake2b160(byte[] data) {
|
||||||
return new Blake2b.Blake2b160().digest(data);
|
return getMessageDigest(BLAKE2B160).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] blake2b256(byte[] data) {
|
public static byte[] blake2b256(byte[] data) {
|
||||||
return new Blake2b.Blake2b256().digest(data);
|
return getMessageDigest(BLAKE2B256).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] blake2b384(byte[] data) {
|
public static byte[] blake2b384(byte[] data) {
|
||||||
return new Blake2b.Blake2b384().digest(data);
|
return getMessageDigest(BLAKE2B384).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] blake2b512(byte[] data) {
|
public static byte[] blake2b512(byte[] data) {
|
||||||
return new Blake2b.Blake2b512().digest(data);
|
return getMessageDigest(BLAKE2B512).digest(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -112,7 +112,7 @@ public class HashElement implements ExtensionElement {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getNamespace() {
|
public String getNamespace() {
|
||||||
return HashManager.NAMESPACE_V2;
|
return HashManager.NAMESPACE.V2.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -23,6 +23,7 @@ import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.packet.IQ;
|
import org.jivesoftware.smack.packet.IQ;
|
||||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
|
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
|
||||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession;
|
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession;
|
||||||
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||||
import org.jivesoftware.smackx.hash.HashManager;
|
import org.jivesoftware.smackx.hash.HashManager;
|
||||||
import org.jivesoftware.smackx.hash.element.HashElement;
|
import org.jivesoftware.smackx.hash.element.HashElement;
|
||||||
import org.jivesoftware.smackx.jingle.JingleManager;
|
import org.jivesoftware.smackx.jingle.JingleManager;
|
||||||
|
@ -67,6 +68,8 @@ public final class JingleFileTransferManager extends Manager {
|
||||||
*/
|
*/
|
||||||
private JingleFileTransferManager(XMPPConnection connection) {
|
private JingleFileTransferManager(XMPPConnection connection) {
|
||||||
super(connection);
|
super(connection);
|
||||||
|
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||||
|
sdm.addFeature(NAMESPACE_V5);
|
||||||
JingleContentProviderManager.addJingleContentDescriptionProvider(
|
JingleContentProviderManager.addJingleContentDescriptionProvider(
|
||||||
NAMESPACE_V5, new JingleContentDescriptionFileTransferProvider());
|
NAMESPACE_V5, new JingleContentDescriptionFileTransferProvider());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue