1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-23 12:52:07 +01:00

Remove deprecated/reserved hash algorithms and add algo names

This commit is contained in:
Paul Schaub 2021-05-15 15:56:27 +02:00
parent 9358e58fb3
commit f2e89bae36

View file

@ -15,8 +15,8 @@
*/ */
package org.pgpainless.algorithm; package org.pgpainless.algorithm;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.bouncycastle.bcpg.HashAlgorithmTags; import org.bouncycastle.bcpg.HashAlgorithmTags;
@ -27,24 +27,22 @@ import org.bouncycastle.bcpg.HashAlgorithmTags;
*/ */
public enum HashAlgorithm { public enum HashAlgorithm {
@Deprecated @Deprecated
MD5 (HashAlgorithmTags.MD5), MD5 (HashAlgorithmTags.MD5, "MD5"),
SHA1 (HashAlgorithmTags.SHA1), SHA1 (HashAlgorithmTags.SHA1, "SHA1"),
RIPEMD160 (HashAlgorithmTags.RIPEMD160), RIPEMD160 (HashAlgorithmTags.RIPEMD160, "RIPEMD160"),
DOUBLE_SHA (HashAlgorithmTags.DOUBLE_SHA), SHA256 (HashAlgorithmTags.SHA256, "SHA256"),
MD2 (HashAlgorithmTags.MD2), SHA384 (HashAlgorithmTags.SHA384, "SHA384"),
TIGER_192 (HashAlgorithmTags.TIGER_192), SHA512 (HashAlgorithmTags.SHA512, "SHA512"),
HAVAL_5_160(HashAlgorithmTags.HAVAL_5_160), SHA224 (HashAlgorithmTags.SHA224, "SHA224"),
SHA256 (HashAlgorithmTags.SHA256),
SHA384 (HashAlgorithmTags.SHA384),
SHA512 (HashAlgorithmTags.SHA512),
SHA224 (HashAlgorithmTags.SHA224),
; ;
private static final Map<Integer, HashAlgorithm> MAP = new ConcurrentHashMap<>(); private static final Map<Integer, HashAlgorithm> ID_MAP = new HashMap<>();
private static final Map<String, HashAlgorithm> NAME_MAP = new HashMap<>();
static { static {
for (HashAlgorithm h : HashAlgorithm.values()) { for (HashAlgorithm h : HashAlgorithm.values()) {
MAP.put(h.algorithmId, h); ID_MAP.put(h.algorithmId, h);
NAME_MAP.put(h.name, h);
} }
} }
@ -56,13 +54,29 @@ public enum HashAlgorithm {
* @return enum value * @return enum value
*/ */
public static HashAlgorithm fromId(int id) { public static HashAlgorithm fromId(int id) {
return MAP.get(id); return ID_MAP.get(id);
}
/**
* Return the {@link HashAlgorithm} value that corresponds to the provided name.
* If an invalid algorithm name was provided, null is returned.
*
* @see <a href="https://datatracker.ietf.org/doc/html/rfc4880#section-9.4">RFC4880: §9.4 Hash Algorithms</a>
* for a list of algorithms and names.
*
* @param name text name
* @return enum value
*/
public static HashAlgorithm fromName(String name) {
return NAME_MAP.get(name);
} }
private final int algorithmId; private final int algorithmId;
private final String name;
HashAlgorithm(int id) { HashAlgorithm(int id, String name) {
this.algorithmId = id; this.algorithmId = id;
this.name = name;
} }
/** /**