org.conqat.lib.commons.digest.Digester Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-lib-commons Show documentation
Show all versions of teamscale-lib-commons Show documentation
Provides common utility functions
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.conqat.lib.commons.digest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.conqat.lib.commons.error.EnvironmentError;
import org.conqat.lib.commons.string.StringUtils;
/**
* Utility functions for creation of digests.
*
* @deprecated this class uses MD5, which is cryptographically weak and relatively slow to
* calculate. Please only use this for legacy code, where migrating to a new hash
* function is hard (e.g. contained in backups, etc.) and use the faster XXhash (see
* class XXHashUtils) instead.
*/
@Deprecated
public class Digester {
/** Number of bytes in an MD5 sum. */
public static final int MD5_BYTES = 16;
/**
* MD5 Digesters used organized by thread. This is used to avoid recreation of digesters, while
* keeping the code thread safe (i.e. each thread has its own instance).
*/
private static final ThreadLocal MD5_DIGESTERS = new ThreadLocal() {
@Override
protected MessageDigest initialValue() {
return getMD5();
}
};
/**
* Computes an MD5 hash for a string. The hash is always 32 characters long and only uses characters
* from [0-9A-F].
*
* @deprecated this method uses MD5, which is cryptographically weak and relatively slow to
* calculate. Please only use this for legacy code, where migrating to a new hash
* function is hard (e.g. contained in backups, etc.) and use the faster XXhash (see
* class XXHashUtils) instead.
*/
@Deprecated
public static String createMD5Digest(String base) {
return createMD5Digest(base.getBytes());
}
/**
* Computes an MD5 hash for a byte array. The hash is always 32 characters long and only uses
* characters from [0-9A-F].
*
* @deprecated this method uses MD5, which is cryptographically weak and relatively slow to
* calculate. Please only use this for legacy code, where migrating to a new hash
* function is hard (e.g. contained in backups, etc.) and use the faster XXhash (see
* class XXHashUtils) instead.
*/
@Deprecated
public static String createMD5Digest(byte[] data) {
MessageDigest digester = MD5_DIGESTERS.get();
digester.reset();
return StringUtils.encodeAsHex(digester.digest(data));
}
/**
* Computes an MD5 hash for a byte array and returns the binary hash (i.e. no string conversion).
*
* @deprecated this method uses MD5, which is cryptographically weak and relatively slow to
* calculate. Please only use this for legacy code, where migrating to a new hash
* function is hard (e.g. contained in backups, etc.) and use the faster XXhash (see
* class XXHashUtils) instead.
*/
@Deprecated
public static byte[] createBinaryMD5Digest(byte[] data) {
MessageDigest digester = MD5_DIGESTERS.get();
digester.reset();
return digester.digest(data);
}
/**
* Returns MD5 digester or throws an AssertionError if the digester could not be located.
*
* @deprecated this method uses MD5, which is cryptographically weak and relatively slow to
* calculate. Please only use this for legacy code, where migrating to a new hash
* function is hard (e.g. contained in backups, etc.) and use the faster XXhash (see
* class XXHashUtils) instead.
*/
@Deprecated
public static MessageDigest getMD5() {
try {
return MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new EnvironmentError("No MD5 algorithm found. Please check your JRE installation", e);
}
}
}